-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathChannelListPayload.swift
More file actions
396 lines (345 loc) 路 17 KB
/
Copy pathChannelListPayload.swift
File metadata and controls
396 lines (345 loc) 路 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// Copyright 漏 2026 Stream.io Inc. All rights reserved.
//
import Foundation
struct ChannelListPayload {
/// A list of channels response (see `ChannelQuery`).
let channels: [ChannelPayload]
/// Server-resolved predefined filter, present only when the query was made with a predefined filter.
let predefinedFilter: PredefinedFilterPayload?
init(channels: [ChannelPayload], predefinedFilter: PredefinedFilterPayload? = nil) {
self.channels = channels
self.predefinedFilter = predefinedFilter
}
}
extension ChannelListPayload: Decodable {
enum CodingKeys: String, CodingKey {
case channels
case predefinedFilter = "predefined_filter"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let channels = try container
.decodeArrayIgnoringFailures([ChannelPayload].self, forKey: .channels)
let predefinedFilter = try container
.decodeIfPresent(PredefinedFilterPayload.self, forKey: .predefinedFilter)
self.init(
channels: channels,
predefinedFilter: predefinedFilter
)
}
}
final class PredefinedFilterPayload: Decodable, Sendable {
let name: String
let filter: [String: RawJSON]
let sort: [[String: RawJSON]]
init(name: String, filter: [String: RawJSON], sort: [[String: RawJSON]]) {
self.name = name
self.filter = filter
self.sort = sort
}
}
struct ChannelPayload {
let channel: ChannelDetailPayload
let watcherCount: Int?
let watchers: [UserPayload]?
let members: [MemberPayload]
let membership: MemberPayload?
let messages: [MessagePayload]
let pendingMessages: [MessagePayload]?
let pinnedMessages: [MessagePayload]
let channelReads: [ChannelReadPayload]
let isHidden: Bool?
let draft: DraftPayload?
let activeLiveLocations: [SharedLocationPayload]
let pushPreference: PushPreferencePayload?
}
extension ChannelPayload {
/// Returns the newest message from `messages` in O(1) assuming messages are sorted by `createdAt`.
var newestMessage: MessagePayload? {
guard let first = messages.first, let last = messages.last else { return nil }
return first.createdAt > last.createdAt ? first : last
}
}
extension ChannelPayload: Decodable {
enum CodingKeys: String, CodingKey {
case channel
case messages
case pendingMessages = "pending_messages"
case pinnedMessages = "pinned_messages"
case channelReads = "read"
case members
case watchers
case membership
case watcherCount = "watcher_count"
case hidden
case draft
case activeLiveLocations = "active_live_locations"
case pushPreference = "push_preferences"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.init(
channel: try container.decode(ChannelDetailPayload.self, forKey: .channel),
watcherCount: try container.decodeIfPresent(Int.self, forKey: .watcherCount),
watchers: try container.decodeArrayIfPresentIgnoringFailures([UserPayload].self, forKey: .watchers),
members: try container.decodeArrayIgnoringFailures([MemberPayload].self, forKey: .members),
membership: try container.decodeIfPresent(MemberPayload.self, forKey: .membership),
messages: try container.decodeArrayIgnoringFailures([MessagePayload].self, forKey: .messages),
pendingMessages: try container.decodeArrayIfPresentIgnoringFailures([MessagePayload.Boxed].self, forKey: .pendingMessages)?.map(\.message),
pinnedMessages: try container.decodeArrayIgnoringFailures([MessagePayload].self, forKey: .pinnedMessages),
channelReads: try container.decodeArrayIfPresentIgnoringFailures([ChannelReadPayload].self, forKey: .channelReads) ?? [],
isHidden: try container.decodeIfPresent(Bool.self, forKey: .hidden),
draft: try container.decodeIfPresent(DraftPayload.self, forKey: .draft),
activeLiveLocations: try container.decodeArrayIfPresentIgnoringFailures([SharedLocationPayload].self, forKey: .activeLiveLocations) ?? [],
pushPreference: try container.decodeIfPresent(PushPreferencePayload.self, forKey: .pushPreference)
)
}
}
struct ChannelDetailPayload {
let cid: ChannelId
let name: String?
let imageURL: URL?
let extraData: [String: RawJSON]
/// A channel type.
let typeRawValue: String
/// The last message date.
let lastMessageAt: Date?
/// A channel created date.
let createdAt: Date
/// A channel deleted date.
let deletedAt: Date?
/// A channel updated date.
let updatedAt: Date
/// A channel truncated date.
let truncatedAt: Date?
/// A creator of the channel.
let createdBy: UserPayload?
/// A config.
let config: ChannelConfig
let filterTags: [String]?
/// The list of actions that the current user can perform in a channel.
/// It is optional, since not all events contain the own capabilities property for performance reasons.
let ownCapabilities: [String]?
/// Checks if the channel is disabled.
let isDisabled: Bool
/// Checks if the channel is frozen.
let isFrozen: Bool
/// Checks if the channel is blocked.
let isBlocked: Bool?
/// Checks if the channel is hidden.
/// Backend only sends this field for `QueryChannel` and `QueryChannels` API calls,
/// but not for events.
/// Missing `hidden` field doesn't mean `false` for this reason.
let isHidden: Bool?
let members: [MemberPayload]?
let memberCount: Int
let messageCount: Int?
/// A list of users to invite in the channel.
let invitedMembers: [MemberPayload] = [] // TODO?
/// The team the channel belongs to. You need to enable multi-tenancy if you want to use this, else it'll be nil.
/// Refer to [docs](https://getstream.io/chat/docs/multi_tenant_chat/?language=swift) for more info.
let team: TeamId?
/// Cooldown duration for the channel, if it's in slow mode.
/// This value will be 0 if the channel is not in slow mode.
let cooldownDuration: Int
}
extension ChannelDetailPayload: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ChannelCodingKeys.self)
let extraData: [String: RawJSON]
if var payload = try? [String: RawJSON](from: decoder) {
payload.removeValues(forKeys: ChannelCodingKeys.allCases.map(\.rawValue))
extraData = payload
} else {
extraData = [:]
}
self.init(
cid: try container.decode(ChannelId.self, forKey: .cid),
name: try container.decodeIfPresent(String.self, forKey: .name),
// Unfortunately, the built-in URL decoder fails, if the string is empty. We need to
// provide custom decoding to handle URL? as expected.
imageURL: try container.decodeIfPresent(String.self, forKey: .imageURL).flatMap(URL.init(string:)),
extraData: extraData,
typeRawValue: try container.decode(String.self, forKey: .typeRawValue),
lastMessageAt: try container.decodeIfPresent(Date.self, forKey: .lastMessageAt),
createdAt: try container.decode(Date.self, forKey: .createdAt),
deletedAt: try container.decodeIfPresent(Date.self, forKey: .deletedAt),
updatedAt: try container.decode(Date.self, forKey: .updatedAt),
truncatedAt: try container.decodeIfPresent(Date.self, forKey: .truncatedAt),
createdBy: try container.decodeIfPresent(UserPayload.self, forKey: .createdBy),
config: try container.decode(ChannelConfig.self, forKey: .config),
filterTags: try container.decodeIfPresent([String].self, forKey: .filterTags),
ownCapabilities: try container.decodeIfPresent([String].self, forKey: .ownCapabilities),
isDisabled: try container.decode(Bool.self, forKey: .disabled),
isFrozen: try container.decode(Bool.self, forKey: .frozen),
isBlocked: try container.decodeIfPresent(Bool.self, forKey: .blocked),
// For `hidden`, we don't fallback to `false`
// since this field is not sent for all API calls and for events
// We can't assume anything regarding this flag when it's absent
isHidden: try container.decodeIfPresent(Bool.self, forKey: .hidden),
members: try container.decodeArrayIfPresentIgnoringFailures([MemberPayload].self, forKey: .members),
memberCount: try container.decodeIfPresent(Int.self, forKey: .memberCount) ?? 0,
messageCount: try container.decodeIfPresent(Int.self, forKey: .messageCount),
team: try container.decodeIfPresent(String.self, forKey: .team),
cooldownDuration: try container.decodeIfPresent(Int.self, forKey: .cooldownDuration) ?? 0
)
}
}
struct ChannelReadPayload: Decodable {
private enum CodingKeys: String, CodingKey {
case user
case lastReadAt = "last_read"
case lastReadMessageId = "last_read_message_id"
case unreadMessagesCount = "unread_messages"
case lastDeliveredAt = "last_delivered_at"
case lastDeliveredMessageId = "last_delivered_message_id"
}
/// A user (see `User`).
let user: UserPayload
/// A last read date by the user.
public let lastReadAt: Date
/// Id for the last message the user has read. Nil means the user has never read this channel
public let lastReadMessageId: MessageId?
/// Unread message count for the user.
public let unreadMessagesCount: Int
/// A last delivered date by the user.
public let lastDeliveredAt: Date?
/// Id for the last message the user has delivered. Nil means the user has never delivered this channel
public let lastDeliveredMessageId: MessageId?
}
/// A channel config.
public class ChannelConfig: Codable {
private enum CodingKeys: String, CodingKey {
case reactionsEnabled = "reactions"
case typingEventsEnabled = "typing_events"
case deliveryEventsEnabled = "delivery_events"
case readEventsEnabled = "read_events"
case connectEventsEnabled = "connect_events"
case uploadsEnabled = "uploads"
case repliesEnabled = "replies"
case quotesEnabled = "quotes"
case searchEnabled = "search"
case mutesEnabled = "mutes"
case pollsEnabled = "polls"
case urlEnrichmentEnabled = "url_enrichment"
case messageRetention = "message_retention"
case maxMessageLength = "max_message_length"
case commands
case createdAt = "created_at"
case updatedAt = "updated_at"
case skipLastMsgAtUpdateForSystemMsg = "skip_last_msg_update_for_system_msgs"
case messageRemindersEnabled = "user_message_reminders"
case sharedLocationsEnabled = "shared_locations"
}
/// If users are allowed to add reactions to messages. Enabled by default.
public let reactionsEnabled: Bool
/// Controls if typing indicators are shown. Enabled by default.
public let typingEventsEnabled: Bool
/// Controls whether the chat shows how far you've read. Enabled by default.
public let readEventsEnabled: Bool
/// Controls whether messages delivered events are handled. Disabled by default.
public let deliveryEventsEnabled: Bool
/// Determines if events are fired for connecting and disconnecting to a chat. Enabled by default.
public let connectEventsEnabled: Bool
/// Enables uploads.
public let uploadsEnabled: Bool
/// Enables message thread replies. Enabled by default.
public let repliesEnabled: Bool
/// Enables quoting of messages. Enabled by default.
public let quotesEnabled: Bool
/// Controls if messages should be searchable (this is a premium feature). Disabled by default.
public let searchEnabled: Bool
/// Determines if users are able to mute other users. Enabled by default.
public let mutesEnabled: Bool
/// Determines if URL enrichment enabled to show they as attachments. Enabled by default.
public let urlEnrichmentEnabled: Bool
/// A number of days or infinite. Infinite by default.
public let messageRetention: String
/// The max message length. 5000 by default.
public let maxMessageLength: Int
/// An array of commands, e.g. /giphy.
public let commands: [Command]
/// A channel created date.
public let createdAt: Date
/// A channel updated date.
public let updatedAt: Date
/// Determines if polls are enabled.
public let pollsEnabled: Bool
/// Determines if system messages should not update the last message at date.
public let skipLastMsgAtUpdateForSystemMsg: Bool
/// Determines if user message reminders are enabled.
public let messageRemindersEnabled: Bool
/// Determines if shared locations are enabled.
public let sharedLocationsEnabled: Bool
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
reactionsEnabled = try container.decode(Bool.self, forKey: .reactionsEnabled)
typingEventsEnabled = try container.decode(Bool.self, forKey: .typingEventsEnabled)
readEventsEnabled = try container.decode(Bool.self, forKey: .readEventsEnabled)
deliveryEventsEnabled = try container.decodeIfPresent(Bool.self, forKey: .deliveryEventsEnabled) ?? false
connectEventsEnabled = try container.decode(Bool.self, forKey: .connectEventsEnabled)
uploadsEnabled = try container.decodeIfPresent(Bool.self, forKey: .uploadsEnabled) ?? false
repliesEnabled = try container.decode(Bool.self, forKey: .repliesEnabled)
quotesEnabled = try container.decode(Bool.self, forKey: .quotesEnabled)
searchEnabled = try container.decode(Bool.self, forKey: .searchEnabled)
mutesEnabled = try container.decode(Bool.self, forKey: .mutesEnabled)
urlEnrichmentEnabled = try container.decode(Bool.self, forKey: .urlEnrichmentEnabled)
messageRetention = try container.decode(String.self, forKey: .messageRetention)
maxMessageLength = try container.decode(Int.self, forKey: .maxMessageLength)
let commands = try container.decodeIfPresent([Command].self, forKey: .commands) ?? []
// We exclude the flag commands since it's not implemented by backend
// and it'll be removed soon.
// TODO: Remove this line of code when backend stops sending the `flag` command
self.commands = commands.filter { $0.name != "flag" }
createdAt = try container.decode(Date.self, forKey: .createdAt)
updatedAt = try container.decode(Date.self, forKey: .updatedAt)
pollsEnabled = try container.decodeIfPresent(Bool.self, forKey: .pollsEnabled) ?? false
skipLastMsgAtUpdateForSystemMsg = try container.decodeIfPresent(Bool.self, forKey: .skipLastMsgAtUpdateForSystemMsg) ?? false
messageRemindersEnabled = try container.decodeIfPresent(Bool.self, forKey: .messageRemindersEnabled) ?? false
sharedLocationsEnabled = try container.decodeIfPresent(Bool.self, forKey: .sharedLocationsEnabled) ?? false
}
internal required init(
reactionsEnabled: Bool = false,
typingEventsEnabled: Bool = false,
readEventsEnabled: Bool = false,
deliveryEventsEnabled: Bool = false,
connectEventsEnabled: Bool = false,
uploadsEnabled: Bool = false,
repliesEnabled: Bool = false,
quotesEnabled: Bool = false,
searchEnabled: Bool = false,
mutesEnabled: Bool = false,
pollsEnabled: Bool = false,
urlEnrichmentEnabled: Bool = false,
skipLastMsgAtUpdateForSystemMsg: Bool = false,
messageRemindersEnabled: Bool = false,
sharedLocationsEnabled: Bool = false,
messageRetention: String = "",
maxMessageLength: Int = 0,
commands: [Command] = [],
createdAt: Date = .init(),
updatedAt: Date = .init()
) {
self.reactionsEnabled = reactionsEnabled
self.typingEventsEnabled = typingEventsEnabled
self.readEventsEnabled = readEventsEnabled
self.deliveryEventsEnabled = deliveryEventsEnabled
self.connectEventsEnabled = connectEventsEnabled
self.uploadsEnabled = uploadsEnabled
self.repliesEnabled = repliesEnabled
self.quotesEnabled = quotesEnabled
self.searchEnabled = searchEnabled
self.mutesEnabled = mutesEnabled
self.urlEnrichmentEnabled = urlEnrichmentEnabled
self.messageRetention = messageRetention
self.maxMessageLength = maxMessageLength
self.commands = commands
self.createdAt = createdAt
self.updatedAt = updatedAt
self.pollsEnabled = pollsEnabled
self.skipLastMsgAtUpdateForSystemMsg = skipLastMsgAtUpdateForSystemMsg
self.messageRemindersEnabled = messageRemindersEnabled
self.sharedLocationsEnabled = sharedLocationsEnabled
}
}