Adds edit comment queries and handle

This commit is contained in:
Oleg Silkin 2019-12-31 00:41:35 -05:00
parent c856438d70
commit 2311fa00e6
4 changed files with 83 additions and 30 deletions

View file

@ -56,6 +56,7 @@ async def main():
i += 1 i += 1
return claims return claims
if __name__ == '__main__': if __name__ == '__main__':
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
claims = loop.run_until_complete(main()) claims = loop.run_until_complete(main())

View file

@ -218,6 +218,25 @@ def hide_comments_by_id(conn: sqlite3.Connection, comment_ids: list) -> bool:
return bool(curs.rowcount) return bool(curs.rowcount)
def edit_comment_by_id(conn: sqlite3.Connection, comment_id: str, comment: str,
signature: str, signing_ts: str) -> bool:
with conn:
curs = conn.execute(
"""
UPDATE COMMENT
SET Body = :comment, Signature = :signature, SigningTs = :signing_ts
WHERE CommentId = :comment_id
""",
{
'comment': comment,
'signature': signature,
'signing_ts': signing_ts,
'comment_id': comment_id
})
logger.info("updated comment with `comment_id`: %s", comment_id)
return bool(curs.rowcount)
class DatabaseWriter(object): class DatabaseWriter(object):
_writer = None _writer = None

View file

@ -2,14 +2,11 @@ import logging
import sqlite3 import sqlite3
from asyncio import coroutine from asyncio import coroutine
from src.database.queries import delete_comment_by_id, get_comments_by_id
from src.database.queries import get_claim_ids_from_comment_ids import src.database.queries as db
from src.database.queries import get_comment_or_none
from src.database.queries import hide_comments_by_id
from src.database.queries import insert_channel
from src.database.queries import insert_comment
from server.validation import is_valid_channel, is_valid_base_comment, is_valid_credential_input, \ from server.validation import is_valid_channel, is_valid_base_comment, is_valid_credential_input, \
validate_signature_from_claim validate_signature_from_claim
from server.validation import body_is_valid
from src.server.misc import get_claim_from_id from src.server.misc import get_claim_from_id
from server.external import send_notifications, send_notification from server.external import send_notifications, send_notification
@ -20,7 +17,7 @@ def create_comment_or_error(conn, comment, claim_id, channel_id=None, channel_na
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 or channel_name or signature or signing_ts:
insert_channel_or_error(conn, channel_name, channel_id) insert_channel_or_error(conn, channel_name, channel_id)
comment_id = insert_comment( comment_id = db.insert_comment(
conn=conn, conn=conn,
comment=comment, comment=comment,
claim_id=claim_id, claim_id=claim_id,
@ -29,13 +26,13 @@ def create_comment_or_error(conn, comment, claim_id, channel_id=None, channel_na
parent_id=parent_id, parent_id=parent_id,
signing_ts=signing_ts signing_ts=signing_ts
) )
return get_comment_or_none(conn, comment_id) return db.get_comment_or_none(conn, comment_id)
def insert_channel_or_error(conn: sqlite3.Connection, channel_name: str, channel_id: str): def insert_channel_or_error(conn: sqlite3.Connection, channel_name: str, channel_id: str):
try: try:
is_valid_channel(channel_id, channel_name) is_valid_channel(channel_id, channel_name)
insert_channel(conn, channel_name, channel_id) db.insert_channel(conn, channel_name, channel_id)
except AssertionError: except AssertionError:
logger.exception('Invalid channel values given') logger.exception('Invalid channel values given')
raise ValueError('Received invalid values for channel_id or channel_name') raise ValueError('Received invalid values for channel_id or channel_name')
@ -44,16 +41,20 @@ def insert_channel_or_error(conn: sqlite3.Connection, channel_name: str, channel
""" COROUTINE WRAPPERS """ """ COROUTINE WRAPPERS """
async def write_comment(app, params): # CREATE async def _create_comment(app, params): # CREATE
return await coroutine(create_comment_or_error)(app['writer'], **params) return await coroutine(create_comment_or_error)(app['writer'], **params)
async def hide_comments(app, comment_ids): # UPDATE async def _hide_comments(app, comment_ids): # UPDATE
return await coroutine(hide_comments_by_id)(app['writer'], comment_ids) return await coroutine(db.hide_comments_by_id)(app['writer'], comment_ids)
async def abandon_comment(app, comment_id): # DELETE async def _edit_comment(**params):
return await coroutine(delete_comment_by_id)(app['writer'], comment_id) return await coroutine(db.edit_comment_by_id)(**params)
async def _abandon_comment(app, comment_id): # DELETE
return await coroutine(db.delete_comment_by_id)(app['writer'], comment_id)
""" Core Functions called by request handlers """ """ Core Functions called by request handlers """
@ -61,7 +62,7 @@ async def abandon_comment(app, comment_id): # DELETE
async def create_comment(app, params): async def create_comment(app, params):
if is_valid_base_comment(**params) and is_valid_credential_input(**params): if is_valid_base_comment(**params) and is_valid_credential_input(**params):
job = await app['comment_scheduler'].spawn(write_comment(app, params)) job = await app['comment_scheduler'].spawn(_create_comment(app, params))
comment = await job.wait() comment = await job.wait()
if comment: if comment:
await app['webhooks'].spawn( await app['webhooks'].spawn(
@ -72,8 +73,8 @@ async def create_comment(app, params):
raise ValueError('base comment is invalid') raise ValueError('base comment is invalid')
async def hide_comments_where_authorized(app, pieces: list) -> list: async def hide_comments(app, pieces: list) -> list:
comment_cids = get_claim_ids_from_comment_ids( comment_cids = db.get_claim_ids_from_comment_ids(
conn=app['reader'], conn=app['reader'],
comment_ids=[p['comment_id'] for p in pieces] comment_ids=[p['comment_id'] for p in pieces]
) )
@ -89,10 +90,10 @@ async def hide_comments_where_authorized(app, pieces: list) -> list:
comments_to_hide.append(p) comments_to_hide.append(p)
comment_ids = [c['comment_id'] for c in comments_to_hide] comment_ids = [c['comment_id'] for c in comments_to_hide]
job = await app['comment_scheduler'].spawn(hide_comments(app, comment_ids)) job = await app['comment_scheduler'].spawn(_hide_comments(app, comment_ids))
await app['webhooks'].spawn( await app['webhooks'].spawn(
send_notifications( send_notifications(
app, 'UPDATE', get_comments_by_id(app['reader'], comment_ids) app, 'UPDATE', db.get_comments_by_id(app['reader'], comment_ids)
) )
) )
@ -100,18 +101,44 @@ async def hide_comments_where_authorized(app, pieces: list) -> list:
return comment_ids return comment_ids
async def edit_comment(app, comment_id, new_comment, channel_id, async def edit_comment(app, comment_id: str, new_comment: str, channel_id: str,
channel_name, new_signature, new_signing_ts): channel_name: str, signature: str, signing_ts: str):
if not(is_valid_credential_input(channel_id, channel_name, signature, signing_ts)
and body_is_valid(new_comment)):
logging.error('Invalid argument values, check input and try again')
return
pass comment = db.get_comment_or_none(app['reader'], comment_id)
if not(comment and 'channel_id' in comment and comment['channel_id'] == channel_id.lower()):
logging.error("comment doesnt exist")
return
channel = await get_claim_from_id(app, channel_id)
if not validate_signature_from_claim(channel, signature, signing_ts, new_comment):
logging.error("signature could not be validated")
return
job = await app['comment_scheduler'].spawn(_edit_comment(
conn=app['writer'],
comment_id=comment_id,
signature=signature,
signing_ts=signing_ts,
comment=new_comment
))
if await job.wait():
logging.info('comment successfully edited')
return db.get_comment_or_none(app['reader'], comment_id)
else:
logging.critical('comment could not be edited')
async def abandon_comment_if_authorized(app, comment_id, channel_id, signature, signing_ts, **kwargs): async def abandon_comment(app, comment_id, channel_id, signature, signing_ts, **kwargs):
channel = await get_claim_from_id(app, channel_id) channel = await get_claim_from_id(app, channel_id)
if not validate_signature_from_claim(channel, signature, signing_ts, comment_id): if not validate_signature_from_claim(channel, signature, signing_ts, comment_id):
return False return False
comment = get_comment_or_none(app['reader'], comment_id) comment = db.get_comment_or_none(app['reader'], comment_id)
job = await app['comment_scheduler'].spawn(abandon_comment(app, comment_id)) job = await app['comment_scheduler'].spawn(_abandon_comment(app, comment_id))
await app['webhooks'].spawn(send_notification(app, 'DELETE', comment)) await app['webhooks'].spawn(send_notification(app, 'DELETE', comment))
return await job.wait() return await job.wait()

View file

@ -9,8 +9,8 @@ from src.database.queries import get_channel_id_from_comment_id
from src.database.queries import get_claim_comments from src.database.queries import get_claim_comments
from src.database.queries import get_claim_hidden_comments from src.database.queries import get_claim_hidden_comments
from src.database.queries import get_comments_by_id, get_comment_ids from src.database.queries import get_comments_by_id, get_comment_ids
from src.database.writes import abandon_comment_if_authorized, create_comment from src.database.writes import abandon_comment, create_comment
from src.database.writes import hide_comments_where_authorized from src.database.writes import hide_comments
from src.server.misc import clean_input_params from src.server.misc import clean_input_params
from src.server.errors import make_error, report_error from src.server.errors import make_error, report_error
@ -43,11 +43,16 @@ def handle_get_claim_hidden_comments(app, kwargs):
async def handle_abandon_comment(app, params): async def handle_abandon_comment(app, params):
return {'abandoned': await abandon_comment_if_authorized(app, **params)} return {'abandoned': await abandon_comment(app, **params)}
async def handle_hide_comments(app, params): async def handle_hide_comments(app, params):
return {'hidden': await hide_comments_where_authorized(app, **params)} return {'hidden': await hide_comments(app, **params)}
async def handle_edit_comment(app, params):
if handle_edit_comment(app, **params):
pass
METHODS = { METHODS = {
@ -60,7 +65,8 @@ METHODS = {
'create_comment': create_comment, 'create_comment': create_comment,
'delete_comment': handle_abandon_comment, 'delete_comment': handle_abandon_comment,
'abandon_comment': handle_abandon_comment, 'abandon_comment': handle_abandon_comment,
'hide_comments': handle_hide_comments 'hide_comments': handle_hide_comments,
'edit_comment': handle_edit_comment
} }