this repo has no description

support failable decodable for chat

+42 -2
+42 -2
Sources/bskyKit/Models/Chat.swift
··· 1 1 import Foundation 2 2 3 + // MARK: - Fault-Tolerant Decoding 4 + 5 + private struct FailableDecodable<T: Decodable & Sendable>: Decodable, Sendable { 6 + let value: T? 7 + init(from decoder: Decoder) throws { 8 + value = try? T(from: decoder) 9 + } 10 + } 11 + 3 12 /// Response from chat.bsky.convo.listConvos 4 13 public struct ConvoListResponse: Codable, Sendable { 5 14 public let convos: [ConvoView] 6 15 public let cursor: String? 16 + 17 + enum CodingKeys: String, CodingKey { 18 + case convos, cursor 19 + } 20 + 21 + public init(from decoder: Decoder) throws { 22 + let container = try decoder.container(keyedBy: CodingKeys.self) 23 + cursor = try container.decodeIfPresent(String.self, forKey: .cursor) 24 + let failableConvos = try container.decode([FailableDecodable<ConvoView>].self, forKey: .convos) 25 + convos = failableConvos.compactMap(\.value) 26 + } 7 27 } 8 28 9 29 /// A conversation view ··· 14 34 public let lastMessage: ChatMessageUnion? 15 35 public let muted: Bool 16 36 public let unreadCount: Int 37 + public let status: String? 17 38 } 18 39 19 40 /// Union type for messages ··· 32 53 33 54 switch type { 34 55 case "chat.bsky.convo.defs#messageView": 35 - self = .message(try ChatMessageView(from: decoder)) 56 + if let message = try? ChatMessageView(from: decoder) { 57 + self = .message(message) 58 + } else { 59 + self = .unknown 60 + } 36 61 case "chat.bsky.convo.defs#deletedMessageView": 37 - self = .deleted(try DeletedMessageView(from: decoder)) 62 + if let deleted = try? DeletedMessageView(from: decoder) { 63 + self = .deleted(deleted) 64 + } else { 65 + self = .unknown 66 + } 38 67 default: 39 68 self = .unknown 40 69 } ··· 93 122 public struct ChatMessagesResponse: Codable, Sendable { 94 123 public let messages: [ChatMessageUnion] 95 124 public let cursor: String? 125 + 126 + enum CodingKeys: String, CodingKey { 127 + case messages, cursor 128 + } 129 + 130 + public init(from decoder: Decoder) throws { 131 + let container = try decoder.container(keyedBy: CodingKeys.self) 132 + cursor = try container.decodeIfPresent(String.self, forKey: .cursor) 133 + let failableMessages = try container.decode([FailableDecodable<ChatMessageUnion>].self, forKey: .messages) 134 + messages = failableMessages.compactMap(\.value) 135 + } 96 136 } 97 137 98 138 /// Response from chat.bsky.convo.sendMessage