Adds Hide Comment Functionality + Updates #7

Merged
osilkin98 merged 21 commits from hide-comments into master 2019-08-09 09:13:38 +02:00
Showing only changes of commit 7f98417d9d - Show all commits

View file

@ -12,6 +12,13 @@ from src.database.schema import CREATE_TABLES_QUERY
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
SELECT_COMMENTS_ON_CLAIMS = """
SELECT comment, comment_id, channel_name, channel_id, channel_url,
timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
"""
def clean(thing: dict) -> dict: def clean(thing: dict) -> dict:
if 'is_hidden' in thing: if 'is_hidden' in thing:
thing.update({'is_hidden': bool(thing['is_hidden'])}) thing.update({'is_hidden': bool(thing['is_hidden'])})
@ -30,51 +37,30 @@ def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str =
with conn: with conn:
if top_level: if top_level:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, SELECT_COMMENTS_ON_CLAIMS + " WHERE claim_id = ? AND parent_id IS NULL LIMIT ? OFFSET ?",
channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id IS NULL
LIMIT ? OFFSET ? """,
(claim_id, page_size, page_size * (page - 1)) (claim_id, page_size, page_size * (page - 1))
)] )]
count = conn.execute( count = conn.execute(
""" "SELECT COUNT(*) FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND parent_id IS NULL",
SELECT COUNT(*) (claim_id,)
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id IS NULL
""", (claim_id,)
) )
elif parent_id is None: elif parent_id is None:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, SELECT_COMMENTS_ON_CLAIMS + "WHERE claim_id = ? LIMIT ? OFFSET ? ",
channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ?
LIMIT ? OFFSET ? """,
(claim_id, page_size, page_size * (page - 1)) (claim_id, page_size, page_size * (page - 1))
)] )]
count = conn.execute( count = conn.execute(
""" "SELECT COUNT(*) FROM COMMENTS_ON_CLAIMS WHERE claim_id = ?",
SELECT COUNT(*) (claim_id,)
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ?
""", (claim_id,)
) )
else: else:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, SELECT_COMMENTS_ON_CLAIMS + "WHERE claim_id = ? AND parent_id = ? LIMIT ? OFFSET ? ",
channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id = ?
LIMIT ? OFFSET ? """,
(claim_id, parent_id, page_size, page_size * (page - 1)) (claim_id, parent_id, page_size, page_size * (page - 1))
)] )]
count = conn.execute( count = conn.execute(
""" "SELECT COUNT(*) FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND parent_id = ?",
SELECT COUNT(*) (claim_id, parent_id)
FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id = ?
""", (claim_id, parent_id)
) )
count = tuple(count.fetchone())[0] count = tuple(count.fetchone())[0]
return { return {
@ -105,13 +91,7 @@ def insert_comment(conn: sqlite3.Connection, claim_id: str, comment: str, parent
def get_comment_or_none(conn: sqlite3.Connection, comment_id: str) -> dict: def get_comment_or_none(conn: sqlite3.Connection, comment_id: str) -> dict:
with conn: with conn:
curry = conn.execute( curry = conn.execute(SELECT_COMMENTS_ON_CLAIMS + "WHERE comment_id = ?", (comment_id,))
"""
SELECT comment, claim_id, comment_id, channel_name, channel_id, channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS WHERE comment_id = ?
""",
(comment_id,)
)
thing = curry.fetchone() thing = curry.fetchone()
return clean(dict(thing)) if thing else None return clean(dict(thing)) if thing else None
@ -146,24 +126,11 @@ def get_comments_by_id(conn, comment_ids: list) -> typing.Union[list, None]:
placeholders = ', '.join('?' for _ in comment_ids) placeholders = ', '.join('?' for _ in comment_ids)
with conn: with conn:
return [clean(dict(row)) for row in conn.execute( return [clean(dict(row)) for row in conn.execute(
""" SELECT_COMMENTS_ON_CLAIMS + f'WHERE comment_id IN ({placeholders})',
SELECT comment, claim_id, comment_id, channel_name, channel_id,
channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
""" + f' WHERE comment_id IN ({placeholders})',
tuple(comment_ids) tuple(comment_ids)
)] )]
def delete_anonymous_comment_by_id(conn: sqlite3.Connection, comment_id: str):
with conn:
curs = conn.execute(
"DELETE FROM COMMENT WHERE ChannelId IS NULL AND CommentId = ?",
(comment_id,)
)
return curs.rowcount
def delete_comment_by_id(conn: sqlite3.Connection, comment_id: str): def delete_comment_by_id(conn: sqlite3.Connection, comment_id: str):
with conn: with conn:
curs = conn.execute("DELETE FROM COMMENT WHERE CommentId = ?", (comment_id,)) curs = conn.execute("DELETE FROM COMMENT WHERE CommentId = ?", (comment_id,))
@ -172,26 +139,20 @@ def delete_comment_by_id(conn: sqlite3.Connection, comment_id: str):
def hide_comment_by_id(conn: sqlite3.Connection, comment_id: str): def hide_comment_by_id(conn: sqlite3.Connection, comment_id: str):
with conn: with conn:
curs = conn.execute(""" curs = conn.execute("UPDATE OR IGNORE COMMENT SET IsHidden = TRUE WHERE CommentId = ?", (comment_id,))
UPDATE OR IGNORE COMMENT SET IsHidden = TRUE
WHERE CommentId = ?
""", (comment_id,))
return bool(curs.rowcount) return bool(curs.rowcount)
def insert_channel(conn: sqlite3.Connection, channel_name: str, channel_id: str): def insert_channel(conn: sqlite3.Connection, channel_name: str, channel_id: str):
with conn: with conn:
conn.execute( curs = conn.execute('INSERT INTO CHANNEL(ClaimId, Name) VALUES (?, ?)', (channel_id, channel_name))
'INSERT INTO CHANNEL(ClaimId, Name) VALUES (?, ?)', return bool(curs.rowcount)
(channel_id, channel_name)
)
def get_channel_id_from_comment_id(conn: sqlite3.Connection, comment_id: str): def get_channel_id_from_comment_id(conn: sqlite3.Connection, comment_id: str):
with conn: with conn:
channel = conn.execute(""" channel = conn.execute(
SELECT channel_id, channel_name FROM COMMENTS_ON_CLAIMS WHERE comment_id = ? "SELECT channel_id, channel_name FROM COMMENTS_ON_CLAIMS WHERE comment_id = ?", (comment_id,)
""", (comment_id,)
).fetchone() ).fetchone()
return dict(channel) if channel else {} return dict(channel) if channel else {}