From eb5ac709ab3d98e8dcf37795fdef9ec068e87516 Mon Sep 17 00:00:00 2001 From: Aholicknight Date: Tue, 26 Nov 2024 08:55:42 -0600 Subject: [PATCH 1/4] api/reddit: add support for resolving short links in comments --- api/src/processing/services/reddit.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/api/src/processing/services/reddit.js b/api/src/processing/services/reddit.js index 701db2362..67f39db96 100644 --- a/api/src/processing/services/reddit.js +++ b/api/src/processing/services/reddit.js @@ -47,11 +47,24 @@ async function getAccessToken() { return access_token; } -export default async function(obj) { - let url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}.json`); +async function resolveShortLink(url) { + return fetch(url, { method: 'HEAD', redirect: 'manual' }) + .then(r => r.headers.get('location')) + .catch(() => null); +} - if (obj.user) { - url.pathname = `/user/${obj.user}/comments/${obj.id}.json`; +export default async function(obj) { + let url; + + if (obj.shortLink) { + const resolvedUrl = await resolveShortLink(obj.shortLink); + if (!resolvedUrl) return { error: "fetch.short_link" }; + url = new URL(resolvedUrl); + } else { + url = new URL(`https://www.reddit.com/r/${obj.sub}/comments/${obj.id}.json`); + if (obj.user) { + url.pathname = `/user/${obj.user}/comments/${obj.id}.json`; + } } const accessToken = await getAccessToken(); @@ -124,4 +137,4 @@ export default async function(obj) { audioFilename: `reddit_${id}_audio`, filename: `reddit_${id}.mp4` } -} +} \ No newline at end of file From 589e96eb4a601e5ed6a18246a0ffcd78ecc1ffc1 Mon Sep 17 00:00:00 2001 From: Aholicknight Date: Tue, 26 Nov 2024 09:00:34 -0600 Subject: [PATCH 2/4] fix await error on deepsource --- api/src/processing/services/reddit.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/src/processing/services/reddit.js b/api/src/processing/services/reddit.js index 67f39db96..4615331cf 100644 --- a/api/src/processing/services/reddit.js +++ b/api/src/processing/services/reddit.js @@ -48,9 +48,12 @@ async function getAccessToken() { } async function resolveShortLink(url) { - return fetch(url, { method: 'HEAD', redirect: 'manual' }) - .then(r => r.headers.get('location')) - .catch(() => null); + try { + const response = await fetch(url, { method: 'HEAD', redirect: 'manual' }); + return response.headers.get('location'); + } catch { + return null; + } } export default async function(obj) { From 41296b7413d1a8538b366decbcdee9047e4d4822 Mon Sep 17 00:00:00 2001 From: Aholicknight Date: Tue, 24 Dec 2024 13:24:48 -0600 Subject: [PATCH 3/4] api/reddit: refactor short link resolution to use utility function --- api/src/processing/services/reddit.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/api/src/processing/services/reddit.js b/api/src/processing/services/reddit.js index 4615331cf..614027779 100644 --- a/api/src/processing/services/reddit.js +++ b/api/src/processing/services/reddit.js @@ -1,4 +1,5 @@ import { genericUserAgent, env } from "../../config.js"; +import { getRedirectingURL } from "../../misc/utils.js"; import { getCookie, updateCookieValues } from "../cookie/manager.js"; async function getAccessToken() { @@ -48,12 +49,7 @@ async function getAccessToken() { } async function resolveShortLink(url) { - try { - const response = await fetch(url, { method: 'HEAD', redirect: 'manual' }); - return response.headers.get('location'); - } catch { - return null; - } + return await getRedirectingURL(url); } export default async function(obj) { From e775d4c5e705146aa7de923e78be366e84bc78d4 Mon Sep 17 00:00:00 2001 From: Aholicknight <7843719+Aholicknight@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:26:03 -0600 Subject: [PATCH 4/4] Remove resolveShortLink function apply suggestions --- api/src/processing/services/reddit.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/src/processing/services/reddit.js b/api/src/processing/services/reddit.js index 614027779..b5283d65d 100644 --- a/api/src/processing/services/reddit.js +++ b/api/src/processing/services/reddit.js @@ -48,15 +48,12 @@ async function getAccessToken() { return access_token; } -async function resolveShortLink(url) { - return await getRedirectingURL(url); -} export default async function(obj) { let url; if (obj.shortLink) { - const resolvedUrl = await resolveShortLink(obj.shortLink); + const resolvedUrl = await getRedirectingURL(obj.shortLink); if (!resolvedUrl) return { error: "fetch.short_link" }; url = new URL(resolvedUrl); } else {