Skip to content

Commit

Permalink
add the iOS side of the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Feb 21, 2024
1 parent 5e9b4d6 commit 6264f28
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,12 @@ public class XMTPModule: Module {
try await subscribeToConversations(clientAddress: clientAddress)
}

AsyncFunction("subscribeToAllMessages") { (clientAddress: String) in
try await subscribeToAllMessages(clientAddress: clientAddress)
AsyncFunction("subscribeToAllMessages") { (clientAddress: String, includeGroups: Bool) in
try await subscribeToAllMessages(clientAddress: clientAddress, includeGroups: includeGroups)
}

AsyncFunction("subscribeToAllGroupMessages") { (clientAddress: String) in
try await subscribeToAllGroupMessages(clientAddress: clientAddress)
}

AsyncFunction("subscribeToMessages") { (clientAddress: String, topic: String) in
Expand All @@ -711,6 +715,11 @@ public class XMTPModule: Module {
AsyncFunction("unsubscribeFromAllMessages") { (clientAddress: String) in
await subscriptionsManager.get(getMessagesKey(clientAddress: clientAddress))?.cancel()
}

AsyncFunction("unsubscribeFromAllGroupMessages") { (clientAddress: String) in
await subscriptionsManager.get(getGroupMessagesKey(clientAddress: clientAddress))?.cancel()
}


AsyncFunction("unsubscribeFromMessages") { (clientAddress: String, topic: String) in
try await unsubscribeFromMessages(clientAddress: clientAddress, topic: topic)
Expand Down Expand Up @@ -916,15 +925,40 @@ public class XMTPModule: Module {
})
}

func subscribeToAllMessages(clientAddress: String) async throws {
func subscribeToAllMessages(clientAddress: String, includeGroups: Bool = false) async throws {
guard let client = await clientsManager.getClient(key: clientAddress) else {
return
}

await subscriptionsManager.get(getMessagesKey(clientAddress: clientAddress))?.cancel()
await subscriptionsManager.set(getMessagesKey(clientAddress: clientAddress), Task {
do {
for try await message in try await client.conversations.streamAllDecryptedMessages() {
for try await message in await client.conversations.streamAllDecryptedMessages(includeGroups: includeGroups) {
do {
try sendEvent("message", [
"clientAddress": clientAddress,
"message": DecodedMessageWrapper.encodeToObj(message, client: client),
])
} catch {
print("discarding message, unable to encode wrapper \(message.id)")
}
}
} catch {
print("Error in all messages subscription: \(error)")
await subscriptionsManager.get(getMessagesKey(clientAddress: clientAddress))?.cancel()
}
})
}

func subscribeToAllGroupMessages(clientAddress: String) async throws {
guard let client = await clientsManager.getClient(key: clientAddress) else {
return
}

await subscriptionsManager.get(getGroupMessagesKey(clientAddress: clientAddress))?.cancel()
await subscriptionsManager.set(getGroupMessagesKey(clientAddress: clientAddress), Task {
do {
for try await message in await client.conversations.streamAllGroupDecryptedMessages() {
do {
try sendEvent("message", [
"clientAddress": clientAddress,
Expand Down Expand Up @@ -1060,6 +1094,10 @@ public class XMTPModule: Module {
func getMessagesKey(clientAddress: String) -> String {
return "messages:\(clientAddress)"
}

func getGroupMessagesKey(clientAddress: String) -> String {
return "groupMessages:\(clientAddress)"
}

func getConversationsKey(clientAddress: String) -> String {
return "conversations:\(clientAddress)"
Expand Down

0 comments on commit 6264f28

Please sign in to comment.