tangled
alpha
login
or
join now
sparrowtek.com
/
bskyKit
2
fork
atom
this repo has no description
2
fork
atom
overview
issues
pulls
pipelines
support failable decodable for chat
radmakr.com
3 days ago
f019b0bc
9a762578
+42
-2
1 changed file
expand all
collapse all
unified
split
Sources
bskyKit
Models
Chat.swift
+42
-2
Sources/bskyKit/Models/Chat.swift
···
1
1
import Foundation
2
2
3
3
+
// MARK: - Fault-Tolerant Decoding
4
4
+
5
5
+
private struct FailableDecodable<T: Decodable & Sendable>: Decodable, Sendable {
6
6
+
let value: T?
7
7
+
init(from decoder: Decoder) throws {
8
8
+
value = try? T(from: decoder)
9
9
+
}
10
10
+
}
11
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
16
+
17
17
+
enum CodingKeys: String, CodingKey {
18
18
+
case convos, cursor
19
19
+
}
20
20
+
21
21
+
public init(from decoder: Decoder) throws {
22
22
+
let container = try decoder.container(keyedBy: CodingKeys.self)
23
23
+
cursor = try container.decodeIfPresent(String.self, forKey: .cursor)
24
24
+
let failableConvos = try container.decode([FailableDecodable<ConvoView>].self, forKey: .convos)
25
25
+
convos = failableConvos.compactMap(\.value)
26
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
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
35
-
self = .message(try ChatMessageView(from: decoder))
56
56
+
if let message = try? ChatMessageView(from: decoder) {
57
57
+
self = .message(message)
58
58
+
} else {
59
59
+
self = .unknown
60
60
+
}
36
61
case "chat.bsky.convo.defs#deletedMessageView":
37
37
-
self = .deleted(try DeletedMessageView(from: decoder))
62
62
+
if let deleted = try? DeletedMessageView(from: decoder) {
63
63
+
self = .deleted(deleted)
64
64
+
} else {
65
65
+
self = .unknown
66
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
125
+
126
126
+
enum CodingKeys: String, CodingKey {
127
127
+
case messages, cursor
128
128
+
}
129
129
+
130
130
+
public init(from decoder: Decoder) throws {
131
131
+
let container = try decoder.container(keyedBy: CodingKeys.self)
132
132
+
cursor = try container.decodeIfPresent(String.self, forKey: .cursor)
133
133
+
let failableMessages = try container.decode([FailableDecodable<ChatMessageUnion>].self, forKey: .messages)
134
134
+
messages = failableMessages.compactMap(\.value)
135
135
+
}
96
136
}
97
137
98
138
/// Response from chat.bsky.convo.sendMessage