@@ -79,9 +79,11 @@ public actor Conversations {
79
79
public func sync( ) async throws {
80
80
try await ffiConversations. sync ( )
81
81
}
82
- public func syncAllConversations( ) async throws -> UInt32 {
83
- // TODO: add consent state here
84
- return try await ffiConversations. syncAllConversations ( consentState: nil )
82
+ public func syncAllConversations( consentState: ConsentState ? = nil )
83
+ async throws -> UInt32
84
+ {
85
+ return try await ffiConversations. syncAllConversations (
86
+ consentState: consentState? . toFFI)
85
87
}
86
88
87
89
public func listGroups(
@@ -105,7 +107,7 @@ public actor Conversations {
105
107
let conversations = try await ffiConversations. listGroups (
106
108
opts: options)
107
109
108
- let sortedConversations = try sortConversations (
110
+ let sortedConversations = try await sortConversations (
109
111
conversations, order: order)
110
112
111
113
return sortedConversations. map {
@@ -134,7 +136,7 @@ public actor Conversations {
134
136
let conversations = try await ffiConversations. listDms (
135
137
opts: options)
136
138
137
- let sortedConversations = try sortConversations (
139
+ let sortedConversations = try await sortConversations (
138
140
conversations, order: order)
139
141
140
142
return sortedConversations. map {
@@ -160,36 +162,42 @@ public actor Conversations {
160
162
if let limit {
161
163
options. limit = Int64 ( limit)
162
164
}
163
- let conversations = try await ffiConversations. list (
165
+ let ffiConversations = try await ffiConversations. list (
164
166
opts: options)
165
167
166
- let sortedConversations = try sortConversations (
167
- conversations, order: order)
168
+ let sortedConversations = try await sortConversations (
169
+ ffiConversations, order: order)
170
+
171
+ var conversations : [ Conversation ] = [ ]
172
+ for sortedConversation in sortedConversations {
173
+ let conversation = try await sortedConversation. toConversation ( client: client)
174
+ conversations. append ( conversation)
175
+ }
168
176
169
- return try sortedConversations. map {
170
- try $0. toConversation ( client: client)
171
- }
177
+ return conversations
172
178
}
173
179
174
180
private func sortConversations(
175
181
_ conversations: [ FfiConversation ] ,
176
182
order: ConversationOrder
177
- ) throws -> [ FfiConversation ] {
183
+ ) async throws -> [ FfiConversation ] {
178
184
switch order {
179
185
case . lastMessage:
180
- let conversationWithTimestamp : [ ( FfiConversation , Int64 ? ) ] =
181
- try conversations. map { conversation in
182
- let message = try conversation. findMessages (
183
- opts: FfiListMessagesOptions (
184
- sentBeforeNs: nil ,
185
- sentAfterNs: nil ,
186
- limit: 1 ,
187
- deliveryStatus: nil ,
188
- direction: . descending
189
- )
190
- ) . first
191
- return ( conversation, message? . sentAtNs)
192
- }
186
+ var conversationWithTimestamp : [ ( FfiConversation , Int64 ? ) ] = [ ]
187
+
188
+ for conversation in conversations {
189
+ let message = try await conversation. findMessages (
190
+ opts: FfiListMessagesOptions (
191
+ sentBeforeNs: nil ,
192
+ sentAfterNs: nil ,
193
+ limit: 1 ,
194
+ deliveryStatus: nil ,
195
+ direction: . descending
196
+ )
197
+ ) . first
198
+ conversationWithTimestamp. append (
199
+ ( conversation, message? . sentAtNs) )
200
+ }
193
201
194
202
let sortedTuples = conversationWithTimestamp. sorted { ( lhs, rhs) in
195
203
( lhs. 1 ?? 0 ) > ( rhs. 1 ?? 0 )
@@ -207,41 +215,45 @@ public actor Conversations {
207
215
let ffiStreamActor = FfiStreamActor ( )
208
216
let conversationCallback = ConversationStreamCallback {
209
217
conversation in
210
- guard !Task. isCancelled else {
211
- continuation. finish ( )
212
- return
213
- }
214
- do {
215
- let conversationType = try conversation. conversationType ( )
216
- if conversationType == . dm {
217
- continuation. yield (
218
- Conversation . dm (
219
- conversation. dmFromFFI ( client: self . client) )
220
- )
221
- } else if conversationType == . group {
222
- continuation. yield (
223
- Conversation . group (
224
- conversation. groupFromFFI ( client: self . client) )
225
- )
218
+ Task {
219
+ guard !Task. isCancelled else {
220
+ continuation. finish ( )
221
+ return
222
+ }
223
+ do {
224
+ let conversationType =
225
+ try await conversation. conversationType ( )
226
+ if conversationType == . dm {
227
+ continuation. yield (
228
+ Conversation . dm (
229
+ conversation. dmFromFFI ( client: self . client) )
230
+ )
231
+ } else if conversationType == . group {
232
+ continuation. yield (
233
+ Conversation . group (
234
+ conversation. groupFromFFI (
235
+ client: self . client) )
236
+ )
237
+ }
238
+ } catch {
239
+ print ( " Error processing conversation type: \( error) " )
226
240
}
227
- } catch {
228
- // Do nothing if the conversation type is neither a group or dm
229
241
}
230
242
}
231
243
232
244
let task = Task {
233
245
let stream : FfiStreamCloser
234
- switch type {
235
- case . groups:
236
- stream = await ffiConversations. streamGroups (
237
- callback: conversationCallback)
238
- case . all:
239
- stream = await ffiConversations. stream (
240
- callback: conversationCallback)
241
- case . dms:
242
- stream = await ffiConversations. streamDms (
243
- callback: conversationCallback)
244
- }
246
+ switch type {
247
+ case . groups:
248
+ stream = await ffiConversations. streamGroups (
249
+ callback: conversationCallback)
250
+ case . all:
251
+ stream = await ffiConversations. stream (
252
+ callback: conversationCallback)
253
+ case . dms:
254
+ stream = await ffiConversations. streamDms (
255
+ callback: conversationCallback)
256
+ }
245
257
await ffiStreamActor. setFfiStream ( stream)
246
258
continuation. onTermination = { @Sendable reason in
247
259
Task {
@@ -268,7 +280,9 @@ public actor Conversations {
268
280
if !canMessage {
269
281
throw ConversationError . memberNotRegistered ( [ peerAddress] )
270
282
}
271
- if let existingDm = try await client. findDmByAddress ( address: peerAddress) {
283
+ if let existingDm = try await client. findDmByAddress (
284
+ address: peerAddress)
285
+ {
272
286
return existingDm
273
287
}
274
288
@@ -385,20 +399,20 @@ public actor Conversations {
385
399
386
400
let task = Task {
387
401
let stream : FfiStreamCloser
388
- switch type {
389
- case . groups:
390
- stream = await ffiConversations. streamAllGroupMessages (
391
- messageCallback: messageCallback
392
- )
393
- case . dms:
394
- stream = await ffiConversations. streamAllDmMessages (
395
- messageCallback: messageCallback
396
- )
397
- case . all:
398
- stream = await ffiConversations. streamAllMessages (
399
- messageCallback: messageCallback
400
- )
401
- }
402
+ switch type {
403
+ case . groups:
404
+ stream = await ffiConversations. streamAllGroupMessages (
405
+ messageCallback: messageCallback
406
+ )
407
+ case . dms:
408
+ stream = await ffiConversations. streamAllDmMessages (
409
+ messageCallback: messageCallback
410
+ )
411
+ case . all:
412
+ stream = await ffiConversations. streamAllMessages (
413
+ messageCallback: messageCallback
414
+ )
415
+ }
402
416
await ffiStreamActor. setFfiStream ( stream)
403
417
}
404
418
@@ -417,7 +431,7 @@ public actor Conversations {
417
431
let conversation =
418
432
try await ffiConversations
419
433
. processStreamedWelcomeMessage ( envelopeBytes: envelopeBytes)
420
- return try conversation. toConversation ( client: client)
434
+ return try await conversation. toConversation ( client: client)
421
435
}
422
436
423
437
public func newConversation(
0 commit comments