Skip to content

Commit bd755ab

Browse files
davidweiclaude
andcommitted
Improve Mastodon image upload failure handling
When image uploads to Mastodon fail (e.g., due to API errors or timeouts), convert markdown image syntax ![alt](url) to a more readable "alt: url" format instead of leaving raw markdown code in the toot. This makes failed image uploads more user-friendly while still preserving the image URL for users to click and view. Co-Authored-By: Claudia Sonnet 4.5 <noreply@anthropic.com>
1 parent f320e38 commit bd755ab

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/HappyNotes.Services/MastodonTootService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ private async Task<Status> _EditLongTootAsPhotoAsync(
296296
}
297297

298298
var successfulUploads = new List<(int index, Attachment attachment)>();
299+
var failedIndexes = new HashSet<int>();
299300

300301
try
301302
{
@@ -315,6 +316,7 @@ private async Task<Status> _EditLongTootAsPhotoAsync(
315316
}
316317
else if (task.Status == TaskStatus.Faulted)
317318
{
319+
failedIndexes.Add(i);
318320
logger.LogError(task.Exception, $"Failed to upload image: {matches[i].ImgUrl}");
319321
}
320322
}
@@ -359,6 +361,23 @@ private async Task<Status> _EditLongTootAsPhotoAsync(
359361
);
360362
}
361363

364+
// Replace failed image uploads with "alt: url" format for readability
365+
foreach (var failedIndex in failedIndexes)
366+
{
367+
var img = matches[failedIndex];
368+
var altText = img.Alt;
369+
altText = altText.Equals("image") ? string.Empty : altText;
370+
371+
var fallbackText = !string.IsNullOrWhiteSpace(altText)
372+
? $"{altText}: {img.ImgUrl}"
373+
: img.ImgUrl;
374+
375+
markdownText = markdownText.Replace(
376+
img.Match,
377+
fallbackText
378+
);
379+
}
380+
362381
return (markdownText.RemoveImageReference(), mediaIds);
363382
}
364383

0 commit comments

Comments
 (0)