webhooks: finish impl for webhook_avy_redir

- migration.command: fix apply_migration call
This commit is contained in:
Luna 2019-05-02 02:03:37 -03:00
parent ecfe4fa457
commit feaef1e463
3 changed files with 17 additions and 10 deletions

View File

@ -18,8 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from os.path import splitext from os.path import splitext
from quart import Blueprint, current_app as app, send_file, redirect from quart import Blueprint, current_app as app, send_file, redirect
from litecord.embed.sanitizer import make_md_req_url from litecord.embed.sanitizer import make_md_req_url
from litecord.embed.schemas import EmbedURL
bp = Blueprint('images', __name__) 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): 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) return redirect(md_url)

View File

@ -21,6 +21,7 @@ import secrets
import hashlib import hashlib
from typing import Dict, Any, Optional from typing import Dict, Any, Optional
import asyncpg
from quart import Blueprint, jsonify, current_app as app, request from quart import Blueprint, jsonify, current_app as app, request
from litecord.auth import token_check 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 return message_id
async def _webhook_avy_redir(webhook_id: int, avatar_url): async def _webhook_avy_redir(webhook_id: int, avatar_url: EmbedURL):
eu_avatar_url = EmbedURL.from_parsed(avatar_url) """Create a row on webhook_avatars."""
url_hash = hashlib.sha256(eu_avatar_url.to_md_path).hexdigest() url_hash = hashlib.sha256(avatar_url.to_md_path.encode()).hexdigest()
try:
await app.db.execute(""" await app.db.execute("""
INSERT INTO webhook_avatars (webhook_id, hash, md_url_redir) INSERT INTO webhook_avatars (webhook_id, hash, md_url_redir)
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
""", webhook_id, url_hash, proxify(eu_avatar_url)) """, webhook_id, url_hash, avatar_url.url)
except asyncpg.UniqueViolationError:
pass
return url_hash 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, """Create an avatar for a webhook out of an avatar URL,
given when executing the webhook. given when executing the webhook.

View File

@ -228,7 +228,7 @@ async def migrate_cmd(app, _args):
migration = ctx.scripts.get(idx) migration = ctx.scripts.get(idx)
print('applying', migration.id, migration.name) print('applying', migration.id, migration.name)
# await apply_migration(app, migration) await apply_migration(app, migration)
def setup(subparser): def setup(subparser):