From feaef1e463d71e3a95ee61648229080174413719 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 2 May 2019 02:03:37 -0300 Subject: [PATCH] webhooks: finish impl for webhook_avy_redir - migration.command: fix apply_migration call --- litecord/blueprints/icons.py | 5 ++++- litecord/blueprints/webhooks.py | 20 ++++++++++++-------- manage/cmd/migration/command.py | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/litecord/blueprints/icons.py b/litecord/blueprints/icons.py index e5f65b0..b01bac7 100644 --- a/litecord/blueprints/icons.py +++ b/litecord/blueprints/icons.py @@ -18,8 +18,11 @@ along with this program. If not, see . """ from os.path import splitext + from quart import Blueprint, current_app as app, send_file, redirect + from litecord.embed.sanitizer import make_md_req_url +from litecord.embed.schemas import EmbedURL bp = Blueprint('images', __name__) @@ -63,7 +66,7 @@ async def _get_default_user_avatar(default_id: int): async def _handle_webhook_avatar(md_url_redir: str): - md_url = make_md_req_url(None, 'img', md_url_redir) + md_url = make_md_req_url(app.config, 'img', EmbedURL(md_url_redir)) return redirect(md_url) diff --git a/litecord/blueprints/webhooks.py b/litecord/blueprints/webhooks.py index 308e01b..a86bae0 100644 --- a/litecord/blueprints/webhooks.py +++ b/litecord/blueprints/webhooks.py @@ -21,6 +21,7 @@ import secrets import hashlib from typing import Dict, Any, Optional +import asyncpg from quart import Blueprint, jsonify, current_app as app, request from litecord.auth import token_check @@ -348,19 +349,22 @@ async def create_message_webhook(guild_id, channel_id, webhook_id, data): return message_id -async def _webhook_avy_redir(webhook_id: int, avatar_url): - eu_avatar_url = EmbedURL.from_parsed(avatar_url) - url_hash = hashlib.sha256(eu_avatar_url.to_md_path).hexdigest() +async def _webhook_avy_redir(webhook_id: int, avatar_url: EmbedURL): + """Create a row on webhook_avatars.""" + url_hash = hashlib.sha256(avatar_url.to_md_path.encode()).hexdigest() - await app.db.execute(""" - INSERT INTO webhook_avatars (webhook_id, hash, md_url_redir) - VALUES ($1, $2, $3) - """, webhook_id, url_hash, proxify(eu_avatar_url)) + try: + await app.db.execute(""" + INSERT INTO webhook_avatars (webhook_id, hash, md_url_redir) + VALUES ($1, $2, $3) + """, webhook_id, url_hash, avatar_url.url) + except asyncpg.UniqueViolationError: + pass return url_hash -async def _create_avatar(webhook_id: int, avatar_url) -> str: +async def _create_avatar(webhook_id: int, avatar_url: EmbedURL) -> str: """Create an avatar for a webhook out of an avatar URL, given when executing the webhook. diff --git a/manage/cmd/migration/command.py b/manage/cmd/migration/command.py index e769b6c..1f1f91f 100644 --- a/manage/cmd/migration/command.py +++ b/manage/cmd/migration/command.py @@ -228,7 +228,7 @@ async def migrate_cmd(app, _args): migration = ctx.scripts.get(idx) print('applying', migration.id, migration.name) - # await apply_migration(app, migration) + await apply_migration(app, migration) def setup(subparser):