Skip to content

Commit 1960c98

Browse files
committed
fix: support mastodon alt text
1 parent 52caf3d commit 1960c98

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
@@ -124,7 +124,20 @@ export class MastodonProvider extends SocialAbstract implements SocialProvider {
124124
body: form,
125125
})
126126
).json();
127-
return media.id;
127+
return media;
128+
}
129+
130+
async updateMediaDescription(instanceUrl: string, mediaId: string, accessToken: string, description: string) {
131+
const form = new FormData();
132+
form.append('description', description);
133+
134+
await this.fetch(`${instanceUrl}/api/v1/media/${mediaId}`, {
135+
method: 'PUT',
136+
headers: {
137+
Authorization: `Bearer ${accessToken}`,
138+
},
139+
body: form,
140+
});
128141
}
129142

130143
async dynamicPost(
@@ -136,21 +149,38 @@ export class MastodonProvider extends SocialAbstract implements SocialProvider {
136149
let loadId = '';
137150
const ids = [] as string[];
138151
for (const getPost of postDetails) {
139-
const uploadFiles = await Promise.all(
152+
// Upload media files and get media objects
153+
const uploadedMedia = await Promise.all(
140154
getPost?.media?.map((media) =>
141155
this.uploadFile(url, media.path, accessToken)
142156
) || []
143157
);
144158

159+
// Update media descriptions (alt text) if provided
160+
if (getPost?.media) {
161+
await Promise.all(
162+
getPost.media.map(async (media, index) => {
163+
if (media.alt && uploadedMedia[index]) {
164+
await this.updateMediaDescription(
165+
url,
166+
uploadedMedia[index].id,
167+
accessToken,
168+
media.alt
169+
);
170+
}
171+
})
172+
);
173+
}
174+
145175
const form = new FormData();
146176
form.append('status', getPost.message);
147177
form.append('visibility', 'public');
148178
if (loadId) {
149179
form.append('in_reply_to_id', loadId);
150180
}
151-
if (uploadFiles.length) {
152-
for (const file of uploadFiles) {
153-
form.append('media_ids[]', file);
181+
if (uploadedMedia.length) {
182+
for (const media of uploadedMedia) {
183+
form.append('media_ids[]', media.id);
154184
}
155185
}
156186

0 commit comments

Comments
 (0)