Skip to content

Commit

Permalink
Sync All Conversations Consent Filtering (#449)
Browse files Browse the repository at this point in the history
* 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 <cameronvoell@users.noreply.github.com>
  • Loading branch information
nplasterer and cameronvoell authored Dec 17, 2024
1 parent b6d58ed commit 712d873
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 113 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/libxmtp-swift.git",
"state" : {
"revision" : "d6670069b6f7ae52415c691dbca4c37fbdaf87b4",
"version" : "3.0.12"
"revision" : "203fd6d67bb72e3114b273ce9bbddd6fc747d583",
"version" : "3.0.13"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.package(url: "https://github.com/bufbuild/connect-swift", exact: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.4.3"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", exact: "1.8.3"),
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.12")
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.13")
],
targets: [
.target(
Expand Down
18 changes: 10 additions & 8 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ public final class Client {
let dbURL = directoryURL.appendingPathComponent(alias).path

let ffiClient = try await LibXMTP.createClient(
host: options.api.env.url,
isSecure: options.api.env.isSecure == true,
api: connectToBackend(
host: options.api.env.url,
isSecure: options.api.env.isSecure == true),
db: dbURL,
encryptionKey: options.dbEncryptionKey,
inboxId: inboxId,
Expand Down Expand Up @@ -297,8 +298,9 @@ public final class Client {
let dbURL = directoryURL.appendingPathComponent(alias).path

let ffiClient = try await LibXMTP.createClient(
host: api.env.url,
isSecure: api.env.isSecure == true,
api: connectToBackend(
host: api.env.url,
isSecure: api.env.isSecure == true),
db: dbURL,
encryptionKey: nil,
inboxId: inboxId,
Expand Down Expand Up @@ -448,18 +450,18 @@ public final class Client {
}
}

public func findConversation(conversationId: String) throws -> Conversation?
public func findConversation(conversationId: String) async throws -> Conversation?
{
do {
let conversation = try ffiClient.conversation(
conversationId: conversationId.hexToData)
return try conversation.toConversation(client: self)
return try await conversation.toConversation(client: self)
} catch {
return nil
}
}

public func findConversationByTopic(topic: String) throws -> Conversation? {
public func findConversationByTopic(topic: String) async throws -> Conversation? {
do {
let regexPattern = #"/xmtp/mls/1/g-(.*?)/proto"#
if let regex = try? NSRegularExpression(pattern: regexPattern) {
Expand All @@ -471,7 +473,7 @@ public final class Client {
with: match.range(at: 1))
let conversation = try ffiClient.conversation(
conversationId: conversationId.hexToData)
return try conversation.toConversation(client: self)
return try await conversation.toConversation(client: self)
}
}
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public enum Conversation: Identifiable, Equatable, Hashable {
public func isCreator() async throws -> Bool {
switch self {
case let .group(group):
return try group.isCreator()
return try await group.isCreator()
case let .dm(dm):
return try dm.isCreator()
return try await dm.isCreator()
}
}

Expand Down
154 changes: 84 additions & 70 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ public actor Conversations {
public func sync() async throws {
try await ffiConversations.sync()
}
public func syncAllConversations() async throws -> UInt32 {
// TODO: add consent state here
return try await ffiConversations.syncAllConversations(consentState: nil)
public func syncAllConversations(consentState: ConsentState? = nil)
async throws -> UInt32
{
return try await ffiConversations.syncAllConversations(
consentState: consentState?.toFFI)
}

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

let sortedConversations = try sortConversations(
let sortedConversations = try await sortConversations(
conversations, order: order)

return sortedConversations.map {
Expand Down Expand Up @@ -134,7 +136,7 @@ public actor Conversations {
let conversations = try await ffiConversations.listDms(
opts: options)

let sortedConversations = try sortConversations(
let sortedConversations = try await sortConversations(
conversations, order: order)

return sortedConversations.map {
Expand All @@ -160,36 +162,42 @@ public actor Conversations {
if let limit {
options.limit = Int64(limit)
}
let conversations = try await ffiConversations.list(
let ffiConversations = try await ffiConversations.list(
opts: options)

let sortedConversations = try sortConversations(
conversations, order: order)
let sortedConversations = try await sortConversations(
ffiConversations, order: order)

var conversations: [Conversation] = []
for sortedConversation in sortedConversations {
let conversation = try await sortedConversation.toConversation(client: client)
conversations.append(conversation)
}

return try sortedConversations.map {
try $0.toConversation(client: client)
}
return conversations
}

private func sortConversations(
_ conversations: [FfiConversation],
order: ConversationOrder
) throws -> [FfiConversation] {
) async throws -> [FfiConversation] {
switch order {
case .lastMessage:
let conversationWithTimestamp: [(FfiConversation, Int64?)] =
try conversations.map { conversation in
let message = try conversation.findMessages(
opts: FfiListMessagesOptions(
sentBeforeNs: nil,
sentAfterNs: nil,
limit: 1,
deliveryStatus: nil,
direction: .descending
)
).first
return (conversation, message?.sentAtNs)
}
var conversationWithTimestamp: [(FfiConversation, Int64?)] = []

for conversation in conversations {
let message = try await conversation.findMessages(
opts: FfiListMessagesOptions(
sentBeforeNs: nil,
sentAfterNs: nil,
limit: 1,
deliveryStatus: nil,
direction: .descending
)
).first
conversationWithTimestamp.append(
(conversation, message?.sentAtNs))
}

let sortedTuples = conversationWithTimestamp.sorted { (lhs, rhs) in
(lhs.1 ?? 0) > (rhs.1 ?? 0)
Expand All @@ -207,41 +215,45 @@ public actor Conversations {
let ffiStreamActor = FfiStreamActor()
let conversationCallback = ConversationStreamCallback {
conversation in
guard !Task.isCancelled else {
continuation.finish()
return
}
do {
let conversationType = try conversation.conversationType()
if conversationType == .dm {
continuation.yield(
Conversation.dm(
conversation.dmFromFFI(client: self.client))
)
} else if conversationType == .group {
continuation.yield(
Conversation.group(
conversation.groupFromFFI(client: self.client))
)
Task {
guard !Task.isCancelled else {
continuation.finish()
return
}
do {
let conversationType =
try await conversation.conversationType()
if conversationType == .dm {
continuation.yield(
Conversation.dm(
conversation.dmFromFFI(client: self.client))
)
} else if conversationType == .group {
continuation.yield(
Conversation.group(
conversation.groupFromFFI(
client: self.client))
)
}
} catch {
print("Error processing conversation type: \(error)")
}
} catch {
// Do nothing if the conversation type is neither a group or dm
}
}

let task = Task {
let stream: FfiStreamCloser
switch type {
case .groups:
stream = await ffiConversations.streamGroups(
callback: conversationCallback)
case .all:
stream = await ffiConversations.stream(
callback: conversationCallback)
case .dms:
stream = await ffiConversations.streamDms(
callback: conversationCallback)
}
switch type {
case .groups:
stream = await ffiConversations.streamGroups(
callback: conversationCallback)
case .all:
stream = await ffiConversations.stream(
callback: conversationCallback)
case .dms:
stream = await ffiConversations.streamDms(
callback: conversationCallback)
}
await ffiStreamActor.setFfiStream(stream)
continuation.onTermination = { @Sendable reason in
Task {
Expand All @@ -268,7 +280,9 @@ public actor Conversations {
if !canMessage {
throw ConversationError.memberNotRegistered([peerAddress])
}
if let existingDm = try await client.findDmByAddress(address: peerAddress) {
if let existingDm = try await client.findDmByAddress(
address: peerAddress)
{
return existingDm
}

Expand Down Expand Up @@ -385,20 +399,20 @@ public actor Conversations {

let task = Task {
let stream: FfiStreamCloser
switch type {
case .groups:
stream = await ffiConversations.streamAllGroupMessages(
messageCallback: messageCallback
)
case .dms:
stream = await ffiConversations.streamAllDmMessages(
messageCallback: messageCallback
)
case .all:
stream = await ffiConversations.streamAllMessages(
messageCallback: messageCallback
)
}
switch type {
case .groups:
stream = await ffiConversations.streamAllGroupMessages(
messageCallback: messageCallback
)
case .dms:
stream = await ffiConversations.streamAllDmMessages(
messageCallback: messageCallback
)
case .all:
stream = await ffiConversations.streamAllMessages(
messageCallback: messageCallback
)
}
await ffiStreamActor.setFfiStream(stream)
}

Expand All @@ -417,7 +431,7 @@ public actor Conversations {
let conversation =
try await ffiConversations
.processStreamedWelcomeMessage(envelopeBytes: envelopeBytes)
return try conversation.toConversation(client: client)
return try await conversation.toConversation(client: client)
}

public func newConversation(
Expand Down
14 changes: 7 additions & 7 deletions Sources/XMTPiOS/Dm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public struct Dm: Identifiable, Equatable, Hashable {
Topic.groupMessage(id).description
}

func metadata() throws -> FfiConversationMetadata {
return try ffiConversation.groupMetadata()
func metadata() async throws -> FfiConversationMetadata {
return try await ffiConversation.groupMetadata()
}

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

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

public func creatorInboxId() throws -> String {
return try metadata().creatorInboxId()
public func creatorInboxId() async throws -> String {
return try await metadata().creatorInboxId()
}

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

options.direction = direction

return try ffiConversation.findMessages(opts: options).compactMap {
return try await ffiConversation.findMessages(opts: options).compactMap {
ffiMessage in
return Message(client: self.client, ffiMessage: ffiMessage)
.decodeOrNull()
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Extensions/Ffi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extension FfiConversation {
Dm(ffiConversation: self, client: client)
}

func toConversation(client: Client) throws -> Conversation {
if try conversationType() == .dm {
func toConversation(client: Client) async throws -> Conversation {
if try await conversationType() == .dm {
return Conversation.dm(self.dmFromFFI(client: client))
} else {
return Conversation.group(self.groupFromFFI(client: client))
Expand Down
Loading

0 comments on commit 712d873

Please sign in to comment.