Skip to content

Commit

Permalink
feat: add image alt text (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgrasset authored Oct 18, 2023
1 parent 933b0fe commit cdfd585
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/services/bluesky-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
}

// Medias
const mediaAttachments: ComAtprotoRepoUploadBlob.Response[] = [];
const mediaAttachments: Array<ComAtprotoRepoUploadBlob.Response &{ alt_text: string | undefined}> = [];
for (const media of medias) {
if (!media.url) {
continue;
Expand All @@ -44,8 +44,10 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues

await client.uploadBlob(blobData, { encoding: mimeType })
.then(async mediaSent => {
mediaAttachments.push(mediaSent);

mediaAttachments.push({
...mediaSent,
...{ alt_text: media.type === 'image' ? media.alt_text : '' }
});
})
.catch(err => {
log.fail(err);
Expand All @@ -70,7 +72,7 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
$type: 'app.bsky.embed.recordWithMedia',
media: {
$type: 'app.bsky.embed.images',
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: '', image: i.data.blob.original })) : undefined
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: i.alt_text, image: i.data.blob.original })) : undefined
},
record: {
$type: 'app.bsky.embed.record',
Expand Down Expand Up @@ -105,7 +107,7 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
if (mediaAttachments.length) {
data.embed = {
$type: 'app.bsky.embed.images',
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: '', image: i.data.blob.original })) : undefined
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: i.alt_text, image: i.data.blob.original })) : undefined
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/mastodon-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export const mastodonSenderService = async (client: mastodon.rest.Client | null,
(media.type === 'image' && mediaAttachments.length < BLUESKY_MEDIA_IMAGES_MAX_COUNT - 1) ||
(media.type === 'video' && mediaAttachments.length === 0)
) {

// Download
log.text = `medias: ↓ (${mediaAttachments.length + 1}/${medias.length}) downloading`;
const mediaBlob = await mediaDownloaderService(media.url);

// Upload
log.text = `medias: ↑ (${mediaAttachments.length + 1}/${medias.length}) uploading`;
await client.v2.media.create({
file: mediaBlob
file: mediaBlob,
description: media.type === 'image' && media.alt_text ? media.alt_text : null
})
.then(async mediaSent => {
mediaAttachments.push(mediaSent);
Expand Down

0 comments on commit cdfd585

Please sign in to comment.