From 6a3dcca9ee8d43ac2f197b3333ced731f9cca09e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 26 Jan 2023 22:10:20 -0500 Subject: [PATCH] Workaround for local-only posts not being decodable on Akkoma See #332 --- .../Sources/Pachyderm/Model/Status.swift | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift b/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift index a5e65fb8..b247af62 100644 --- a/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift +++ b/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift @@ -44,6 +44,47 @@ public final class Status: StatusProtocol, Decodable { public var applicationName: String? { application?.name } + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(String.self, forKey: .id) + self.uri = try container.decode(String.self, forKey: .uri) + self.url = try container.decodeIfPresent(WebURL.self, forKey: .url) + self.account = try container.decode(Account.self, forKey: .account) + self.inReplyToID = try container.decodeIfPresent(String.self, forKey: .inReplyToID) + self.inReplyToAccountID = try container.decodeIfPresent(String.self, forKey: .inReplyToAccountID) + self.reblog = try container.decodeIfPresent(Status.self, forKey: .reblog) + self.content = try container.decode(String.self, forKey: .content) + self.createdAt = try container.decode(Date.self, forKey: .createdAt) + self.emojis = try container.decode([Emoji].self, forKey: .emojis) + self.reblogsCount = try container.decode(Int.self, forKey: .reblogsCount) + self.favouritesCount = try container.decode(Int.self, forKey: .favouritesCount) + self.reblogged = try container.decodeIfPresent(Bool.self, forKey: .reblogged) + self.favourited = try container.decodeIfPresent(Bool.self, forKey: .favourited) + self.muted = try container.decodeIfPresent(Bool.self, forKey: .muted) + self.sensitive = try container.decode(Bool.self, forKey: .sensitive) + self.spoilerText = try container.decode(String.self, forKey: .spoilerText) + if let visibility = try? container.decode(Status.Visibility.self, forKey: .visibility) { + self.visibility = visibility + self.localOnly = try container.decodeIfPresent(Bool.self, forKey: .localOnly) + } else if let s = try? container.decode(String.self, forKey: .visibility), + s == "local" { + // hacky workaround for #332, akkoma describes local posts with a separate visibility + self.visibility = .public + self.localOnly = true + } else { + throw DecodingError.dataCorruptedError(forKey: .visibility, in: container, debugDescription: "Could not decode visibility") + } + self.attachments = try container.decode([Attachment].self, forKey: .attachments) + self.mentions = try container.decode([Mention].self, forKey: .mentions) + self.hashtags = try container.decode([Hashtag].self, forKey: .hashtags) + self.application = try container.decodeIfPresent(Application.self, forKey: .application) + self.language = try container.decodeIfPresent(String.self, forKey: .language) + self.pinned = try container.decodeIfPresent(Bool.self, forKey: .pinned) + self.bookmarked = try container.decodeIfPresent(Bool.self, forKey: .bookmarked) + self.card = try container.decodeIfPresent(Card.self, forKey: .card) + self.poll = try container.decodeIfPresent(Poll.self, forKey: .poll) + } + public static func getContext(_ statusID: String) -> Request { return Request(method: .get, path: "/api/v1/statuses/\(statusID)/context") }