Skip to content

Commit 712d873

Browse files
Sync All Conversations Consent Filtering (#449)
* add consent sync filtering * update async methods * update async methods * more fixes * add missing async modifier * bump podspec to 3.0.18 --------- Co-authored-by: cameronvoell <[email protected]>
1 parent b6d58ed commit 712d873

File tree

13 files changed

+159
-113
lines changed

13 files changed

+159
-113
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let package = Package(
2121
.package(url: "https://github.com/bufbuild/connect-swift", exact: "1.0.0"),
2222
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.4.3"),
2323
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", exact: "1.8.3"),
24-
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.12")
24+
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.13")
2525
],
2626
targets: [
2727
.target(

Sources/XMTPiOS/Client.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ public final class Client {
204204
let dbURL = directoryURL.appendingPathComponent(alias).path
205205

206206
let ffiClient = try await LibXMTP.createClient(
207-
host: options.api.env.url,
208-
isSecure: options.api.env.isSecure == true,
207+
api: connectToBackend(
208+
host: options.api.env.url,
209+
isSecure: options.api.env.isSecure == true),
209210
db: dbURL,
210211
encryptionKey: options.dbEncryptionKey,
211212
inboxId: inboxId,
@@ -297,8 +298,9 @@ public final class Client {
297298
let dbURL = directoryURL.appendingPathComponent(alias).path
298299

299300
let ffiClient = try await LibXMTP.createClient(
300-
host: api.env.url,
301-
isSecure: api.env.isSecure == true,
301+
api: connectToBackend(
302+
host: api.env.url,
303+
isSecure: api.env.isSecure == true),
302304
db: dbURL,
303305
encryptionKey: nil,
304306
inboxId: inboxId,
@@ -448,18 +450,18 @@ public final class Client {
448450
}
449451
}
450452

451-
public func findConversation(conversationId: String) throws -> Conversation?
453+
public func findConversation(conversationId: String) async throws -> Conversation?
452454
{
453455
do {
454456
let conversation = try ffiClient.conversation(
455457
conversationId: conversationId.hexToData)
456-
return try conversation.toConversation(client: self)
458+
return try await conversation.toConversation(client: self)
457459
} catch {
458460
return nil
459461
}
460462
}
461463

462-
public func findConversationByTopic(topic: String) throws -> Conversation? {
464+
public func findConversationByTopic(topic: String) async throws -> Conversation? {
463465
do {
464466
let regexPattern = #"/xmtp/mls/1/g-(.*?)/proto"#
465467
if let regex = try? NSRegularExpression(pattern: regexPattern) {
@@ -471,7 +473,7 @@ public final class Client {
471473
with: match.range(at: 1))
472474
let conversation = try ffiClient.conversation(
473475
conversationId: conversationId.hexToData)
474-
return try conversation.toConversation(client: self)
476+
return try await conversation.toConversation(client: self)
475477
}
476478
}
477479
} catch {

Sources/XMTPiOS/Conversation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public enum Conversation: Identifiable, Equatable, Hashable {
2929
public func isCreator() async throws -> Bool {
3030
switch self {
3131
case let .group(group):
32-
return try group.isCreator()
32+
return try await group.isCreator()
3333
case let .dm(dm):
34-
return try dm.isCreator()
34+
return try await dm.isCreator()
3535
}
3636
}
3737

Sources/XMTPiOS/Conversations.swift

Lines changed: 84 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public actor Conversations {
7979
public func sync() async throws {
8080
try await ffiConversations.sync()
8181
}
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)
8587
}
8688

8789
public func listGroups(
@@ -105,7 +107,7 @@ public actor Conversations {
105107
let conversations = try await ffiConversations.listGroups(
106108
opts: options)
107109

108-
let sortedConversations = try sortConversations(
110+
let sortedConversations = try await sortConversations(
109111
conversations, order: order)
110112

111113
return sortedConversations.map {
@@ -134,7 +136,7 @@ public actor Conversations {
134136
let conversations = try await ffiConversations.listDms(
135137
opts: options)
136138

137-
let sortedConversations = try sortConversations(
139+
let sortedConversations = try await sortConversations(
138140
conversations, order: order)
139141

140142
return sortedConversations.map {
@@ -160,36 +162,42 @@ public actor Conversations {
160162
if let limit {
161163
options.limit = Int64(limit)
162164
}
163-
let conversations = try await ffiConversations.list(
165+
let ffiConversations = try await ffiConversations.list(
164166
opts: options)
165167

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+
}
168176

169-
return try sortedConversations.map {
170-
try $0.toConversation(client: client)
171-
}
177+
return conversations
172178
}
173179

174180
private func sortConversations(
175181
_ conversations: [FfiConversation],
176182
order: ConversationOrder
177-
) throws -> [FfiConversation] {
183+
) async throws -> [FfiConversation] {
178184
switch order {
179185
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+
}
193201

194202
let sortedTuples = conversationWithTimestamp.sorted { (lhs, rhs) in
195203
(lhs.1 ?? 0) > (rhs.1 ?? 0)
@@ -207,41 +215,45 @@ public actor Conversations {
207215
let ffiStreamActor = FfiStreamActor()
208216
let conversationCallback = ConversationStreamCallback {
209217
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)")
226240
}
227-
} catch {
228-
// Do nothing if the conversation type is neither a group or dm
229241
}
230242
}
231243

232244
let task = Task {
233245
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+
}
245257
await ffiStreamActor.setFfiStream(stream)
246258
continuation.onTermination = { @Sendable reason in
247259
Task {
@@ -268,7 +280,9 @@ public actor Conversations {
268280
if !canMessage {
269281
throw ConversationError.memberNotRegistered([peerAddress])
270282
}
271-
if let existingDm = try await client.findDmByAddress(address: peerAddress) {
283+
if let existingDm = try await client.findDmByAddress(
284+
address: peerAddress)
285+
{
272286
return existingDm
273287
}
274288

@@ -385,20 +399,20 @@ public actor Conversations {
385399

386400
let task = Task {
387401
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+
}
402416
await ffiStreamActor.setFfiStream(stream)
403417
}
404418

@@ -417,7 +431,7 @@ public actor Conversations {
417431
let conversation =
418432
try await ffiConversations
419433
.processStreamedWelcomeMessage(envelopeBytes: envelopeBytes)
420-
return try conversation.toConversation(client: client)
434+
return try await conversation.toConversation(client: client)
421435
}
422436

423437
public func newConversation(

Sources/XMTPiOS/Dm.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public struct Dm: Identifiable, Equatable, Hashable {
1414
Topic.groupMessage(id).description
1515
}
1616

17-
func metadata() throws -> FfiConversationMetadata {
18-
return try ffiConversation.groupMetadata()
17+
func metadata() async throws -> FfiConversationMetadata {
18+
return try await ffiConversation.groupMetadata()
1919
}
2020

2121
public func sync() async throws {
@@ -30,12 +30,12 @@ public struct Dm: Identifiable, Equatable, Hashable {
3030
id.hash(into: &hasher)
3131
}
3232

33-
public func isCreator() throws -> Bool {
34-
return try metadata().creatorInboxId() == client.inboxID
33+
public func isCreator() async throws -> Bool {
34+
return try await metadata().creatorInboxId() == client.inboxID
3535
}
3636

37-
public func creatorInboxId() throws -> String {
38-
return try metadata().creatorInboxId()
37+
public func creatorInboxId() async throws -> String {
38+
return try await metadata().creatorInboxId()
3939
}
4040

4141
public func addedByInboxId() throws -> String {
@@ -253,7 +253,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
253253

254254
options.direction = direction
255255

256-
return try ffiConversation.findMessages(opts: options).compactMap {
256+
return try await ffiConversation.findMessages(opts: options).compactMap {
257257
ffiMessage in
258258
return Message(client: self.client, ffiMessage: ffiMessage)
259259
.decodeOrNull()

Sources/XMTPiOS/Extensions/Ffi.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extension FfiConversation {
1010
Dm(ffiConversation: self, client: client)
1111
}
1212

13-
func toConversation(client: Client) throws -> Conversation {
14-
if try conversationType() == .dm {
13+
func toConversation(client: Client) async throws -> Conversation {
14+
if try await conversationType() == .dm {
1515
return Conversation.dm(self.dmFromFFI(client: client))
1616
} else {
1717
return Conversation.group(self.groupFromFFI(client: client))

0 commit comments

Comments
 (0)