reimplements abandon method using ORM; no longer require channel_id param

This commit is contained in:
Oleg Silkin 2020-04-01 18:12:46 -04:00
parent 8f12d997ae
commit c852697c94
3 changed files with 50 additions and 6 deletions

View file

@ -113,9 +113,12 @@ def comment_list(claim_id: str = None, parent_id: str = None,
def get_comment(comment_id: str) -> dict: def get_comment(comment_id: str) -> dict:
return (comment_list(expressions=(Comment.comment_id == comment_id), page_size=1) try:
.get('items') comment = comment_list(expressions=(Comment.comment_id == comment_id), page_size=1).get('items').pop()
.pop()) except IndexError:
raise ValueError(f'Comment does not exist with id {comment_id}')
else:
return comment
def create_comment_id(comment: str, channel_id: str, timestamp: int): def create_comment_id(comment: str, channel_id: str, timestamp: int):

View file

@ -84,13 +84,17 @@ async def hide_comments(app, pieces: list) -> list:
# TODO: Amortize this process # TODO: Amortize this process
claims = {} claims = {}
comments_to_hide = [] comments_to_hide = []
# go through a list of dict objects
for p in pieces: for p in pieces:
# maps the comment_id from the piece to a claim_id
claim_id = comment_cids[p['comment_id']] claim_id = comment_cids[p['comment_id']]
# resolve the claim from its id
if claim_id not in claims: if claim_id not in claims:
claim = await get_claim_from_id(app, claim_id) claim = await get_claim_from_id(app, claim_id)
if claim: if claim:
claims[claim_id] = claim claims[claim_id] = claim
# get the claim's signing channel, then use it to validate the hidden comment
channel = claims[claim_id].get('signing_channel') channel = claims[claim_id].get('signing_channel')
if validate_signature_from_claim(channel, p['signature'], p['signing_ts'], p['comment_id']): if validate_signature_from_claim(channel, p['signature'], p['signing_ts'], p['comment_id']):
comments_to_hide.append(p) comments_to_hide.append(p)

View file

@ -5,6 +5,7 @@ import typing
from aiohttp import web from aiohttp import web
from aiojobs.aiohttp import atomic from aiojobs.aiohttp import atomic
from peewee import DoesNotExist
from src.server.validation import validate_signature_from_claim from src.server.validation import validate_signature_from_claim
from src.misc import clean_input_params, get_claim_from_id from src.misc import clean_input_params, get_claim_from_id
@ -110,9 +111,45 @@ def get_channel_from_comment_id(app, comment_id: str) -> dict:
return results['items'].pop() return results['items'].pop()
async def handle_abandon_comment(app, params): async def handle_abandon_comment(
# return {'abandoned': await abandon_comment(app, **params)} app: web.Application,
raise NotImplementedError comment_id: str,
signature: str,
signing_ts: str,
**kwargs,
) -> dict:
comment = get_comment(comment_id)
try:
channel = await get_claim_from_id(app, comment['channel_id'])
except DoesNotExist:
raise ValueError('Could not find a channel associated with the given comment')
else:
if not validate_signature_from_claim(channel, signature, signing_ts, comment_id):
raise ValueError('Abandon signature could not be validated')
with app['db'].atomic():
return {
'abandoned': delete_comment(comment_id)
}
async def handle_hide_comments(app: web.Application, pieces: list, hide: bool = True) -> dict:
# let's get all the distinct claim_ids from the list of comment_ids
pieces_by_id = {p['comment_id']: p for p in pieces}
comment_ids = list(pieces_by_id.keys())
comments = (Comment
.select(Comment.comment_id, Comment.claim_id)
.where(Comment.comment_id.in_(comment_ids))
.tuples())
# resolve the claims and map them to their corresponding comment_ids
claims = {}
for comment_id, claim_id in comments:
try:
# try and resolve the claim, if fails then we mark it as null
# and remove the associated comment from the pieces
if claim_id not in claims:
claims[claim_id] = await get_claim_from_id(app, claim_id)
async def handle_hide_comments(app, pieces: list = None, claim_id: str = None) -> dict: async def handle_hide_comments(app, pieces: list = None, claim_id: str = None) -> dict: