diff --git a/src/database/comments_ddl.sql b/src/database/comments_ddl.sql index 4eebe13..2dfc19d 100644 --- a/src/database/comments_ddl.sql +++ b/src/database/comments_ddl.sql @@ -9,13 +9,13 @@ CREATE TABLE IF NOT EXISTS COMMENT ( CommentId TEXT NOT NULL, LbryClaimId TEXT NOT NULL, - ChannelId TEXT DEFAULT NULL, + ChannelId TEXT DEFAULT NULL, Body TEXT NOT NULL, - ParentId TEXT DEFAULT NULL, - Signature TEXT DEFAULT NULL, + ParentId TEXT DEFAULT NULL, + Signature TEXT DEFAULT NULL, Timestamp INTEGER NOT NULL, - SigningTs TEXT DEFAULT NULL, - IsHidden BOOLEAN NOT NULL DEFAULT FALSE, + SigningTs TEXT DEFAULT NULL, + IsHidden BOOLEAN NOT NULL DEFAULT FALSE, CONSTRAINT COMMENT_PRIMARY_KEY PRIMARY KEY (CommentId) ON CONFLICT IGNORE, CONSTRAINT COMMENT_SIGNATURE_SK UNIQUE (Signature) ON CONFLICT ABORT, CONSTRAINT COMMENT_CHANNEL_FK FOREIGN KEY (ChannelId) REFERENCES CHANNEL (ClaimId) @@ -44,21 +44,21 @@ CREATE TABLE IF NOT EXISTS CHANNEL -- CREATE INDEX IF NOT EXISTS CHANNEL_COMMENT_INDEX ON COMMENT (ChannelId, CommentId); -- VIEWS -CREATE VIEW IF NOT EXISTS COMMENTS_ON_CLAIMS AS SELECT - C.CommentId AS comment_id, - C.Body AS comment, - C.LbryClaimId AS claim_id, - C.Timestamp AS timestamp, - CHAN.Name AS channel_name, - CHAN.ClaimId AS channel_id, - ('lbry://' || CHAN.Name || '#' || CHAN.ClaimId) AS channel_url, - C.Signature AS signature, - C.SigningTs AS signing_ts, - C.ParentId AS parent_id, - C.IsHidden AS is_hidden - FROM COMMENT AS C - LEFT OUTER JOIN CHANNEL CHAN ON C.ChannelId = CHAN.ClaimId - ORDER BY C.Timestamp DESC; +CREATE VIEW IF NOT EXISTS COMMENTS_ON_CLAIMS AS +SELECT C.CommentId AS comment_id, + C.Body AS comment, + C.LbryClaimId AS claim_id, + C.Timestamp AS timestamp, + CHAN.Name AS channel_name, + CHAN.ClaimId AS channel_id, + ('lbry://' || CHAN.Name || '#' || CHAN.ClaimId) AS channel_url, + C.Signature AS signature, + C.SigningTs AS signing_ts, + C.ParentId AS parent_id, + C.IsHidden AS is_hidden +FROM COMMENT AS C + LEFT OUTER JOIN CHANNEL CHAN ON C.ChannelId = CHAN.ClaimId +ORDER BY C.Timestamp DESC; DROP VIEW IF EXISTS COMMENT_REPLIES; diff --git a/src/database/queries.py b/src/database/queries.py index 4d16f80..325d38f 100644 --- a/src/database/queries.py +++ b/src/database/queries.py @@ -1,17 +1,16 @@ import atexit import logging +import math import sqlite3 import time import typing -import math import nacl.hash from src.database.schema import CREATE_TABLES_QUERY 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 @@ -95,7 +94,7 @@ def get_claim_hidden_comments(conn: sqlite3.Connection, claim_id: str, hidden=Tr 'items': results, 'page': page, 'page_size': page_size, - 'total_pages': math.ceil(count/page_size), + 'total_pages': math.ceil(count / page_size), 'total_items': count, 'has_hidden_comments': claim_has_hidden_comments(conn, claim_id) } @@ -157,14 +156,14 @@ def get_comment_ids(conn: sqlite3.Connection, claim_id: str, parent_id: str = No curs = conn.execute(""" SELECT comment_id FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND parent_id IS NULL LIMIT ? OFFSET ? - """, (claim_id, page_size, page_size*abs(page - 1),) - ) + """, (claim_id, page_size, page_size * abs(page - 1),) + ) else: curs = conn.execute(""" SELECT comment_id FROM COMMENTS_ON_CLAIMS WHERE claim_id = ? AND parent_id = ? LIMIT ? OFFSET ? """, (claim_id, parent_id, page_size, page_size * abs(page - 1),) - ) + ) return [tuple(row)[0] for row in curs.fetchall()] diff --git a/src/database/schema.py b/src/database/schema.py index d56ad1f..c75681b 100644 --- a/src/database/schema.py +++ b/src/database/schema.py @@ -67,11 +67,10 @@ ORDER BY OG.Timestamp; """ CREATE_TABLES_QUERY = ( - PRAGMAS + - CREATE_COMMENT_TABLE + - CREATE_COMMENT_INDEXES + - CREATE_CHANNEL_TABLE + - CREATE_COMMENTS_ON_CLAIMS_VIEW + - CREATE_COMMENT_REPLIES_VIEW + PRAGMAS + + CREATE_COMMENT_TABLE + + CREATE_COMMENT_INDEXES + + CREATE_CHANNEL_TABLE + + CREATE_COMMENTS_ON_CLAIMS_VIEW + + CREATE_COMMENT_REPLIES_VIEW ) - diff --git a/src/database/writes.py b/src/database/writes.py index 611874a..4672140 100644 --- a/src/database/writes.py +++ b/src/database/writes.py @@ -1,6 +1,5 @@ import logging import sqlite3 - from asyncio import coroutine from src.database.queries import delete_comment_by_id, get_comments_by_id @@ -38,7 +37,7 @@ def create_comment_or_error(conn, comment, claim_id, channel_id=None, channel_na def insert_channel_or_error(conn: sqlite3.Connection, channel_name: str, channel_id: str): try: - channel_matches_pattern_or_error(channel_id, channel_name) + is_valid_channel(channel_id, channel_name) insert_channel(conn, channel_name, channel_id) except AssertionError: logger.exception('Invalid channel values given') @@ -63,17 +62,6 @@ async def abandon_comment(app, comment_id): # DELETE """ Core Functions called by request handlers """ -async def abandon_comment_if_authorized(app, comment_id, channel_id, signature, signing_ts, **kwargs): - claim = await get_claim_from_id(app, channel_id) - if not validate_signature_from_claim(claim, signature, signing_ts, comment_id): - return False - - comment = get_comment_or_none(app['reader'], comment_id) - job = await app['comment_scheduler'].spawn(abandon_comment(app, comment_id)) - await app['webhooks'].spawn(send_notification(app, 'DELETE', comment)) - return await job.wait() - - async def create_comment(app, params): if is_valid_base_comment(**params) and is_valid_credential_input(**params): job = await app['comment_scheduler'].spawn(write_comment(app, params)) @@ -113,3 +101,20 @@ async def hide_comments_where_authorized(app, pieces: list) -> list: await job.wait() return comment_ids + + +async def edit_comment(app, comment_id, new_comment, channel_id, + channel_name, new_signature, new_signing_ts): + + pass + + +async def abandon_comment_if_authorized(app, comment_id, channel_id, signature, signing_ts, **kwargs): + channel = await get_claim_from_id(app, channel_id) + if not validate_signature_from_claim(channel, signature, signing_ts, comment_id): + return False + + comment = get_comment_or_none(app['reader'], comment_id) + job = await app['comment_scheduler'].spawn(abandon_comment(app, comment_id)) + await app['webhooks'].spawn(send_notification(app, 'DELETE', comment)) + return await job.wait() diff --git a/src/main.py b/src/main.py index 859e224..b31bcea 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,10 @@ -import logging.config -import logging import argparse +import logging +import logging.config import sys -from src.settings import config from src.server.app import run_app +from src.settings import config def config_logging_from_settings(conf):