diff --git a/src/modules/fedimbed.js b/src/modules/fedimbed.js index d056f8a..e5dba2f 100644 --- a/src/modules/fedimbed.js +++ b/src/modules/fedimbed.js @@ -249,6 +249,13 @@ async function signedFetch(url, options) { return await fetch(url, options); } +function trimAltText(str) { + if (str == null) return null; + else if (str.length <= 0) return null; + else if (str.length > 1024) return str.substring(0, 1023) + "\u2026"; + else return str; +} + // https://tenor.com/view/12766761978490409957 // https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/unicode.ts const encoder = new TextEncoder(); @@ -396,7 +403,7 @@ async function blueskyQuoteEmbed(quote) { images.push( ...embed.images.map((image) => ({ media: {url: image.fullsize}, - description: image.alt.length > 0 ? image.alt : null, + description: trimAltText(image.alt), })) ); break; @@ -406,7 +413,7 @@ async function blueskyQuoteEmbed(quote) { const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${quote.author.did}&cid=${embed.cid}`; - videos.push({media: {url: videoUrl}, description: embed.alt?.length > 0 ? embed.alt : null}); + videos.push({media: {url: videoUrl}, description: trimAltText(embed.alt)}); break; } case "app.bsky.embed.recordWithMedia#view": { @@ -414,7 +421,7 @@ async function blueskyQuoteEmbed(quote) { images.push( ...embed.media.images.map((image) => ({ media: {url: image.fullsize}, - description: image.alt.length > 0 ? image.alt : null, + description: trimAltText(image.alt), })) ); } else if (embed.media.$type === "app.bsky.embed.video#view") { @@ -422,7 +429,7 @@ async function blueskyQuoteEmbed(quote) { const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${quote.author.did}&cid=${embed.media.cid}`; - videos.push({media: {url: videoUrl}, description: embed.alt?.length > 0 ? embed.alt : null}); + videos.push({media: {url: videoUrl}, description: trimAltText(embed.alt)}); } break; } @@ -542,7 +549,7 @@ async function bluesky(msg, url, spoiler = false) { images.push( ...post.embed.images.map((image) => ({ media: {url: image.fullsize}, - description: image.alt.length > 0 ? image.alt : null, + description: trimAltText(image.alt), })) ); break; @@ -552,7 +559,7 @@ async function bluesky(msg, url, spoiler = false) { const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${post.author.did}&cid=${post.embed.cid}`; - videos.push({media: {url: videoUrl}, description: post.embed.alt?.length > 0 ? post.embed.alt : null}); + videos.push({media: {url: videoUrl}, description: trimAltText(post.embed.alt)}); break; } case "app.bsky.embed.record#view": { @@ -574,7 +581,7 @@ async function bluesky(msg, url, spoiler = false) { images.push( ...post.embed.media.images.map((image) => ({ media: {url: image.fullsize}, - description: image.alt.length > 0 ? image.alt : null, + description: trimAltText(image.alt), })) ); } else if (post.embed.media.$type === "app.bsky.embed.video#view") { @@ -582,7 +589,7 @@ async function bluesky(msg, url, spoiler = false) { const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint; const videoUrl = `${domain}/xrpc/com.atproto.sync.getBlob?did=${post.author.did}&cid=${post.embed.media.cid}`; - videos.push({media: {url: videoUrl}, description: post.embed.alt?.length > 0 ? post.embed.alt : null}); + videos.push({media: {url: videoUrl}, description: trimAltText(post.embed.alt)}); } else if (post.embed.media.$type === "app.bsky.embed.external#view") { if (post.embed.media.external.uri.includes("tenor.com")) { const url = new URL(post.embed.media.external.uri); @@ -1071,21 +1078,21 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote = for (const attachment of attachments) { const type = attachment.type?.toLowerCase(); - const attDesc = attachment.description ?? attachment.comment; + const attDesc = trimAltText(attachment.description ?? attachment.comment); if (type.startsWith("image")) { images.push({ media: {url: attachment.url}, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } else if (type.startsWith("video")) { videos.push({ media: {url: attachment.url}, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } else if (type.startsWith("audio")) { audios.push({ url: attachment.url, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } } @@ -1197,21 +1204,21 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote = } } - const attDesc = attachment.name ?? attachment.description ?? attachment.comment; + const attDesc = trimAltText(attachment.name ?? attachment.description ?? attachment.comment); if (type.startsWith("image") || mediaType.startsWith("image")) { images.push({ media: {url: attUrl}, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } else if (type.startsWith("video") || mediaType.startsWith("video")) { videos.push({ media: {url: attUrl}, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } else if (type.startsWith("audio") || mediaType.startsWith("audio")) { audios.push({ url: attUrl, - description: attDesc?.length > 0 ? attDesc : null, + description: attDesc, }); } } else { @@ -1481,7 +1488,6 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote = }); } for (const video of videos) { - if (video.description?.length === 0) video.description = null; container.components.push({ type: 12, items: [video],