diff options
Diffstat (limited to '')
-rw-r--r-- | src/api/util/utility/EmbedHandlers.ts | 88 |
1 files changed, 21 insertions, 67 deletions
diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index 0f1e88a5..78e56986 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -27,8 +27,7 @@ export const DEFAULT_FETCH_OPTIONS: RequestInit = { redirect: "follow", follow: 1, headers: { - "user-agent": - "Mozilla/5.0 (compatible; Spacebar/1.0; +https://github.com/spacebarchat/server)", + "user-agent": "Mozilla/5.0 (compatible; Spacebar/1.0; +https://github.com/spacebarchat/server)", }, // size: 1024 * 1024 * 5, // grabbed from config later compress: true, @@ -38,7 +37,7 @@ export const DEFAULT_FETCH_OPTIONS: RequestInit = { const makeEmbedImage = ( url: string | undefined, width: number | undefined, - height: number | undefined, + height: number | undefined ): Required<EmbedImage> | undefined => { if (!url || !width || !height) return undefined; return { @@ -51,13 +50,8 @@ const makeEmbedImage = ( let hasWarnedAboutImagor = false; -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); @@ -81,8 +75,8 @@ export const getProxyUrl = ( console.log( "[Embeds]", yellow( - "Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/", - ), + "Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/" + ) ); } @@ -157,11 +151,7 @@ const genericImageHandler = async (url: URL): Promise<Embed | null> => { const response = await doFetch(url); if (!response) return null; const metas = getMetaDescriptions(await response.text()); - image = makeEmbedImage( - metas.image || metas.image_fallback, - metas.width, - metas.height, - ); + image = makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height); } if (!image) return null; @@ -182,8 +172,7 @@ export const EmbedHandlers: { ...DEFAULT_FETCH_OPTIONS, method: "HEAD", }); - if (type.headers.get("content-type")?.indexOf("image") !== -1) - return await genericImageHandler(url); + if (type.headers.get("content-type")?.indexOf("image") !== -1) return await genericImageHandler(url); const response = await doFetch(url); if (!response) return null; @@ -275,9 +264,7 @@ export const EmbedHandlers: { const text = json.data.text; const created_at = new Date(json.data.created_at); const metrics = json.data.public_metrics; - const media = json.includes.media?.filter( - (x: { type: string }) => x.type == "photo", - ); + const media = json.includes.media?.filter((x: { type: string }) => x.type == "photo"); const embed: Embed = { type: EmbedType.rich, @@ -286,11 +273,7 @@ export const EmbedHandlers: { author: { url: `https://twitter.com/${author.username}`, name: `${author.name} (@${author.username})`, - proxy_icon_url: getProxyUrl( - new URL(author.profile_image_url), - 400, - 400, - ), + proxy_icon_url: getProxyUrl(new URL(author.profile_image_url), 400, 400), icon_url: author.profile_image_url, }, timestamp: created_at, @@ -310,14 +293,11 @@ export const EmbedHandlers: { footer: { text: "Twitter", proxy_icon_url: getProxyUrl( - new URL( - "https://abs.twimg.com/icons/apple-touch-icon-192x192.png", - ), - 192, + new URL("https://abs.twimg.com/icons/apple-touch-icon-192x192.png"), 192, + 192 ), - icon_url: - "https://abs.twimg.com/icons/apple-touch-icon-192x192.png", + icon_url: "https://abs.twimg.com/icons/apple-touch-icon-192x192.png", }, // Discord doesn't send this? // provider: { @@ -331,11 +311,7 @@ export const EmbedHandlers: { width: media[0].width, height: media[0].height, url: media[0].url, - proxy_url: getProxyUrl( - new URL(media[0].url), - media[0].width, - media[0].height, - ), + proxy_url: getProxyUrl(new URL(media[0].url), media[0].width, media[0].height), }; media.shift(); } @@ -388,11 +364,7 @@ export const EmbedHandlers: { type: EmbedType.image, title: metas.title, description: metas.description, - image: makeEmbedImage( - metas.image || metas.image_fallback, - metas.width, - metas.height, - ), + image: makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height), provider: { url: "https://pixiv.net", name: "Pixiv", @@ -404,17 +376,9 @@ export const EmbedHandlers: { const response = await doFetch(url); if (!response) return null; const metas = getMetaDescriptions(await response.text()); - const numReviews = metas.$("#review_summary_num_reviews").val() as - | string - | undefined; - const price = metas - .$(".game_purchase_price.price") - .data("price-final") as number | undefined; - const releaseDate = metas - .$(".release_date") - .find("div.date") - .text() - .trim(); + const numReviews = metas.$("#review_summary_num_reviews").val() as string | undefined; + const price = metas.$(".game_purchase_price.price").data("price-final") as number | undefined; + const releaseDate = metas.$(".release_date").find("div.date").text().trim(); const isReleased = new Date(releaseDate) < new Date(); const fields: Embed["fields"] = []; @@ -452,9 +416,7 @@ export const EmbedHandlers: { width: 460, height: 215, url: metas.image, - proxy_url: metas.image - ? getProxyUrl(new URL(metas.image), 460, 215) - : undefined, + proxy_url: metas.image ? getProxyUrl(new URL(metas.image), 460, 215) : undefined, }, provider: { url: "https://store.steampowered.com", @@ -485,19 +447,11 @@ export const EmbedHandlers: { const metas = getMetaDescriptions(await response.text()); return { - video: makeEmbedImage( - metas.youtube_embed, - metas.width, - metas.height, - ), + video: makeEmbedImage(metas.youtube_embed, metas.width, metas.height), url: url.href, type: metas.youtube_embed ? EmbedType.video : EmbedType.link, title: metas.title, - thumbnail: makeEmbedImage( - metas.image || metas.image_fallback, - metas.width, - metas.height, - ), + thumbnail: makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height), provider: { url: "https://www.youtube.com", name: "YouTube", |