fedimbed: basic video and audio file support

This commit is contained in:
Cynthia Foxwell 2022-12-06 10:39:24 -07:00
parent 680891f264
commit b8fab6ec4f

View file

@ -69,7 +69,9 @@ async function processUrl(msg, url) {
.replace("gotosocial", "GoToSocial") .replace("gotosocial", "GoToSocial")
.replace(/^(.)/, (_, c) => c.toUpperCase()); .replace(/^(.)/, (_, c) => c.toUpperCase());
const attachments = []; const images = [];
const videos = [];
const audios = [];
let content, cw, author, timestamp; let content, cw, author, timestamp;
// Fetch post // Fetch post
@ -172,7 +174,9 @@ async function processUrl(msg, url) {
postData2.content; postData2.content;
author = { author = {
name: postData2.account.display_name, name: postData2.account.display_name,
handle: postData2.account.fqn ?? `${postData2.account.username}@${urlObj.hostname}`, handle:
postData2.account.fqn ??
`${postData2.account.username}@${urlObj.hostname}`,
url: postData2.account.url, url: postData2.account.url,
avatar: postData2.account.avatar, avatar: postData2.account.avatar,
}; };
@ -190,10 +194,22 @@ async function processUrl(msg, url) {
cw = postData.summary; cw = postData.summary;
timestamp = postData.published; timestamp = postData.published;
for (const attachment of postData.attachment) { for (const attachment of postData.attachment) {
attachments.push({ if (attachment.mediaType.startsWith("video/")) {
url: attachment.url, videos.push({
desc: attachment.name, url: attachment.url,
}); desc: attachment.name,
});
} else if (attachment.mediaType.startsWith("image/")) {
images.push({
url: attachment.url,
desc: attachment.name,
});
} else if (attachment.mediaType.startsWith("audio/")) {
audios.push({
url: attachment.url,
desc: attachment.name,
});
}
} }
// Author data is not sent with the post with AS2 // Author data is not sent with the post with AS2
@ -240,7 +256,7 @@ async function processUrl(msg, url) {
let desc = ""; let desc = "";
let MAX_LENGTH = 3999; let MAX_LENGTH = 3999;
if (cw != "" && attachments.length == 0) { if (cw != "" && (images.length == 0 || videos.length == 0)) {
desc += "\u26a0 " + cw + "\n\n||" + content + "||"; desc += "\u26a0 " + cw + "\n\n||" + content + "||";
MAX_LENGTH -= 8 - cw.length; MAX_LENGTH -= 8 - cw.length;
} else { } else {
@ -270,26 +286,62 @@ async function processUrl(msg, url) {
}, },
fields: [], fields: [],
}; };
if (attachments.length > 0) { if (images.length > 0) {
if (attachments.length > 1) { if (images.length > 1) {
baseEmbed.fields.push({ baseEmbed.fields.push({
name: "Images", name: "Images",
value: attachments value: images
.map((attachment, index) => `[Image ${index + 1}](${attachment.url})`) .map((attachment, index) => `[Image ${index + 1}](${attachment.url})`)
.join(" | "), .join(" | "),
inline: true,
}); });
} else { } else {
baseEmbed.fields.push({ baseEmbed.fields.push({
name: "Image", name: "Image",
value: `[Click for image](${attachments[0].url})`, value: `[Click for image](${images[0].url})`,
inline: true,
});
}
}
if (videos.length > 0) {
if (videos.length > 1) {
baseEmbed.fields.push({
name: "Videos",
value: videos
.map((attachment, index) => `[Video ${index + 1}](${attachment.url})`)
.join(" | "),
inline: true,
});
} else {
baseEmbed.fields.push({
name: "Video",
value: `[Click for video](${videos[0].url})`,
inline: true,
});
}
}
if (audios.length > 0) {
if (audios.length > 1) {
baseEmbed.fields.push({
name: "Audios",
value: audios
.map((attachment, index) => `[Audio ${index + 1}](${attachment.url})`)
.join(" | "),
inline: true,
});
} else {
baseEmbed.fields.push({
name: "Audio",
value: `[Click for audio](${audios[0].url})`,
inline: true,
}); });
} }
} }
const embeds = []; const embeds = [];
if (attachments.length > 0) { if (images.length > 0) {
for (const attachment of attachments) { for (const attachment of images) {
const embed = Object.assign({}, baseEmbed); const embed = Object.assign({}, baseEmbed);
embed.image = { embed.image = {
url: attachment.url, url: attachment.url,