fedimbed: revert, this broke entirely somehow

This commit is contained in:
Cynthia Foxwell 2025-06-23 22:42:21 -06:00
parent 46cd1569d0
commit 693d2fda69
Signed by: Cynosphere
SSH key fingerprint: SHA256:H3SM8ufP/uxqLwKSH7xY89TDnbR9uOHzjLoBr0tlajk

View file

@ -1,7 +1,6 @@
const fs = require("node:fs"); const fs = require("node:fs");
const httpSignature = require("@peertube/http-signature"); const httpSignature = require("@peertube/http-signature");
const {XMLParser} = require("fast-xml-parser"); const {XMLParser} = require("fast-xml-parser");
const {ProxyAgent} = require("undici");
const events = require("#lib/events.js"); const events = require("#lib/events.js");
const logger = require("#lib/logger.js"); const logger = require("#lib/logger.js");
@ -22,7 +21,6 @@ const BSKY_POST_REGEX =
/^\/profile\/(did:plc:[a-z0-9]+|(did:web:)?[a-z0-9][a-z0-9.-]+[a-z0-9]*)\/post\/([a-z0-9]+)\/?$/i; /^\/profile\/(did:plc:[a-z0-9]+|(did:web:)?[a-z0-9][a-z0-9.-]+[a-z0-9]*)\/post\/([a-z0-9]+)\/?$/i;
const PATH_REGEX = { const PATH_REGEX = {
truthsocial: /^\/@(.+?)\/posts\/(\d+)\/?/i,
mastodon: /^\/@(.+?)\/([a-z0-9]+?)\/?/i, mastodon: /^\/@(.+?)\/([a-z0-9]+?)\/?/i,
mastodon2: /^\/(.+?)\/statuses\/([a-z0-9]+?)\/?/i, mastodon2: /^\/(.+?)\/statuses\/([a-z0-9]+?)\/?/i,
pleroma: /^\/objects\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/?/, pleroma: /^\/objects\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/?/,
@ -114,16 +112,6 @@ const numberFormatter = Intl.NumberFormat("en-US");
const domainCache = new Map(); const domainCache = new Map();
domainCache.set("cohost.org", "cohost"); // no nodeinfo domainCache.set("cohost.org", "cohost"); // no nodeinfo
domainCache.set("truthsocial.com", "Truth Social"); // no nodeinfo
const proxyAgent = new ProxyAgent(hf.config.proxy);
function firefoxUseragent() {
const now = Math.floor(Date.now() / 1000);
const version = Math.floor(124 + (now - 1710892800) / 2419200);
return `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${version}.0) Gecko/20100101 Firefox/${version}.0`;
}
async function resolvePlatform(url) { async function resolvePlatform(url) {
const urlObj = new URL(url); const urlObj = new URL(url);
@ -183,9 +171,8 @@ async function getCrawledData(url, color, platformName) {
try { try {
const page = await fetch(url, { const page = await fetch(url, {
headers: { headers: {
"User-Agent": urlObj.hostname === "truthsocial.com" ? firefoxUseragent() : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
}, },
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}) })
.then((res) => res.text()) .then((res) => res.text())
.catch(() => {}); .catch(() => {});
@ -774,17 +761,15 @@ async function bluesky(msg, url, spoiler = false, minimal = false) {
async function fetchPost(url, platform, forceMastoAPI = false) { async function fetchPost(url, platform, forceMastoAPI = false) {
let urlObj = new URL(url); let urlObj = new URL(url);
let postData; let postData;
const ffua = firefoxUseragent();
if (!forceMastoAPI) { if (!forceMastoAPI) {
let rawPostData; let rawPostData;
try { try {
rawPostData = await signedFetch(url, { rawPostData = await signedFetch(url, {
headers: { headers: {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json", Accept: "application/activity+json",
}, },
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}).then((res) => res.text()); }).then((res) => res.text());
} catch (err) { } catch (err) {
logger.error("fedimbed", `Failed to signed fetch "${url}", retrying unsigned: ${err}`); logger.error("fedimbed", `Failed to signed fetch "${url}", retrying unsigned: ${err}`);
@ -793,10 +778,9 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
try { try {
rawPostData = await fetch(url, { rawPostData = await fetch(url, {
headers: { headers: {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json", Accept: "application/activity+json",
}, },
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}).then((res) => res.text()); }).then((res) => res.text());
} catch (err) { } catch (err) {
logger.error("fedimbed", `Failed to fetch "${url}": ${err}`); logger.error("fedimbed", `Failed to fetch "${url}": ${err}`);
@ -840,10 +824,7 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
let redirUrl; let redirUrl;
const options = {}; const options = {};
const headers = {}; const headers = {};
if (urlObj.hostname === "truthsocial.com") { if (PATH_REGEX.pleroma2.test(urlObj.pathname)) {
const postId = urlObj.pathname.match(PATH_REGEX.truthsocial)?.[2];
redirUrl = urlObj.origin + "/api/v1/statuses/" + postId;
} else if (PATH_REGEX.pleroma2.test(urlObj.pathname)) {
redirUrl = url.replace("notice", "api/v1/statuses"); redirUrl = url.replace("notice", "api/v1/statuses");
} else if (PATH_REGEX.mastodon.test(urlObj.pathname)) { } else if (PATH_REGEX.mastodon.test(urlObj.pathname)) {
const postId = urlObj.pathname.match(PATH_REGEX.mastodon)?.[2]; const postId = urlObj.pathname.match(PATH_REGEX.mastodon)?.[2];
@ -886,9 +867,8 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
redirUrl, redirUrl,
Object.assign(options, { Object.assign(options, {
headers: Object.assign(headers, { headers: Object.assign(headers, {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
}), }),
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}) })
).then((res) => res.text()); ).then((res) => res.text());
} catch (err) { } catch (err) {
@ -900,9 +880,8 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
redirUrl, redirUrl,
Object.assign(options, { Object.assign(options, {
headers: Object.assign(headers, { headers: Object.assign(headers, {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
}), }),
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}) })
).then((res) => res.text()); ).then((res) => res.text());
} catch (err) { } catch (err) {
@ -939,28 +918,23 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
} }
async function getStatsAS(post) { async function getStatsAS(post) {
const urlObj = new URL(post);
const ffua = firefoxUseragent();
// agony // agony
let replyCount = 0; let replyCount = 0;
if (post?.replies?.id) if (post?.replies?.id)
try { try {
const selfReplies = await signedFetch(post.replies.id + "?page=true", { const selfReplies = await signedFetch(post.replies.id + "?page=true", {
headers: { headers: {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json", Accept: "application/activity+json",
}, },
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}).then((res) => res.json()); }).then((res) => res.json());
replyCount += selfReplies?.items?.length ?? 0; replyCount += selfReplies?.items?.length ?? 0;
const otherReplies = await signedFetch(post.replies.id + "?page=true&only_other_accounts=true", { const otherReplies = await signedFetch(post.replies.id + "?page=true&only_other_accounts=true", {
headers: { headers: {
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json", Accept: "application/activity+json",
}, },
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
}).then((res) => res.json()); }).then((res) => res.json());
replyCount += otherReplies?.items?.length ?? 0; replyCount += otherReplies?.items?.length ?? 0;
} catch { } catch {