From 1b79aefb054411b526ac82367c83d9b4d773fe1c Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Sun, 4 Aug 2019 17:46:33 -0400 Subject: [PATCH] Adds hidden comment queries --- src/database/queries.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/database/queries.py b/src/database/queries.py index 2dca72b..7dd780c 100644 --- a/src/database/queries.py +++ b/src/database/queries.py @@ -22,7 +22,7 @@ SELECT_COMMENTS_ON_CLAIMS = """ def clean(thing: dict) -> dict: if 'is_hidden' in thing: thing.update({'is_hidden': bool(thing['is_hidden'])}) - return {k: v for k, v in thing.items() if v} + return {k: v for k, v in thing.items() if v is not None} def obtain_connection(filepath: str = None, row_factory: bool = True): @@ -68,10 +68,42 @@ def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str = 'page': page, 'page_size': page_size, 'total_pages': math.ceil(count / page_size), - 'total_items': count + 'total_items': count, + 'has_hidden_comments': claim_has_hidden_comments(conn, claim_id) } +def get_hidden_claim_comments(conn: sqlite3.Connection, claim_id: str, hidden=True, page=1, page_size=50): + with conn: + results = conn.execute( + SELECT_COMMENTS_ON_CLAIMS + "WHERE claim_id = ? AND is_hidden = ? LIMIT ? OFFSET ?", + (claim_id, hidden, page_size, page_size * (page - 1)) + ) + count = conn.execute( + "SELECT COUNT(*) FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND is_hidden = ?", (claim_id, hidden) + ) + results = [clean(dict(row)) for row in results.fetchall()] + count = tuple(count.fetchone())[0] + + return { + 'items': results, + 'page': page, + 'page_size': page_size, + 'total_pages': math.ceil(count/page_size), + 'total_items': count, + 'has_hidden_comments': claim_has_hidden_comments(conn, claim_id) + } + + +def claim_has_hidden_comments(conn, claim_id): + with conn: + result = conn.execute( + "SELECT COUNT(DISTINCT is_hidden) FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND is_hidden = TRUE", + (claim_id,) + ) + return bool(tuple(result.fetchone())[0]) + + def insert_comment(conn: sqlite3.Connection, claim_id: str, comment: str, parent_id: str = None, channel_id: str = None, signature: str = None, signing_ts: str = None) -> str: timestamp = int(time.time())