From dc01e9b251f1a7ad2798e48e41a567b2a67f9ad0 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Fri, 9 Aug 2019 00:51:56 -0400 Subject: [PATCH] Replace singular hide_comment function with batch hide_comments function --- src/database/writes.py | 48 +++++++++++++++++++++++------------------- src/server/handles.py | 8 +++---- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/database/writes.py b/src/database/writes.py index 3be5475..16d1f17 100644 --- a/src/database/writes.py +++ b/src/database/writes.py @@ -3,12 +3,12 @@ import sqlite3 from asyncio import coroutine +from src.database.queries import hide_comments_by_id from src.database.queries import delete_comment_by_id from src.database.queries import get_comment_or_none from src.database.queries import insert_comment from src.database.queries import insert_channel -from src.database.queries import get_channel_id_from_comment_id -from src.database.queries import hide_comment_by_id +from src.database.queries import get_claim_ids_from_comment_ids from src.server.misc import is_authentic_delete_signal from src.server.misc import request_lbrynet from src.server.misc import validate_signature_from_claim @@ -54,28 +54,32 @@ async def write_comment(app, params): return await coroutine(create_comment_or_error)(app['writer'], **params) -async def hide_comment(app, comment_id): - return await coroutine(hide_comment_by_id)(app['writer'], comment_id) +async def hide_comments(app, comment_ids): + return await coroutine(hide_comments_by_id)(app['writer'], comment_ids) + async def claim_search(app, **kwargs): return (await request_lbrynet(app, 'claim_search', **kwargs))['items'][0] -# comment_ids: [ -# { -# "comment_id": id, -# "signing_ts": signing_ts, -# "signature": signature -# }, -# ... -# ] -async def hide_comment_if_authorized(app, comment_id, signing_ts, signature): - channel = get_channel_id_from_comment_id(app['reader'], comment_id) - claim = await request_lbrynet(app, 'claim_search', claim_id=channel['channel_id']) - claim = claim['items'][0] - if not validate_signature_from_claim(claim, signature, signing_ts, comment_id): - raise ValueError('Invalid Signature') - job = await app['comment_scheduler'].spawn(hide_comment(app, comment_id)) - return { - 'hidden': await job.wait() - } +async def hide_comments_where_authorized(app, pieces: list): + comment_cids = get_claim_ids_from_comment_ids( + conn=app['reader'], + comment_ids=[p['comment_id'] for p in pieces] + ) + # TODO: Amortize this process + claims = {} + comments_to_hide = [] + for p in pieces: + claim_id = comment_cids[p['comment_id']] + if claim_id not in claims: + claims[claim_id] = await claim_search(app, claim_id=claim_id, no_totals=True) + channel = claims[claim_id].get('signing_channel') + if validate_signature_from_claim(channel, p['signature'], p['signing_ts'], p['comment_id']): + comments_to_hide.append(p['comment_id']) + + if comments_to_hide: + job = await app['comment_scheduler'].spawn(hide_comments(app, comments_to_hide)) + await job.wait() + + return {'hidden': comments_to_hide} diff --git a/src/server/handles.py b/src/server/handles.py index 5951960..1f6cc44 100644 --- a/src/server/handles.py +++ b/src/server/handles.py @@ -16,7 +16,7 @@ from src.server.misc import is_valid_credential_input from src.server.misc import make_error from src.database.writes import delete_comment_if_authorized from src.database.writes import write_comment -from src.database.writes import hide_comment_if_authorized +from src.database.writes import hide_comments_where_authorized logger = logging.getLogger(__name__) @@ -59,8 +59,8 @@ async def handle_delete_comment(app, params): return await delete_comment_if_authorized(app, **params) -async def handle_hide_comment(app, params): - return await hide_comment_if_authorized(app, **params) +async def handle_hide_comments(app, params): + return await hide_comments_where_authorized(app, **params) METHODS = { @@ -73,7 +73,7 @@ METHODS = { 'create_comment': handle_create_comment, 'delete_comment': handle_delete_comment, 'abandon_comment': handle_delete_comment, - 'hide_comment': handle_hide_comment, + 'hide_comments': handle_hide_comments }