fedimbed: revert, this broke entirely somehow
This commit is contained in:
parent
46cd1569d0
commit
693d2fda69
1 changed files with 8 additions and 34 deletions
|
@ -1,7 +1,6 @@
|
|||
const fs = require("node:fs");
|
||||
const httpSignature = require("@peertube/http-signature");
|
||||
const {XMLParser} = require("fast-xml-parser");
|
||||
const {ProxyAgent} = require("undici");
|
||||
|
||||
const events = require("#lib/events.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;
|
||||
|
||||
const PATH_REGEX = {
|
||||
truthsocial: /^\/@(.+?)\/posts\/(\d+)\/?/i,
|
||||
mastodon: /^\/@(.+?)\/([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}\/?/,
|
||||
|
@ -114,16 +112,6 @@ const numberFormatter = Intl.NumberFormat("en-US");
|
|||
|
||||
const domainCache = new Map();
|
||||
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) {
|
||||
const urlObj = new URL(url);
|
||||
|
@ -183,9 +171,8 @@ async function getCrawledData(url, color, platformName) {
|
|||
try {
|
||||
const page = await fetch(url, {
|
||||
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())
|
||||
.catch(() => {});
|
||||
|
@ -774,17 +761,15 @@ async function bluesky(msg, url, spoiler = false, minimal = false) {
|
|||
async function fetchPost(url, platform, forceMastoAPI = false) {
|
||||
let urlObj = new URL(url);
|
||||
let postData;
|
||||
const ffua = firefoxUseragent();
|
||||
|
||||
if (!forceMastoAPI) {
|
||||
let rawPostData;
|
||||
try {
|
||||
rawPostData = await signedFetch(url, {
|
||||
headers: {
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
|
||||
}).then((res) => res.text());
|
||||
} catch (err) {
|
||||
logger.error("fedimbed", `Failed to signed fetch "${url}", retrying unsigned: ${err}`);
|
||||
|
@ -793,10 +778,9 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
try {
|
||||
rawPostData = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
|
||||
}).then((res) => res.text());
|
||||
} catch (err) {
|
||||
logger.error("fedimbed", `Failed to fetch "${url}": ${err}`);
|
||||
|
@ -840,10 +824,7 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
let redirUrl;
|
||||
const options = {};
|
||||
const headers = {};
|
||||
if (urlObj.hostname === "truthsocial.com") {
|
||||
const postId = urlObj.pathname.match(PATH_REGEX.truthsocial)?.[2];
|
||||
redirUrl = urlObj.origin + "/api/v1/statuses/" + postId;
|
||||
} else if (PATH_REGEX.pleroma2.test(urlObj.pathname)) {
|
||||
if (PATH_REGEX.pleroma2.test(urlObj.pathname)) {
|
||||
redirUrl = url.replace("notice", "api/v1/statuses");
|
||||
} else if (PATH_REGEX.mastodon.test(urlObj.pathname)) {
|
||||
const postId = urlObj.pathname.match(PATH_REGEX.mastodon)?.[2];
|
||||
|
@ -886,9 +867,8 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
redirUrl,
|
||||
Object.assign(options, {
|
||||
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());
|
||||
} catch (err) {
|
||||
|
@ -900,9 +880,8 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
redirUrl,
|
||||
Object.assign(options, {
|
||||
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());
|
||||
} catch (err) {
|
||||
|
@ -939,28 +918,23 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
}
|
||||
|
||||
async function getStatsAS(post) {
|
||||
const urlObj = new URL(post);
|
||||
const ffua = firefoxUseragent();
|
||||
|
||||
// agony
|
||||
let replyCount = 0;
|
||||
if (post?.replies?.id)
|
||||
try {
|
||||
const selfReplies = await signedFetch(post.replies.id + "?page=true", {
|
||||
headers: {
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
|
||||
}).then((res) => res.json());
|
||||
replyCount += selfReplies?.items?.length ?? 0;
|
||||
|
||||
const otherReplies = await signedFetch(post.replies.id + "?page=true&only_other_accounts=true", {
|
||||
headers: {
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
dispatcher: urlObj.hostname === "truthsocial.com" ? proxyAgent : null,
|
||||
}).then((res) => res.json());
|
||||
replyCount += otherReplies?.items?.length ?? 0;
|
||||
} catch {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue