comment-server/src/handles.py

132 lines
4.5 KiB
Python
Raw Normal View History

2019-05-26 00:42:39 -04:00
# cython: language_level=3
2019-05-20 06:49:08 -04:00
import json
2019-05-21 07:54:52 -04:00
import logging
import asyncio
2019-05-20 06:49:08 -04:00
from aiohttp import web
2019-05-21 07:54:52 -04:00
from aiojobs.aiohttp import atomic
from asyncio import coroutine
2019-05-21 07:54:52 -04:00
from misc import clean_input_params, ERRORS
2019-05-21 15:13:08 -04:00
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
2019-05-21 15:13:08 -04:00
from src.database import obtain_connection
from src.database import delete_channel_comment_by_id
from src.writes import create_comment_or_error
from src.misc import is_authentic_delete_signal
2019-05-21 06:56:27 -04:00
logger = logging.getLogger(__name__)
2019-05-20 06:49:08 -04:00
# noinspection PyUnusedLocal
def ping(*args):
return 'pong'
2019-05-20 06:49:08 -04:00
def handle_get_channel_from_comment_id(app, kwargs: dict):
with obtain_connection(app['db_path']) as conn:
return get_channel_from_comment_id(conn, **kwargs)
2019-05-20 06:49:08 -04:00
def handle_get_comment_ids(app, kwargs):
2019-05-21 05:00:01 -04:00
with obtain_connection(app['db_path']) as conn:
return get_comment_ids(conn, **kwargs)
2019-05-20 06:49:08 -04:00
def handle_get_claim_comments(app, kwargs):
2019-05-21 05:00:01 -04:00
with obtain_connection(app['db_path']) as conn:
return get_claim_comments(conn, **kwargs)
2019-05-20 06:49:08 -04:00
def handle_get_comments_by_id(app, kwargs):
2019-05-21 05:00:01 -04:00
with obtain_connection(app['db_path']) as conn:
return get_comments_by_id(conn, **kwargs)
2019-05-20 06:49:08 -04:00
async def write_comment(app, comment):
return await coroutine(create_comment_or_error)(app['writer'], **comment)
2019-05-26 00:42:39 -04:00
async def handle_create_comment(app, params):
job = await app['comment_scheduler'].spawn(write_comment(app, params))
return await job.wait()
2019-05-26 00:42:39 -04:00
async def delete_comment_if_authorized(app, comment_id, channel_name, channel_id, signature):
authorized = await is_authentic_delete_signal(app, comment_id, channel_name, channel_id, signature)
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)
return {'deleted': await job.wait()}
async def handle_delete_comment(app, params):
return await delete_comment_if_authorized(app, **params)
2019-05-20 06:49:08 -04:00
METHODS = {
'ping': ping,
'get_claim_comments': handle_get_claim_comments,
'get_comment_ids': handle_get_comment_ids,
'get_comments_by_id': handle_get_comments_by_id,
'get_channel_from_comment_id': handle_get_channel_from_comment_id,
'create_comment': handle_create_comment,
'delete_comment': handle_delete_comment,
2019-05-20 06:49:08 -04:00
}
2019-05-21 05:00:01 -04:00
async def process_json(app, body: dict) -> dict:
2019-05-20 06:49:08 -04:00
response = {'jsonrpc': '2.0', 'id': body['id']}
if body['method'] in METHODS:
method = body['method']
params = body.get('params', {})
clean_input_params(params)
2019-05-20 06:49:08 -04:00
try:
2019-05-21 05:00:01 -04:00
if asyncio.iscoroutinefunction(METHODS[method]):
result = await METHODS[method](app, params)
2019-05-20 06:49:08 -04:00
else:
result = METHODS[method](app, params)
2019-05-20 06:49:08 -04:00
response['result'] = result
except TypeError as te:
2019-05-21 06:56:27 -04:00
logger.exception('Got TypeError: %s', te)
2019-05-20 06:49:08 -04:00
response['error'] = ERRORS['INVALID_PARAMS']
except ValueError as ve:
logger.exception('Got ValueError: %s', ve)
response['error'] = ERRORS['INVALID_PARAMS']
except Exception as e:
logger.exception('Got unknown exception: %s', e)
response['error'] = ERRORS['INTERNAL']
2019-05-20 06:49:08 -04:00
else:
response['error'] = ERRORS['METHOD_NOT_FOUND']
2019-05-20 06:49:08 -04:00
return response
2019-05-21 05:00:01 -04:00
@atomic
async def api_endpoint(request: web.Request):
2019-05-20 06:49:08 -04:00
try:
2019-05-21 06:56:27 -04:00
logger.info('Received POST request from %s', request.remote)
body = await request.json()
2019-05-20 06:49:08 -04:00
if type(body) is list or type(body) is dict:
if type(body) is list:
# for batching
2019-05-21 05:00:01 -04:00
return web.json_response(
[await process_json(request.app, part) for part in body]
)
2019-05-20 06:49:08 -04:00
else:
2019-05-21 05:00:01 -04:00
return web.json_response(await process_json(request.app, body))
2019-05-20 06:49:08 -04:00
else:
logger.warning('Got invalid request from %s: %s', request.remote, body)
return web.json_response({'error': ERRORS['INVALID_REQUEST']})
2019-05-20 06:49:08 -04:00
except json.decoder.JSONDecodeError as jde:
2019-05-21 06:56:27 -04:00
logger.exception('Received malformed JSON from %s: %s', request.remote, jde.msg)
logger.debug('Request headers: %s', request.headers)
2019-05-20 06:49:08 -04:00
return web.json_response({
'error': ERRORS['PARSE_ERROR']
2019-05-20 06:49:08 -04:00
})
except Exception as e:
logger.exception('Exception raised by request from %s: %s', request.remote, e)
return web.json_response({'error': ERRORS['INVALID_REQUEST']})