From a3f2f997a3ddfafdff19bd8911f7b610326c02d8 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Thu, 5 Jan 2023 17:12:21 +1100 Subject: Prettier --- src/api/util/utility/EmbedHandlers.ts | 164 +++++++++++++++++++++++----------- 1 file changed, 114 insertions(+), 50 deletions(-) (limited to 'src/api/util/utility') diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index dca264d0..2549937e 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -16,8 +16,13 @@ export const DEFAULT_FETCH_OPTIONS: any = { method: "GET", }; -export const getProxyUrl = (url: URL, width: number, height: number): string => { - const { resizeWidthMax, resizeHeightMax, imagorServerUrl } = Config.get().cdn; +export const getProxyUrl = ( + url: URL, + width: number, + height: number, +): string => { + const { resizeWidthMax, resizeHeightMax, imagorServerUrl } = + Config.get().cdn; const secret = Config.get().security.requestSignature; width = Math.min(width || 500, resizeWidthMax || width); height = Math.min(height || 500, resizeHeightMax || width); @@ -26,16 +31,20 @@ export const getProxyUrl = (url: URL, width: number, height: number): string => if (imagorServerUrl) { let path = `${width}x${height}/${url.host}${url.pathname}`; - const hash = crypto.createHmac('sha1', secret) + const hash = crypto + .createHmac("sha1", secret) .update(path) - .digest('base64') - .replace(/\+/g, '-').replace(/\//g, '_'); + .digest("base64") + .replace(/\+/g, "-") + .replace(/\//g, "_"); return `${imagorServerUrl}/${hash}/${path}`; } // TODO: Imagor documentation - console.log("Imagor has not been set up correctly. docs.fosscord.com/set/up/a/page/about/this"); + console.log( + "Imagor has not been set up correctly. docs.fosscord.com/set/up/a/page/about/this", + ); return ""; }; @@ -69,8 +78,7 @@ const doFetch = async (url: URL) => { ...DEFAULT_FETCH_OPTIONS, size: Config.get().limits.message.maxEmbedDownloadSize, }); - } - catch (e) { + } catch (e) { return null; } }; @@ -88,12 +96,10 @@ const genericImageHandler = async (url: URL): Promise => { width = result.width; height = result.height; image = url.href; - } - else if (type.headers.get("content-type")?.indexOf("video") !== -1) { + } else if (type.headers.get("content-type")?.indexOf("video") !== -1) { // TODO return null; - } - else { + } else { // have to download the page, unfortunately const response = await doFetch(url); if (!response) return null; @@ -113,13 +119,15 @@ const genericImageHandler = async (url: URL): Promise => { height: height, url: url.href, proxy_url: getProxyUrl(new URL(image), width, height), - } + }, }; }; -export const EmbedHandlers: { [key: string]: (url: URL) => Promise; } = { +export const EmbedHandlers: { + [key: string]: (url: URL) => Promise; +} = { // the url does not have a special handler - "default": async (url: URL) => { + default: async (url: URL) => { const type = await fetch(url, { ...DEFAULT_FETCH_OPTIONS, method: "HEAD", @@ -154,7 +162,13 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise Promise { return EmbedHandlers["www.twitter.com"](url); }, + "twitter.com": (url: URL) => { + return EmbedHandlers["www.twitter.com"](url); + }, "www.twitter.com": async (url: URL) => { const token = Config.get().external.twitter; if (!token) return null; - if (!url.href.includes("/status/")) return null; // TODO; - const id = url.pathname.split("/")[3]; // super bad lol + if (!url.href.includes("/status/")) return null; // TODO; + const id = url.pathname.split("/")[3]; // super bad lol if (!parseInt(id)) return null; - const endpointUrl = `https://api.twitter.com/2/tweets/${id}` + + const endpointUrl = + `https://api.twitter.com/2/tweets/${id}` + `?expansions=author_id,attachments.media_keys` + - `&media.fields=url,width,height` + - `&tweet.fields=created_at,public_metrics` + - `&user.fields=profile_image_url`; - + `&media.fields=url,width,height` + + `&tweet.fields=created_at,public_metrics` + + `&user.fields=profile_image_url`; const response = await fetch(endpointUrl, { ...DEFAULT_FETCH_OPTIONS, headers: { authorization: `Bearer ${token}`, - } + }, }); const json = await response.json(); if (json.errors) return null; @@ -196,7 +212,9 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise x.type == "photo") as any[]; // TODO: video + let media = json.includes.media?.filter( + (x: any) => x.type == "photo", + ) as any[]; // TODO: video const embed: Embed = { type: EmbedType.rich, @@ -205,19 +223,38 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise Promise Promise { return EmbedHandlers["www.pixiv.net"](url); }, + "pixiv.net": (url: URL) => { + return EmbedHandlers["www.pixiv.net"](url); + }, "www.pixiv.net": async (url: URL) => { const response = await doFetch(url); if (!response) return null; @@ -291,12 +336,18 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise Promise { return EmbedHandlers["www.reddit.com"](url); }, + "reddit.com": (url: URL) => { + return EmbedHandlers["www.reddit.com"](url); + }, "www.reddit.com": async (url: URL) => { const res = await EmbedHandlers["default"](url); return { ...res, color: 16777215, provider: { - name: "reddit" - } + name: "reddit", + }, }; }, - "youtube.com": (url: URL) => { return EmbedHandlers["www.youtube.com"](url); }, + "youtube.com": (url: URL) => { + return EmbedHandlers["www.youtube.com"](url); + }, "www.youtube.com": async (url: URL): Promise => { const response = await doFetch(url); if (!response) return null; @@ -358,7 +416,13 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise Promise => { + self: async (url: URL): Promise => { const result = await probe(url.href); return { @@ -385,7 +449,7 @@ export const EmbedHandlers: { [key: string]: (url: URL) => Promise