From 2024c4bdf8811154fb4e09c1ccfee5ee76970780 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 25 Oct 2019 10:40:26 -0300 Subject: [PATCH] remove app parameters from embed functions --- litecord/blueprints/channel/messages.py | 11 ++--- litecord/embed/messages.py | 37 +++++++-------- litecord/embed/sanitizer.py | 60 +++++++------------------ 3 files changed, 39 insertions(+), 69 deletions(-) diff --git a/litecord/blueprints/channel/messages.py b/litecord/blueprints/channel/messages.py index 551508b..1a3d6b5 100644 --- a/litecord/blueprints/channel/messages.py +++ b/litecord/blueprints/channel/messages.py @@ -383,12 +383,8 @@ async def msg_add_attachment(message_id: int, channel_id: int, attachment_file) return attachment_id -async def _spawn_embed(app_, payload, **kwargs): - app_.sched.spawn( - process_url_embed( - app_.config, app_.storage, app_.dispatcher, app_.session, payload, **kwargs - ) - ) +async def _spawn_embed(payload, **kwargs): + app.sched.spawn(process_url_embed(payload, **kwargs)) @bp.route("//messages", methods=["POST"]) @@ -458,7 +454,7 @@ async def _create_message(channel_id): # spawn url processor for embedding of images perms = await get_permissions(user_id, channel_id) if perms.bits.embed_links: - await _spawn_embed(app, payload) + await _spawn_embed(payload) # update read state for the author await app.db.execute( @@ -536,7 +532,6 @@ async def edit_message(channel_id, message_id): perms = await get_permissions(user_id, channel_id) if perms.bits.embed_links: await _spawn_embed( - app, { "id": message_id, "channel_id": channel_id, diff --git a/litecord/embed/messages.py b/litecord/embed/messages.py index ce23ea4..bc240ea 100644 --- a/litecord/embed/messages.py +++ b/litecord/embed/messages.py @@ -22,6 +22,7 @@ import asyncio import urllib.parse from pathlib import Path +from quart import current_app as app from logbook import Logger from litecord.embed.sanitizer import proxify, fetch_metadata, fetch_embed @@ -33,10 +34,10 @@ log = Logger(__name__) MEDIA_EXTENSIONS = ("png", "jpg", "jpeg", "gif", "webm") -async def insert_media_meta(url, config, session): +async def insert_media_meta(url): """Insert media metadata as an embed.""" - img_proxy_url = proxify(url, config=config) - meta = await fetch_metadata(url, config=config, session=session) + img_proxy_url = proxify(url) + meta = await fetch_metadata(url) if meta is None: return @@ -56,19 +57,19 @@ async def insert_media_meta(url, config, session): } -async def msg_update_embeds(payload, new_embeds, storage, dispatcher): +async def msg_update_embeds(payload, new_embeds): """Update the message with the given embeds and dispatch a MESSAGE_UPDATE to users.""" message_id = int(payload["id"]) channel_id = int(payload["channel_id"]) - await storage.execute_with_json( + await app.storage.execute_with_json( """ - UPDATE messages - SET embeds = $1 - WHERE messages.id = $2 - """, + UPDATE messages + SET embeds = $1 + WHERE messages.id = $2 + """, new_embeds, message_id, ) @@ -85,7 +86,9 @@ async def msg_update_embeds(payload, new_embeds, storage, dispatcher): if "flags" in payload: update_payload["flags"] = payload["flags"] - await dispatcher.dispatch("channel", channel_id, "MESSAGE_UPDATE", update_payload) + await app.dispatcher.dispatch( + "channel", channel_id, "MESSAGE_UPDATE", update_payload + ) def is_media_url(url) -> bool: @@ -102,15 +105,13 @@ def is_media_url(url) -> bool: return extension in MEDIA_EXTENSIONS -async def insert_mp_embed(parsed, config, session): +async def insert_mp_embed(parsed): """Insert mediaproxy embed.""" - embed = await fetch_embed(parsed, config=config, session=session) + embed = await fetch_embed(parsed) return embed -async def process_url_embed( - config, storage, dispatcher, session, payload: dict, *, delay=0 -): +async def process_url_embed(payload: dict, *, delay=0): """Process URLs in a message and generate embeds based on that.""" await asyncio.sleep(delay) @@ -145,9 +146,9 @@ async def process_url_embed( url = EmbedURL(url) if is_media_url(url): - embed = await insert_media_meta(url, config, session) + embed = await insert_media_meta(url) else: - embed = await insert_mp_embed(url, config, session) + embed = await insert_mp_embed(url) if not embed: continue @@ -160,4 +161,4 @@ async def process_url_embed( log.debug("made {} embeds for mid {}", len(new_embeds), message_id) - await msg_update_embeds(payload, new_embeds, storage, dispatcher) + await msg_update_embeds(payload, new_embeds) diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index b14e436..14e8977 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -75,35 +75,24 @@ def path_exists(embed: Embed, components_in: Union[List[str], str]): return False -def _mk_cfg_sess(config, session) -> tuple: - """Return a tuple of (config, session).""" - if config is None: - config = app.config - - if session is None: - session = app.session - - return config, session - - -def _md_base(config) -> Optional[tuple]: +def _md_base() -> Optional[tuple]: """Return the protocol and base url for the mediaproxy.""" - md_base_url = config["MEDIA_PROXY"] + md_base_url = app.config["MEDIA_PROXY"] if md_base_url is None: return None - proto = "https" if config["IS_SSL"] else "http" + proto = "https" if app.config["IS_SSL"] else "http" return proto, md_base_url -def make_md_req_url(config, scope: str, url): - """Make a mediaproxy request URL given the config, scope, and the url +def make_md_req_url(scope: str, url): + """Make a mediaproxy request URL given the scope and the url to be proxied. When MEDIA_PROXY is None, however, returns the original URL. """ - base = _md_base(config) + base = _md_base() if base is None: return url.url if isinstance(url, EmbedURL) else url @@ -111,38 +100,25 @@ def make_md_req_url(config, scope: str, url): return f"{proto}://{base_url}/{scope}/{url.to_md_path}" -def proxify(url, *, config=None) -> str: +def proxify(url) -> str: """Return a mediaproxy url for the given EmbedURL. Returns an /img/ scope.""" - config, _sess = _mk_cfg_sess(config, False) - if isinstance(url, str): url = EmbedURL(url) - return make_md_req_url(config, "img", url) + return make_md_req_url("img", url) async def _md_client_req( - config, session, scope: str, url, *, ret_resp=False + scope: str, url, *, ret_resp=False ) -> Optional[Union[Tuple, Dict]]: """Makes a request to the mediaproxy. This has common code between all the main mediaproxy request functions to decrease code repetition. - Note that config and session exist because there are cases where the app - isn't retrievable (as those functions usually run in background tasks, - not in the app itself). - Parameters ---------- - config: dict-like - the app configuration, if None, this will get the global one from the - app instance. - session: aiohttp client session - the aiohttp ClientSession instance to use, if None, this will get - the global one from the app. - scope: str the scope of your request. one of 'meta', 'img', or 'embed' are available for the mediaproxy's API. @@ -155,14 +131,12 @@ async def _md_client_req( the raw bytes of the response, but by the time this function is returned, the response object is invalid and the socket is closed """ - config, session = _mk_cfg_sess(config, session) - if not isinstance(url, EmbedURL): url = EmbedURL(url) - request_url = make_md_req_url(config, scope, url) + request_url = make_md_req_url(scope, url) - async with session.get(request_url) as resp: + async with app.session.get(request_url) as resp: if resp.status == 200: if ret_resp: return resp, await resp.read() @@ -174,18 +148,18 @@ async def _md_client_req( return None -async def fetch_metadata(url, *, config=None, session=None) -> Optional[Dict]: +async def fetch_metadata(url) -> Optional[Dict]: """Fetch metadata for a url (image width, mime, etc).""" - return await _md_client_req(config, session, "meta", url) + return await _md_client_req("meta", url) -async def fetch_raw_img(url, *, config=None, session=None) -> Optional[tuple]: +async def fetch_raw_img(url) -> Optional[tuple]: """Fetch raw data for a url (the bytes given off, used to proxy images). Returns a tuple containing the response object and the raw bytes given by the website. """ - tup = await _md_client_req(config, session, "img", url, ret_resp=True) + tup = await _md_client_req("img", url, ret_resp=True) if not tup: return None @@ -193,13 +167,13 @@ async def fetch_raw_img(url, *, config=None, session=None) -> Optional[tuple]: return tup -async def fetch_embed(url, *, config=None, session=None) -> Dict[str, Any]: +async def fetch_embed(url) -> Dict[str, Any]: """Fetch an embed for a given webpage (an automatically generated embed by the mediaproxy, look over the project on how it generates embeds). Returns a discord embed object. """ - return await _md_client_req(config, session, "embed", url) + return await _md_client_req("embed", url) async def fill_embed(embed: Optional[Embed]) -> Optional[Embed]: