From d29fa2e1f33c6fac5e7e4fcb0216f941250bbc06 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 17 Mar 2019 17:35:51 -0300 Subject: [PATCH] webhooks: add webhook patch impl - schemas: add WEBHOOK_UPDATE --- litecord/blueprints/webhooks.py | 49 ++++++++++++++++++++++++++++++--- litecord/schemas.py | 12 ++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/litecord/blueprints/webhooks.py b/litecord/blueprints/webhooks.py index b3a573d..ead8eb2 100644 --- a/litecord/blueprints/webhooks.py +++ b/litecord/blueprints/webhooks.py @@ -27,7 +27,7 @@ from litecord.blueprints.checks import ( channel_check, channel_perm_check, guild_check, guild_perm_check ) -from litecord.schemas import validate, WEBHOOK_CREATE +from litecord.schemas import validate, WEBHOOK_CREATE, WEBHOOK_UPDATE from litecord.enums import ChannelType from litecord.snowflake import get_snowflake from litecord.utils import async_map @@ -181,14 +181,55 @@ async def get_tokened_webhook(webhook_id, webhook_token): return await jsonify(await get_webhook(webhook_id, secure=False)) +async def _update_webhook(webhook_id: int, j: dict): + if 'name' in j: + await app.db.execute(""" + UPDATE webhooks + SET name = $1 + WHERE id = $2 + """, j['name'], webhook_id) + + if 'channel_id' in j: + await app.db.execute(""" + UPDATE webhooks + SET channel_id = $1 + WHERE id = $2 + """, j['name'], webhook_id) + + if 'avatar' in j: + new_icon = await app.icons.update( + 'user', webhook_id, j['avatar'], always_icon=True, size=(128, 128) + ) + + await app.db.execute(""" + UPDATE webhooks + SET icon = $1 + WHERE id = $2 + """, new_icon.icon_hash, webhook_id) + + @bp.route('/webhooks/', methods=['PATCH']) -async def modify_webhook(webhook_id): - pass +async def modify_webhook(webhook_id: int): + """Patch a webhook.""" + await _webhook_check_fw(webhook_id) + j = validate(await request.get_json(), WEBHOOK_UPDATE) + + await _update_webhook(webhook_id, j) + return jsonify(await get_webhook(webhook_id)) @bp.route('/webhooks//', methods=['PATCH']) async def modify_webhook_tokened(webhook_id, webhook_token): - pass + """Modify a webhook, using its token.""" + await webhook_token_check(webhook_id, webhook_token) + + # forcefully pop() the channel id out of the schema + # instead of making another, for simplicity's sake + j = validate(await request.get_json(), + WEBHOOK_UPDATE.pop('channel_id')) + + await _update_webhook(webhook_id, j) + return jsonify(await get_webhook(webhook_id, secure=False)) @bp.route('/webhooks/', methods=['DELETE']) diff --git a/litecord/schemas.py b/litecord/schemas.py index f3dca79..10dedf1 100644 --- a/litecord/schemas.py +++ b/litecord/schemas.py @@ -685,3 +685,15 @@ WEBHOOK_CREATE = { }, 'avatar': {'type': 'b64_icon', 'required': False, 'nullable': False} } + +WEBHOOK_UPDATE = { + 'name': { + 'type': 'string', 'minlength': 2, 'maxlength': 32, + 'required': False + }, + + # TODO: check if its b64_icon or string since the client + # could pass an icon hash instead. + 'avatar': {'type': 'b64_icon', 'required': False, 'nullable': False}, + 'channel_id': {'coerce': int, 'required': False, 'nullable': False} +}