fedimbed: fallback to unsigned if signed fails

temp workaround for firefish
This commit is contained in:
Cynthia Foxwell 2023-07-27 21:12:14 -06:00
parent 967a7f7bc8
commit 5d8153588f

View file

@ -115,19 +115,32 @@ async function processUrl(msg, url, spoiler = false) {
let content, cw, author, timestamp; let content, cw, author, timestamp;
// Fetch post // Fetch post
const rawPostData = await signedFetch(url, { let rawPostData = await signedFetch(url, {
headers: { headers: {
"User-Agent": FRIENDLY_USERAGENT, "User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json", Accept: "application/activity+json",
}, },
}) })
.then((res) => res.text()) .then((res) => res.text())
.catch((err) => { .catch(async (err) => {
logger.error("fedimbed", `Failed to fetch "${url}" as AS2: ${err}`); logger.error(
"fedimbed",
`Failed to signed fetch "${url}", retrying unsigned: ${err}`
);
rawPostData = await fetch(url, {
headers: {
"User-Agent": FRIENDLY_USERAGENT,
Accept: "application/activity+json",
},
})
.then((res) => res.text())
.catch((err) => {
logger.error("fedimbed", `Failed to fetch "${url}": ${err}`);
});
}); });
let postData; let postData;
if (rawPostData.startsWith("{")) { if (rawPostData?.startsWith("{")) {
postData = JSON.parse(rawPostData); postData = JSON.parse(rawPostData);
} else { } else {
logger.warn("fedimbed", `Got non-JSON for "${url}": ${rawPostData}`); logger.warn("fedimbed", `Got non-JSON for "${url}": ${rawPostData}`);
@ -194,7 +207,7 @@ async function processUrl(msg, url, spoiler = false) {
options options
)}, ${JSON.stringify(headers)}` )}, ${JSON.stringify(headers)}`
); );
const rawPostData2 = await signedFetch( let rawPostData2 = await signedFetch(
redirUrl, redirUrl,
Object.assign(options, { Object.assign(options, {
headers: Object.assign(headers, { headers: Object.assign(headers, {
@ -203,27 +216,42 @@ async function processUrl(msg, url, spoiler = false) {
}) })
) )
.then((res) => res.text()) .then((res) => res.text())
.catch((err) => { .catch(async (err) => {
logger.error( logger.error(
"fedimbed", "fedimbed",
`Failed to fetch "${url}" as MastoAPI: ${err}` `Failed to signed fetch "${url}" via MastoAPI, retrying unsigned: ${err}`
); );
rawPostData2 = await signedFetch(
redirUrl,
Object.assign(options, {
headers: Object.assign(headers, {
"User-Agent": FRIENDLY_USERAGENT,
}),
})
)
.then((res) => res.text())
.catch((err) => {
logger.error(
"fedimbed",
`Failed to fetch "${url}" via MastoAPI: ${err}`
);
});
}); });
let postData2; let postData2;
if (rawPostData2.startsWith("{")) { if (rawPostData2?.startsWith("{")) {
postData2 = JSON.parse(rawPostData2); postData2 = JSON.parse(rawPostData2);
} else { } else {
logger.warn( logger.warn(
"fedimbed", "fedimbed",
`Got non-JSON for "${url}" as MastoAPI: ${rawPostData2}` `Got non-JSON for "${url}" via MastoAPI: ${rawPostData2}`
); );
} }
if (!postData2) { if (!postData2) {
logger.warn( logger.warn(
"fedimbed", "fedimbed",
`Bailing trying to re-embed "${url}": Failed to get post from both AS2 and MastoAPI.` `Bailing trying to re-embed "${url}": Failed to get post from normal and MastoAPI.`
); );
} else if (postData2.error) { } else if (postData2.error) {
logger.error( logger.error(