create_comment doesnt require claim_id when parent_id is specified #31

Merged
osilkin98 merged 1 commit from soften-dependencies into master 2020-01-04 07:08:22 +01:00
3 changed files with 38 additions and 11 deletions

View file

@ -109,30 +109,56 @@ def claim_has_hidden_comments(conn, claim_id):
return bool(tuple(result.fetchone())[0]) return bool(tuple(result.fetchone())[0])
def insert_comment(conn: sqlite3.Connection, claim_id: str, comment: str, parent_id: str = None, def insert_comment(conn: sqlite3.Connection, claim_id: str, comment: str,
channel_id: str = None, signature: str = None, signing_ts: str = None) -> str: channel_id: str = None, signature: str = None, signing_ts: str = None, **extra) -> str:
timestamp = int(time.time()) timestamp = int(time.time())
prehash = b':'.join((claim_id.encode(), comment.encode(), str(timestamp).encode(),)) prehash = b':'.join((claim_id.encode(), comment.encode(), str(timestamp).encode(),))
comment_id = nacl.hash.sha256(prehash).decode() comment_id = nacl.hash.sha256(prehash).decode()
with conn: with conn:
conn.execute( curs = conn.execute(
""" """
INSERT INTO COMMENT(CommentId, LbryClaimId, ChannelId, Body, ParentId, INSERT INTO COMMENT(CommentId, LbryClaimId, ChannelId, Body, ParentId,
Timestamp, Signature, SigningTs, IsHidden) Timestamp, Signature, SigningTs, IsHidden)
VALUES (:comment_id, :claim_id, :channel_id, :comment, :parent_id, VALUES (:comment_id, :claim_id, :channel_id, :comment, NULL,
:timestamp, :signature, :signing_ts, 0) """, :timestamp, :signature, :signing_ts, 0) """,
{ {
'comment_id': comment_id, 'comment_id': comment_id,
'claim_id': claim_id, 'claim_id': claim_id,
'channel_id': channel_id, 'channel_id': channel_id,
'comment': comment, 'comment': comment,
'parent_id': parent_id,
'timestamp': timestamp, 'timestamp': timestamp,
'signature': signature, 'signature': signature,
'signing_ts': signing_ts 'signing_ts': signing_ts
} }
) )
logging.info('Inserted Comment into DB, `comment_id`: %s', comment_id) logging.info('attempted to insert comment with comment_id [%s] | %d rows affected', comment_id, curs.rowcount)
return comment_id
def insert_reply(conn: sqlite3.Connection, comment: str, parent_id: str,
channel_id: str = None, signature: str = None,
signing_ts: str = None, **extra) -> str:
timestamp = int(time.time())
prehash = b':'.join((parent_id.encode(), comment.encode(), str(timestamp).encode(),))
comment_id = nacl.hash.sha256(prehash).decode()
with conn:
curs = conn.execute(
"""
INSERT INTO COMMENT
(CommentId, LbryClaimId, ChannelId, Body, ParentId, Signature, Timestamp, SigningTs, IsHidden)
SELECT :comment_id, LbryClaimId, :channel_id, :comment, :parent_id, :signature, :timestamp, :signing_ts, 0
FROM COMMENT WHERE CommentId = :parent_id
""", {
'comment_id': comment_id,
'parent_id': parent_id,
'timestamp': timestamp,
'comment': comment,
'channel_id': channel_id,
'signature': signature,
'signing_ts': signing_ts
}
)
logging.info('attempted to insert reply with comment_id [%s] | %d rows affected', comment_id, curs.rowcount)
return comment_id return comment_id

View file

@ -16,11 +16,12 @@ import src.database.queries as db
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def create_comment_or_error(conn, comment, claim_id, channel_id=None, channel_name=None, def create_comment_or_error(conn, comment, claim_id=None, channel_id=None, channel_name=None,
signature=None, signing_ts=None, parent_id=None) -> dict: signature=None, signing_ts=None, parent_id=None) -> dict:
if channel_id or channel_name or signature or signing_ts: if channel_id and channel_name:
insert_channel_or_error(conn, channel_name, channel_id) insert_channel_or_error(conn, channel_name, channel_id)
comment_id = db.insert_comment( fn = db.insert_comment if parent_id is None else db.insert_reply
comment_id = fn(
conn=conn, conn=conn,
comment=comment, comment=comment,
claim_id=claim_id, claim_id=claim_id,

View file

@ -52,8 +52,8 @@ def claim_id_is_valid(claim_id: str) -> bool:
def is_valid_base_comment(comment: str, claim_id: str, parent_id: str = None, **kwargs) -> bool: def is_valid_base_comment(comment: str, claim_id: str, parent_id: str = None, **kwargs) -> bool:
return comment is not None and body_is_valid(comment) and \ return comment is not None and body_is_valid(comment) and \
claim_id is not None and claim_id_is_valid(claim_id) and \ ((claim_id is not None and claim_id_is_valid(claim_id)) or
(parent_id is None or comment_id_is_valid(parent_id)) (parent_id is not None and comment_id_is_valid(parent_id)))
def is_valid_credential_input(channel_id: str = None, channel_name: str = None, def is_valid_credential_input(channel_id: str = None, channel_name: str = None,