fedimbed: support for misskey fallback

This commit is contained in:
Cynthia Foxwell 2022-12-31 12:47:29 -07:00
parent 1d541bf122
commit 9609b39e7f

View file

@ -117,6 +117,7 @@ async function processUrl(msg, url) {
}
let redirUrl;
const options = {};
if (PATH_REGEX.pleroma2.test(urlObj.pathname)) {
redirUrl = url.replace("notice", "api/v1/statuses");
} else if (PATH_REGEX.mastodon.test(urlObj.pathname)) {
@ -124,6 +125,11 @@ async function processUrl(msg, url) {
redirUrl = urlObj.origin + "/api/v1/statuses/" + postId;
} else if (PATH_REGEX.mastodon2.test(urlObj.pathname)) {
redirUrl = url.replace(/^\/(.+?)\/statuses/, "/api/v1/statuses");
} else if (PATH_REGEX.misskey.test(urlObj.pathname)) {
const noteId = url.match(/^\/notes\/([a-z0-9]+)\/?/)?.[1];
redirUrl = urlObj.origin + "/api/notes/show/";
options.method = "POST";
options.body = JSON.stringify({noteId});
} else {
logger.error(
"fedimbed",
@ -133,11 +139,17 @@ async function processUrl(msg, url) {
if (redirUrl) {
logger.verbose("fedimbed", `Redirecting "${url}" to "${redirUrl}"`);
const rawPostData2 = await fetch(redirUrl, {
headers: {
"User-Agent": FRIENDLY_USERAGENT,
},
})
const rawPostData2 = await fetch(
redirUrl,
Object.assign(
{
headers: {
"User-Agent": FRIENDLY_USERAGENT,
},
},
options
)
)
.then((res) => res.text())
.catch((err) => {
logger.error(
@ -167,48 +179,60 @@ async function processUrl(msg, url) {
`Bailing trying to re-embed "${url}", MastoAPI gave us error: ${postData2.error}`
);
} else {
cw = postData2.spoiler_warning ?? postData2.spoiler_text;
cw =
postData2.spoiler_warning ?? postData2.spoiler_text ?? postData2.cw;
content =
postData2.akkoma?.source?.content ??
postData2.pleroma?.content?.["text/plain"] ??
postData2.text ??
postData2.content;
author = {
name: postData2.account.display_name,
name: postData2.account?.display_name ?? postData2.user?.name,
handle:
postData2.account.fqn ??
`${postData2.account.username}@${urlObj.hostname}`,
url: postData2.account.url,
avatar: postData2.account.avatar,
`${postData2.account?.username ?? postData2.user?.username}@${
urlObj.hostname
}`,
url:
postData2.account?.url ??
`${urlObj.origin}/@${
postData2.account?.username ?? postData2.user?.username
}`,
avatar: postData2.account?.avatar ?? postData2.user?.avatarUrl,
};
timestamp = postData2.created_at;
for (const attachment of postData2.media_attachments) {
const fileType =
attachment.pleroma?.mime_type ??
attachment.type +
(url.match(/\.([a-z0-9]{3,4})$/)?.[0] ??
attachment.type == "image"
? "png"
: attachment.type == "video"
? "mp4"
: "mpeg");
if (attachment.type == "image") {
images.push({
url: attachment.url,
desc: attachment.description,
type: fileType,
});
} else if (attachment.type == "video") {
videos.push({
url: attachment.url,
desc: attachment.description,
type: fileType,
});
} else if (attachment.type == "audio") {
audios.push({
url: attachment.url,
desc: attachment.description,
type: fileType,
});
const attachments = postData2.media_attachments ?? postData2.files;
if (attachments) {
for (const attachment of attachments) {
const fileType =
attachment.pleroma?.mime_type ?? attachment.type.indexOf("/") > -1
? attachment.type
: attachment.type +
(url.match(/\.([a-z0-9]{3,4})$/)?.[0] ??
attachment.type == "image"
? "png"
: attachment.type == "video"
? "mp4"
: "mpeg");
if (attachment.type.startsWith("image")) {
images.push({
url: attachment.url,
desc: attachment.description ?? attachment.comment,
type: fileType,
});
} else if (attachment.type.startsWith("video")) {
videos.push({
url: attachment.url,
desc: attachment.description ?? attachment.comment,
type: fileType,
});
} else if (attachment.type.startsWith("audio")) {
audios.push({
url: attachment.url,
desc: attachment.description ?? attachment.comment,
type: fileType,
});
}
}
}
}
@ -450,7 +474,11 @@ async function processUrl(msg, url) {
.then((buf) => Buffer.from(buf));
files.push({
name: attachment.type.replace("/", ".").replace("mpeg", "mp3"),
name: attachment.type
.replace("/", ".")
.replace("mpeg", "mp3")
.replace("vnd.wave", "wav")
.replace("x-wav", "wav"),
contents: file,
});
}