Skip to content

Commit

Permalink
Merge pull request #267 from xmtp/np/ios-can-message
Browse files Browse the repository at this point in the history
iOS Can Message
  • Loading branch information
nplasterer authored Feb 16, 2024
2 parents cbcc042 + 0217c13 commit 1e298e9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
14 changes: 7 additions & 7 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PODS:
- hermes-engine/Pre-built (= 0.71.14)
- hermes-engine/Pre-built (0.71.14)
- libevent (2.1.12)
- LibXMTP (0.4.1-beta3)
- LibXMTP (0.4.2-beta1)
- Logging (1.0.0)
- MessagePacker (0.4.7)
- MMKV (1.3.3):
Expand Down Expand Up @@ -442,16 +442,16 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.8.5):
- XMTP (0.8.6):
- Connect-Swift (= 0.3.0)
- GzipSwift
- LibXMTP (= 0.4.1-beta3)
- LibXMTP (= 0.4.2-beta1)
- web3.swift
- XMTPReactNative (0.1.0):
- ExpoModulesCore
- MessagePacker
- secp256k1.swift
- XMTP (= 0.8.5)
- XMTP (= 0.8.6)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -695,7 +695,7 @@ SPEC CHECKSUMS:
GzipSwift: 893f3e48e597a1a4f62fafcb6514220fcf8287fa
hermes-engine: d7cc127932c89c53374452d6f93473f1970d8e88
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
LibXMTP: 678390cd0049d090af4aa6a7d5c9133ded3473b3
LibXMTP: 026f2fec3c88b24e92f21895b29a12817fa54779
Logging: 9ef4ecb546ad3169398d5a723bc9bea1c46bef26
MessagePacker: ab2fe250e86ea7aedd1a9ee47a37083edd41fd02
MMKV: f902fb6719da13c2ab0965233d8963a59416f911
Expand Down Expand Up @@ -744,8 +744,8 @@ SPEC CHECKSUMS:
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftProtobuf: b02b5075dcf60c9f5f403000b3b0c202a11b6ae1
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: 1957e318059e8723bc1e76b1723c1eeb98e7760b
XMTPReactNative: 048504b17f0a7f9380b48ddeda6bfb15f7a5d799
XMTP: 2cd39f7244fdeeb0a5f544da37e027cddc10df45
XMTPReactNative: 491b7689b36dacf6ca1a63d99e1481b8b3926dfa
Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2
Expand Down
27 changes: 22 additions & 5 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ test('can list all groups and conversations', async () => {

const listedContainers = await aliceClient.conversations.listAll()

// Verify informatioin in listed containers is correct
// Verify information in listed containers is correct
if (
listedContainers[0].topic !== bobGroup.topic ||
listedContainers[0].version !== ConversationVersion.GROUP ||
Expand Down Expand Up @@ -769,11 +769,28 @@ test('can pass a custom filter date and receive message objects with expected da
})

test('canMessage', async () => {
const bob = await Client.createRandom({ env: 'local' })
const alice = await Client.createRandom({ env: 'local' })
const bo = await Client.createRandom({ env: 'local' })
const alix = await Client.createRandom({ env: 'local' })

const canMessage = await bob.canMessage(alice.address)
return canMessage
const canMessage = await bo.canMessage(alix.address)
if (!canMessage) {
throw new Error('should be able to message v2 client')
}

const caro = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})
const chux = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})

const canMessageV3 = await caro.canGroupMessage([chux.address])
if (!canMessageV3) {
throw new Error('should be able to message v3 client')
}
return true
})

test('createFromKeyBundle throws error for non string value', async () => {
Expand Down
9 changes: 9 additions & 0 deletions ios/Wrappers/ConversationContainerWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ struct ConversationContainerWrapper {
return try ConversationWrapper.encodeToObj(conversation, client: client)
}
}

static func encode(_ conversation: XMTP.Conversation, client: XMTP.Client) throws -> String {
let obj = try encodeToObj(conversation, client: client)
let data = try JSONSerialization.data(withJSONObject: obj)
guard let result = String(data: data, encoding: .utf8) else {
throw WrapperError.encodeError("could not encode conversation")
}
return result
}
}
31 changes: 31 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ public class XMTPModule: Module {

return try await client.canMessage(peerAddress)
}

AsyncFunction("canGroupMessage") { (clientAddress: String, peerAddresses: [String]) -> Bool in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}

return try await client.canMessageV3(addresses: peerAddresses)
}

AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in
do {
Expand Down Expand Up @@ -298,6 +306,29 @@ public class XMTPModule: Module {
return results
}
}

AsyncFunction("listAll") { (clientAddress: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
let conversationContainerList = try await client.conversations.list(includeGroups: true)

return try await withThrowingTaskGroup(of: String.self) { taskGroup in
for conversation in conversationContainerList {
taskGroup.addTask {
await self.conversationsManager.set(conversation.cacheKey(clientAddress), conversation)
return try ConversationContainerWrapper.encode(conversation, client: client)
}
}

var results: [String] = []
for try await result in taskGroup {
results.append(result)
}

return results
}
}

AsyncFunction("loadMessages") { (clientAddress: String, topic: String, limit: Int?, before: Double?, after: Double?, direction: String?) -> [String] in
let beforeDate = before != nil ? Date(timeIntervalSince1970: TimeInterval(before!) / 1000) : nil
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Pod::Spec.new do |s|
s.source_files = "**/*.{h,m,swift}"
s.dependency 'secp256k1.swift'
s.dependency "MessagePacker"
s.dependency "XMTP", "= 0.8.5"
s.dependency "XMTP", "= 0.8.6"
end

0 comments on commit 1e298e9

Please sign in to comment.