Accept missing metadata on attachments

Akkoma/Pleroma (and Friendica until recently) aren't providing attachment meta information like width or height.

Because Mastodon app enforced those fields to be present, attachments would be filtered out.

This commit change the behaviour of Mastodon.Entity.Status.mastodonAttachments by allowing those values to be missing and use default values instead.
This commit is contained in:
Mary 2022-12-21 23:06:49 +01:00
parent fba444d82f
commit 7eebb62267
1 changed files with 21 additions and 7 deletions

View File

@ -51,14 +51,28 @@ extension Mastodon.Entity.Status {
guard let mediaAttachments = mediaAttachments else { return [] }
let attachments = mediaAttachments.compactMap { media -> MastodonAttachment? in
guard let kind = media.attachmentKind,
let meta = media.meta,
let original = meta.original,
let width = original.width, // audio has width/height
let height = original.height
guard let kind = media.attachmentKind
else { return nil }
let durationMS: Int? = original.duration.flatMap { Int($0 * 1000) }
let width: Int;
let height: Int;
let durationMS: Int?;
if let meta = media.meta,
let original = meta.original,
let originalWidth = original.width,
let originalHeight = original.height {
width = originalWidth; // audio has width/height
height = originalHeight;
durationMS = original.duration.flatMap { Int($0 * 1000) }
}
else {
// In case metadata field is missing, use default values.
width = 32;
height = 32;
durationMS = nil;
}
return MastodonAttachment(
id: media.id,
kind: kind,