Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/reddit: add support for resolving short links in comments #950

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions api/src/processing/services/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@ 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) {
try {
Aholicknight marked this conversation as resolved.
Show resolved Hide resolved
const response = await fetch(url, { method: 'HEAD', redirect: 'manual' });
return response.headers.get('location');
} catch {
return 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resolvedUrl = await resolveShortLink(obj.shortLink);
const resolvedUrl = await getRedirectingURL(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();
Expand Down Expand Up @@ -124,4 +140,4 @@ export default async function(obj) {
audioFilename: `reddit_${id}_audio`,
filename: `reddit_${id}.mp4`
}
}
}