Comment deletion query handling

This commit is contained in:
Oleg Silkin 2019-07-20 09:09:13 -04:00
parent 43d23c4a48
commit 7ddc18086d
3 changed files with 19 additions and 15 deletions

View file

@ -156,13 +156,10 @@ def delete_anonymous_comment_by_id(conn: sqlite3.Connection, comment_id: str):
return curs.rowcount 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: with conn:
curs = conn.execute( curs = conn.execute("DELETE FROM COMMENT WHERE CommentId = ?", (comment_id,))
"DELETE FROM COMMENT WHERE ChannelId = ? AND CommentId = ?", return bool(curs.rowcount)
(channel_id, comment_id)
)
return 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):

View file

@ -1,5 +1,4 @@
# cython: language_level=3 # cython: language_level=3
import json
import logging import logging
import asyncio import asyncio
@ -7,12 +6,12 @@ from aiohttp import web
from aiojobs.aiohttp import atomic from aiojobs.aiohttp import atomic
from asyncio import coroutine 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_claim_comments
from src.database import get_comments_by_id, get_comment_ids from src.database import get_comments_by_id, get_comment_ids
from src.database import get_channel_from_comment_id from src.database import get_channel_from_comment_id
from src.database import obtain_connection 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.writes import create_comment_or_error
from src.misc import is_authentic_delete_signal 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) 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): async def handle_create_comment(app, params):
job = await app['comment_scheduler'].spawn(write_comment(app, params)) job = await app['comment_scheduler'].spawn(write_comment(app, params))
return await job.wait() return await job.wait()
@ -58,8 +61,7 @@ async def delete_comment_if_authorized(app, comment_id, channel_name, channel_id
if not authorized: if not authorized:
return {'deleted': False} return {'deleted': False}
delete_query = delete_channel_comment_by_id(app['writer'], comment_id, channel_id) job = await app['comment_scheduler'].spawn(delete_comment(app, comment_id))
job = await app['comment_scheduler'].spawn(delete_query)
return {'deleted': await job.wait()} return {'deleted': await job.wait()}
@ -75,6 +77,7 @@ METHODS = {
'get_channel_from_comment_id': handle_get_channel_from_comment_id, 'get_channel_from_comment_id': handle_get_channel_from_comment_id,
'create_comment': handle_create_comment, 'create_comment': handle_create_comment,
'delete_comment': handle_delete_comment, 'delete_comment': handle_delete_comment,
'abandon_comment': handle_delete_comment,
} }

View file

@ -3,7 +3,7 @@ import logging
import re import re
from json import JSONDecodeError from json import JSONDecodeError
from nacl.hash import sha256 import hashlib
import aiohttp import aiohttp
import ecdsa 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: async with aiohttp.request('POST', app['config']['LBRYNET'], json=resolve_body) as req:
try: try:
resp = await req.json() resp = await req.json()
return resp.get(lbry_url)
except JSONDecodeError as jde: except JSONDecodeError as jde:
logger.exception(jde.msg) 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): 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: if claim:
public_key = claim['value']['public_key'] public_key = claim['value']['public_key']
claim_hash = binascii.unhexlify(claim['claim_id'].encode())[::-1] claim_hash = binascii.unhexlify(claim['claim_id'].encode())[::-1]
pieces_injest = b''.join((comment_id.encode(), claim_hash))
return is_signature_valid( return is_signature_valid(
encoded_signature=get_encoded_signature(signature), 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()) public_key_bytes=binascii.unhexlify(public_key.encode())
) )
return False return False