mirror of https://gitlab.com/litecord/litecord.git
remove app parameters from embed functions
This commit is contained in:
parent
2780ca4175
commit
2024c4bdf8
|
|
@ -383,12 +383,8 @@ async def msg_add_attachment(message_id: int, channel_id: int, attachment_file)
|
||||||
return attachment_id
|
return attachment_id
|
||||||
|
|
||||||
|
|
||||||
async def _spawn_embed(app_, payload, **kwargs):
|
async def _spawn_embed(payload, **kwargs):
|
||||||
app_.sched.spawn(
|
app.sched.spawn(process_url_embed(payload, **kwargs))
|
||||||
process_url_embed(
|
|
||||||
app_.config, app_.storage, app_.dispatcher, app_.session, payload, **kwargs
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/<int:channel_id>/messages", methods=["POST"])
|
@bp.route("/<int:channel_id>/messages", methods=["POST"])
|
||||||
|
|
@ -458,7 +454,7 @@ async def _create_message(channel_id):
|
||||||
# spawn url processor for embedding of images
|
# spawn url processor for embedding of images
|
||||||
perms = await get_permissions(user_id, channel_id)
|
perms = await get_permissions(user_id, channel_id)
|
||||||
if perms.bits.embed_links:
|
if perms.bits.embed_links:
|
||||||
await _spawn_embed(app, payload)
|
await _spawn_embed(payload)
|
||||||
|
|
||||||
# update read state for the author
|
# update read state for the author
|
||||||
await app.db.execute(
|
await app.db.execute(
|
||||||
|
|
@ -536,7 +532,6 @@ async def edit_message(channel_id, message_id):
|
||||||
perms = await get_permissions(user_id, channel_id)
|
perms = await get_permissions(user_id, channel_id)
|
||||||
if perms.bits.embed_links:
|
if perms.bits.embed_links:
|
||||||
await _spawn_embed(
|
await _spawn_embed(
|
||||||
app,
|
|
||||||
{
|
{
|
||||||
"id": message_id,
|
"id": message_id,
|
||||||
"channel_id": channel_id,
|
"channel_id": channel_id,
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import asyncio
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from quart import current_app as app
|
||||||
from logbook import Logger
|
from logbook import Logger
|
||||||
|
|
||||||
from litecord.embed.sanitizer import proxify, fetch_metadata, fetch_embed
|
from litecord.embed.sanitizer import proxify, fetch_metadata, fetch_embed
|
||||||
|
|
@ -33,10 +34,10 @@ log = Logger(__name__)
|
||||||
MEDIA_EXTENSIONS = ("png", "jpg", "jpeg", "gif", "webm")
|
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."""
|
"""Insert media metadata as an embed."""
|
||||||
img_proxy_url = proxify(url, config=config)
|
img_proxy_url = proxify(url)
|
||||||
meta = await fetch_metadata(url, config=config, session=session)
|
meta = await fetch_metadata(url)
|
||||||
|
|
||||||
if meta is None:
|
if meta is None:
|
||||||
return
|
return
|
||||||
|
|
@ -56,14 +57,14 @@ 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
|
"""Update the message with the given embeds and dispatch a MESSAGE_UPDATE
|
||||||
to users."""
|
to users."""
|
||||||
|
|
||||||
message_id = int(payload["id"])
|
message_id = int(payload["id"])
|
||||||
channel_id = int(payload["channel_id"])
|
channel_id = int(payload["channel_id"])
|
||||||
|
|
||||||
await storage.execute_with_json(
|
await app.storage.execute_with_json(
|
||||||
"""
|
"""
|
||||||
UPDATE messages
|
UPDATE messages
|
||||||
SET embeds = $1
|
SET embeds = $1
|
||||||
|
|
@ -85,7 +86,9 @@ async def msg_update_embeds(payload, new_embeds, storage, dispatcher):
|
||||||
if "flags" in payload:
|
if "flags" in payload:
|
||||||
update_payload["flags"] = payload["flags"]
|
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:
|
def is_media_url(url) -> bool:
|
||||||
|
|
@ -102,15 +105,13 @@ def is_media_url(url) -> bool:
|
||||||
return extension in MEDIA_EXTENSIONS
|
return extension in MEDIA_EXTENSIONS
|
||||||
|
|
||||||
|
|
||||||
async def insert_mp_embed(parsed, config, session):
|
async def insert_mp_embed(parsed):
|
||||||
"""Insert mediaproxy embed."""
|
"""Insert mediaproxy embed."""
|
||||||
embed = await fetch_embed(parsed, config=config, session=session)
|
embed = await fetch_embed(parsed)
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
async def process_url_embed(
|
async def process_url_embed(payload: dict, *, delay=0):
|
||||||
config, storage, dispatcher, session, payload: dict, *, delay=0
|
|
||||||
):
|
|
||||||
"""Process URLs in a message and generate embeds based on that."""
|
"""Process URLs in a message and generate embeds based on that."""
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
|
|
||||||
|
|
@ -145,9 +146,9 @@ async def process_url_embed(
|
||||||
url = EmbedURL(url)
|
url = EmbedURL(url)
|
||||||
|
|
||||||
if is_media_url(url):
|
if is_media_url(url):
|
||||||
embed = await insert_media_meta(url, config, session)
|
embed = await insert_media_meta(url)
|
||||||
else:
|
else:
|
||||||
embed = await insert_mp_embed(url, config, session)
|
embed = await insert_mp_embed(url)
|
||||||
|
|
||||||
if not embed:
|
if not embed:
|
||||||
continue
|
continue
|
||||||
|
|
@ -160,4 +161,4 @@ async def process_url_embed(
|
||||||
|
|
||||||
log.debug("made {} embeds for mid {}", len(new_embeds), message_id)
|
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)
|
||||||
|
|
|
||||||
|
|
@ -75,35 +75,24 @@ def path_exists(embed: Embed, components_in: Union[List[str], str]):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _mk_cfg_sess(config, session) -> tuple:
|
def _md_base() -> Optional[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]:
|
|
||||||
"""Return the protocol and base url for the mediaproxy."""
|
"""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:
|
if md_base_url is None:
|
||||||
return 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
|
return proto, md_base_url
|
||||||
|
|
||||||
|
|
||||||
def make_md_req_url(config, scope: str, url):
|
def make_md_req_url(scope: str, url):
|
||||||
"""Make a mediaproxy request URL given the config, scope, and the url
|
"""Make a mediaproxy request URL given the scope and the url
|
||||||
to be proxied.
|
to be proxied.
|
||||||
|
|
||||||
When MEDIA_PROXY is None, however, returns the original URL.
|
When MEDIA_PROXY is None, however, returns the original URL.
|
||||||
"""
|
"""
|
||||||
base = _md_base(config)
|
base = _md_base()
|
||||||
if base is None:
|
if base is None:
|
||||||
return url.url if isinstance(url, EmbedURL) else url
|
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}"
|
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
|
"""Return a mediaproxy url for the given EmbedURL. Returns an
|
||||||
/img/ scope."""
|
/img/ scope."""
|
||||||
config, _sess = _mk_cfg_sess(config, False)
|
|
||||||
|
|
||||||
if isinstance(url, str):
|
if isinstance(url, str):
|
||||||
url = EmbedURL(url)
|
url = EmbedURL(url)
|
||||||
|
|
||||||
return make_md_req_url(config, "img", url)
|
return make_md_req_url("img", url)
|
||||||
|
|
||||||
|
|
||||||
async def _md_client_req(
|
async def _md_client_req(
|
||||||
config, session, scope: str, url, *, ret_resp=False
|
scope: str, url, *, ret_resp=False
|
||||||
) -> Optional[Union[Tuple, Dict]]:
|
) -> Optional[Union[Tuple, Dict]]:
|
||||||
"""Makes a request to the mediaproxy.
|
"""Makes a request to the mediaproxy.
|
||||||
|
|
||||||
This has common code between all the main mediaproxy request functions
|
This has common code between all the main mediaproxy request functions
|
||||||
to decrease code repetition.
|
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
|
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
|
scope: str
|
||||||
the scope of your request. one of 'meta', 'img', or 'embed' are
|
the scope of your request. one of 'meta', 'img', or 'embed' are
|
||||||
available for the mediaproxy's API.
|
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
|
the raw bytes of the response, but by the time this function is
|
||||||
returned, the response object is invalid and the socket is closed
|
returned, the response object is invalid and the socket is closed
|
||||||
"""
|
"""
|
||||||
config, session = _mk_cfg_sess(config, session)
|
|
||||||
|
|
||||||
if not isinstance(url, EmbedURL):
|
if not isinstance(url, EmbedURL):
|
||||||
url = EmbedURL(url)
|
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 resp.status == 200:
|
||||||
if ret_resp:
|
if ret_resp:
|
||||||
return resp, await resp.read()
|
return resp, await resp.read()
|
||||||
|
|
@ -174,18 +148,18 @@ async def _md_client_req(
|
||||||
return None
|
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)."""
|
"""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).
|
"""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
|
Returns a tuple containing the response object and the raw bytes given by
|
||||||
the website.
|
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:
|
if not tup:
|
||||||
return None
|
return None
|
||||||
|
|
@ -193,13 +167,13 @@ async def fetch_raw_img(url, *, config=None, session=None) -> Optional[tuple]:
|
||||||
return tup
|
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
|
"""Fetch an embed for a given webpage (an automatically generated embed
|
||||||
by the mediaproxy, look over the project on how it generates embeds).
|
by the mediaproxy, look over the project on how it generates embeds).
|
||||||
|
|
||||||
Returns a discord embed object.
|
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]:
|
async def fill_embed(embed: Optional[Embed]) -> Optional[Embed]:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue