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 3 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
22 changes: 17 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,22 @@ 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 await getRedirectingURL(url);
}
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
async function resolveShortLink(url) {
return await getRedirectingURL(url);
}


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