fedimbed: support for misskey fallback

This commit is contained in:
Cynthia Foxwell 2022-12-31 12:47:29 -07:00
parent 1d541bf122
commit 9609b39e7f
1 changed files with 67 additions and 39 deletions

View File

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