fedimbed: trim alt text
This commit is contained in:
		
							parent
							
								
									fc97aaf4d1
								
							
						
					
					
						commit
						6d82d5a95c
					
				
					 1 changed files with 23 additions and 17 deletions
				
			
		| 
						 | 
					@ -249,6 +249,13 @@ async function signedFetch(url, options) {
 | 
				
			||||||
  return await fetch(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://tenor.com/view/12766761978490409957
 | 
				
			||||||
// https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/unicode.ts
 | 
					// https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/unicode.ts
 | 
				
			||||||
const encoder = new TextEncoder();
 | 
					const encoder = new TextEncoder();
 | 
				
			||||||
| 
						 | 
					@ -396,7 +403,7 @@ async function blueskyQuoteEmbed(quote) {
 | 
				
			||||||
        images.push(
 | 
					        images.push(
 | 
				
			||||||
          ...embed.images.map((image) => ({
 | 
					          ...embed.images.map((image) => ({
 | 
				
			||||||
            media: {url: image.fullsize},
 | 
					            media: {url: image.fullsize},
 | 
				
			||||||
            description: image.alt.length > 0 ? image.alt : null,
 | 
					            description: trimAltText(image.alt),
 | 
				
			||||||
          }))
 | 
					          }))
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
| 
						 | 
					@ -406,7 +413,7 @@ async function blueskyQuoteEmbed(quote) {
 | 
				
			||||||
        const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint;
 | 
					        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}`;
 | 
					        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;
 | 
					        break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      case "app.bsky.embed.recordWithMedia#view": {
 | 
					      case "app.bsky.embed.recordWithMedia#view": {
 | 
				
			||||||
| 
						 | 
					@ -414,7 +421,7 @@ async function blueskyQuoteEmbed(quote) {
 | 
				
			||||||
          images.push(
 | 
					          images.push(
 | 
				
			||||||
            ...embed.media.images.map((image) => ({
 | 
					            ...embed.media.images.map((image) => ({
 | 
				
			||||||
              media: {url: image.fullsize},
 | 
					              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") {
 | 
					        } 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 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}`;
 | 
					          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;
 | 
					        break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					@ -542,7 +549,7 @@ async function bluesky(msg, url, spoiler = false) {
 | 
				
			||||||
        images.push(
 | 
					        images.push(
 | 
				
			||||||
          ...post.embed.images.map((image) => ({
 | 
					          ...post.embed.images.map((image) => ({
 | 
				
			||||||
            media: {url: image.fullsize},
 | 
					            media: {url: image.fullsize},
 | 
				
			||||||
            description: image.alt.length > 0 ? image.alt : null,
 | 
					            description: trimAltText(image.alt),
 | 
				
			||||||
          }))
 | 
					          }))
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
| 
						 | 
					@ -552,7 +559,7 @@ async function bluesky(msg, url, spoiler = false) {
 | 
				
			||||||
        const domain = lookup.service.find((service) => service.id === "#atproto_pds").serviceEndpoint;
 | 
					        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}`;
 | 
					        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;
 | 
					        break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      case "app.bsky.embed.record#view": {
 | 
					      case "app.bsky.embed.record#view": {
 | 
				
			||||||
| 
						 | 
					@ -574,7 +581,7 @@ async function bluesky(msg, url, spoiler = false) {
 | 
				
			||||||
          images.push(
 | 
					          images.push(
 | 
				
			||||||
            ...post.embed.media.images.map((image) => ({
 | 
					            ...post.embed.media.images.map((image) => ({
 | 
				
			||||||
              media: {url: image.fullsize},
 | 
					              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") {
 | 
					        } 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 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}`;
 | 
					          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") {
 | 
					        } else if (post.embed.media.$type === "app.bsky.embed.external#view") {
 | 
				
			||||||
          if (post.embed.media.external.uri.includes("tenor.com")) {
 | 
					          if (post.embed.media.external.uri.includes("tenor.com")) {
 | 
				
			||||||
            const url = new URL(post.embed.media.external.uri);
 | 
					            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) {
 | 
					      for (const attachment of attachments) {
 | 
				
			||||||
        const type = attachment.type?.toLowerCase();
 | 
					        const type = attachment.type?.toLowerCase();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const attDesc = attachment.description ?? attachment.comment;
 | 
					        const attDesc = trimAltText(attachment.description ?? attachment.comment);
 | 
				
			||||||
        if (type.startsWith("image")) {
 | 
					        if (type.startsWith("image")) {
 | 
				
			||||||
          images.push({
 | 
					          images.push({
 | 
				
			||||||
            media: {url: attachment.url},
 | 
					            media: {url: attachment.url},
 | 
				
			||||||
            description: attDesc?.length > 0 ? attDesc : null,
 | 
					            description: attDesc,
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
        } else if (type.startsWith("video")) {
 | 
					        } else if (type.startsWith("video")) {
 | 
				
			||||||
          videos.push({
 | 
					          videos.push({
 | 
				
			||||||
            media: {url: attachment.url},
 | 
					            media: {url: attachment.url},
 | 
				
			||||||
            description: attDesc?.length > 0 ? attDesc : null,
 | 
					            description: attDesc,
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
        } else if (type.startsWith("audio")) {
 | 
					        } else if (type.startsWith("audio")) {
 | 
				
			||||||
          audios.push({
 | 
					          audios.push({
 | 
				
			||||||
            url: attachment.url,
 | 
					            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")) {
 | 
					          if (type.startsWith("image") || mediaType.startsWith("image")) {
 | 
				
			||||||
            images.push({
 | 
					            images.push({
 | 
				
			||||||
              media: {url: attUrl},
 | 
					              media: {url: attUrl},
 | 
				
			||||||
              description: attDesc?.length > 0 ? attDesc : null,
 | 
					              description: attDesc,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
          } else if (type.startsWith("video") || mediaType.startsWith("video")) {
 | 
					          } else if (type.startsWith("video") || mediaType.startsWith("video")) {
 | 
				
			||||||
            videos.push({
 | 
					            videos.push({
 | 
				
			||||||
              media: {url: attUrl},
 | 
					              media: {url: attUrl},
 | 
				
			||||||
              description: attDesc?.length > 0 ? attDesc : null,
 | 
					              description: attDesc,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
          } else if (type.startsWith("audio") || mediaType.startsWith("audio")) {
 | 
					          } else if (type.startsWith("audio") || mediaType.startsWith("audio")) {
 | 
				
			||||||
            audios.push({
 | 
					            audios.push({
 | 
				
			||||||
              url: attUrl,
 | 
					              url: attUrl,
 | 
				
			||||||
              description: attDesc?.length > 0 ? attDesc : null,
 | 
					              description: attDesc,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
| 
						 | 
					@ -1481,7 +1488,6 @@ async function processUrl(msg, url, spoiler = false, command = false, inQuote =
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  for (const video of videos) {
 | 
					  for (const video of videos) {
 | 
				
			||||||
    if (video.description?.length === 0) video.description = null;
 | 
					 | 
				
			||||||
    container.components.push({
 | 
					    container.components.push({
 | 
				
			||||||
      type: 12,
 | 
					      type: 12,
 | 
				
			||||||
      items: [video],
 | 
					      items: [video],
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue