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 all 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
19 changes: 14 additions & 5 deletions api/src/processing/services/reddit.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -47,11 +48,19 @@ 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`);

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 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 +133,4 @@ export default async function(obj) {
audioFilename: `reddit_${id}_audio`,
filename: `reddit_${id}.mp4`
}
}
}