Skip to content

Commit 578b18b

Browse files
committed
fix: support mastodon alt text
1 parent ad7f7d9 commit 578b18b

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

libraries/nestjs-libraries/src/integrations/social/mastodon.provider.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,20 @@ export class MastodonProvider extends SocialAbstract implements SocialProvider {
121121
body: form,
122122
})
123123
).json();
124-
return media.id;
124+
return media;
125+
}
126+
127+
async updateMediaDescription(instanceUrl: string, mediaId: string, accessToken: string, description: string) {
128+
const form = new FormData();
129+
form.append('description', description);
130+
131+
await this.fetch(`${instanceUrl}/api/v1/media/${mediaId}`, {
132+
method: 'PUT',
133+
headers: {
134+
Authorization: `Bearer ${accessToken}`,
135+
},
136+
body: form,
137+
});
125138
}
126139

127140
async dynamicPost(
@@ -133,21 +146,38 @@ export class MastodonProvider extends SocialAbstract implements SocialProvider {
133146
let loadId = '';
134147
const ids = [] as string[];
135148
for (const getPost of postDetails) {
136-
const uploadFiles = await Promise.all(
149+
// Upload media files and get media objects
150+
const uploadedMedia = await Promise.all(
137151
getPost?.media?.map((media) =>
138152
this.uploadFile(url, media.path, accessToken)
139153
) || []
140154
);
141155

156+
// Update media descriptions (alt text) if provided
157+
if (getPost?.media) {
158+
await Promise.all(
159+
getPost.media.map(async (media, index) => {
160+
if (media.alt && uploadedMedia[index]) {
161+
await this.updateMediaDescription(
162+
url,
163+
uploadedMedia[index].id,
164+
accessToken,
165+
media.alt
166+
);
167+
}
168+
})
169+
);
170+
}
171+
142172
const form = new FormData();
143173
form.append('status', getPost.message);
144174
form.append('visibility', 'public');
145175
if (loadId) {
146176
form.append('in_reply_to_id', loadId);
147177
}
148-
if (uploadFiles.length) {
149-
for (const file of uploadFiles) {
150-
form.append('media_ids[]', file);
178+
if (uploadedMedia.length) {
179+
for (const media of uploadedMedia) {
180+
form.append('media_ids[]', media.id);
151181
}
152182
}
153183

0 commit comments

Comments
 (0)