From 34d7302a7d1db91bd9cdb7c698b999d65ea99cbb Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Sep 2025 14:00:39 -0700 Subject: fix: Properly handle videos --- src/reddit.ts | 16 ++++++++++++++++ src/server.ts | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/reddit.ts b/src/reddit.ts index aaadcd2..6fe576e 100644 --- a/src/reddit.ts +++ b/src/reddit.ts @@ -13,6 +13,22 @@ export interface RedditPost { is_gallery?: boolean; over_18: boolean; link_flair_text?: string; + thumbnail?: string; + preview?: { + images: Array<{ + source: { + url: string; + width: number; + height: number; + }; + resolutions: Array<{ + url: string; + width: number; + height: number; + }>; + }>; + enabled: boolean; + }; media?: { reddit_video?: { fallback_url: string; diff --git a/src/server.ts b/src/server.ts index bb0c458..baeb607 100644 --- a/src/server.ts +++ b/src/server.ts @@ -58,7 +58,6 @@ interface DiscordEmbed { footer: { text: string; }; - video?: { url: string }; image?: { url: string }; } @@ -87,6 +86,18 @@ class JSONResponse extends Response { const router = AutoRouter(); +function decodeHtmlEntities(str: string): string { + return str + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(///g, '/') + .replace(/`/g, '`') + .replace(/=/g, '='); +} + function createPostEmbed(post: RedditPost): DiscordEmbed { const mediaUrl = post.media?.reddit_video?.fallback_url || @@ -125,13 +136,40 @@ function createPostEmbed(post: RedditPost): DiscordEmbed { }, }; - if (mediaUrl) { + if (mediaUrl) if (post.media?.reddit_video || post.secure_media?.reddit_video) { - embed.video = { url: mediaUrl }; + if (!description) description = ''; + + description += + '\n\n📹 **This post contains a video** - [Click here to view](' + + mediaUrl + + ')'; + embed.description = description; + + if (post.preview?.images?.[0]?.source?.url) { + const decodedURL = decodeHtmlEntities( + post.preview.images[0].source.url, + ); + + console.log('Using preview image:', decodedURL); + + embed.image = { url: decodedURL }; + } else if ( + post.thumbnail && + post.thumbnail !== 'self' && + post.thumbnail !== 'default' + ) { + const decodedThumbnail = decodeHtmlEntities(post.thumbnail); + + console.log('Using thumbnail:', decodedThumbnail); + + embed.image = { url: decodedThumbnail }; + } else { + console.log('No suitable thumbnail found for video post'); + } } else { embed.image = { url: mediaUrl }; } - } return embed; } -- cgit v1.2.3