From 7ddc18086d90fdbfdf08063ec061969efdc028d9 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Sat, 20 Jul 2019 09:09:13 -0400 Subject: [PATCH] Comment deletion query handling --- src/database.py | 9 +++------ src/handles.py | 13 ++++++++----- src/misc.py | 12 ++++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/database.py b/src/database.py index cb385c4..6753e8f 100644 --- a/src/database.py +++ b/src/database.py @@ -156,13 +156,10 @@ def delete_anonymous_comment_by_id(conn: sqlite3.Connection, comment_id: str): return curs.rowcount -def delete_channel_comment_by_id(conn: sqlite3.Connection, comment_id: str, channel_id: str): +def delete_comment_by_id(conn: sqlite3.Connection, comment_id: str): with conn: - curs = conn.execute( - "DELETE FROM COMMENT WHERE ChannelId = ? AND CommentId = ?", - (channel_id, comment_id) - ) - return curs.rowcount + curs = conn.execute("DELETE FROM COMMENT WHERE CommentId = ?", (comment_id,)) + return bool(curs.rowcount) def insert_channel(conn: sqlite3.Connection, channel_name: str, channel_id: str): diff --git a/src/handles.py b/src/handles.py index 2fc4696..ca3677e 100644 --- a/src/handles.py +++ b/src/handles.py @@ -1,5 +1,4 @@ # cython: language_level=3 -import json import logging import asyncio @@ -7,12 +6,12 @@ from aiohttp import web from aiojobs.aiohttp import atomic from asyncio import coroutine -from misc import clean_input_params, ERRORS +from misc import clean_input_params from src.database import get_claim_comments from src.database import get_comments_by_id, get_comment_ids from src.database import get_channel_from_comment_id from src.database import obtain_connection -from src.database import delete_channel_comment_by_id +from src.database import delete_comment_by_id from src.writes import create_comment_or_error from src.misc import is_authentic_delete_signal @@ -48,6 +47,10 @@ async def write_comment(app, comment): return await coroutine(create_comment_or_error)(app['writer'], **comment) +async def delete_comment(app, comment_id): + return await coroutine(delete_comment_by_id)(app['writer'], comment_id) + + async def handle_create_comment(app, params): job = await app['comment_scheduler'].spawn(write_comment(app, params)) return await job.wait() @@ -58,8 +61,7 @@ async def delete_comment_if_authorized(app, comment_id, channel_name, channel_id if not authorized: return {'deleted': False} - delete_query = delete_channel_comment_by_id(app['writer'], comment_id, channel_id) - job = await app['comment_scheduler'].spawn(delete_query) + job = await app['comment_scheduler'].spawn(delete_comment(app, comment_id)) return {'deleted': await job.wait()} @@ -75,6 +77,7 @@ METHODS = { 'get_channel_from_comment_id': handle_get_channel_from_comment_id, 'create_comment': handle_create_comment, 'delete_comment': handle_delete_comment, + 'abandon_comment': handle_delete_comment, } diff --git a/src/misc.py b/src/misc.py index 32ce4ea..31ffa8d 100644 --- a/src/misc.py +++ b/src/misc.py @@ -3,7 +3,7 @@ import logging import re from json import JSONDecodeError -from nacl.hash import sha256 +import hashlib import aiohttp import ecdsa @@ -53,10 +53,13 @@ async def resolve_channel_claim(app: dict, channel_id: str, channel_name: str): async with aiohttp.request('POST', app['config']['LBRYNET'], json=resolve_body) as req: try: resp = await req.json() - return resp.get(lbry_url) except JSONDecodeError as jde: logger.exception(jde.msg) - raise Exception(jde) + raise Exception('JSON Decode Error in Claim Resolution') + finally: + if 'result' in resp: + return resp['result'].get(lbry_url) + raise ValueError('claim resolution yields error', {'error': resp['error']}) def is_signature_valid(encoded_signature, signature_digest, public_key_bytes): @@ -85,9 +88,10 @@ async def is_authentic_delete_signal(app, comment_id: str, channel_name: str, ch if claim: public_key = claim['value']['public_key'] claim_hash = binascii.unhexlify(claim['claim_id'].encode())[::-1] + pieces_injest = b''.join((comment_id.encode(), claim_hash)) return is_signature_valid( encoded_signature=get_encoded_signature(signature), - signature_digest=sha256(b''.join([comment_id.encode(), claim_hash])), + signature_digest=hashlib.sha256(pieces_injest).digest(), public_key_bytes=binascii.unhexlify(public_key.encode()) ) return False