Skip to content

Commit 3c6594f

Browse files
kele-leanesgiovasdistillerynplasterer
authored
make canMessage method accessible on the Client class (#140)
* expose canMessage method * improve publicCanMessage test * Update canMessage function to work with an instance of ApiClient * fix up the test and check * one more small tweak to the tests --------- Co-authored-by: Giovani Gonzalez <giovani.gonzalez@distillery.com> Co-authored-by: Naomi Plasterer <naomi@xmtp.com>
1 parent e5b8995 commit 3c6594f

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt

+21-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ClientTest {
3636
val client = Client().buildFrom(v1Copy)
3737
assertEquals(
3838
wallet.address,
39-
client.address
39+
client.address,
4040
)
4141
}
4242

@@ -49,11 +49,11 @@ class ClientTest {
4949
assertEquals(client.address, clientFromV1Bundle.address)
5050
assertEquals(
5151
client.privateKeyBundleV1?.identityKey,
52-
clientFromV1Bundle.privateKeyBundleV1?.identityKey
52+
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
5353
)
5454
assertEquals(
5555
client.privateKeyBundleV1?.preKeysList,
56-
clientFromV1Bundle.privateKeyBundleV1?.preKeysList
56+
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
5757
)
5858
}
5959

@@ -66,13 +66,14 @@ class ClientTest {
6666
assertEquals(client.address, clientFromV1Bundle.address)
6767
assertEquals(
6868
client.privateKeyBundleV1?.identityKey,
69-
clientFromV1Bundle.privateKeyBundleV1?.identityKey
69+
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
7070
)
7171
assertEquals(
7272
client.privateKeyBundleV1?.preKeysList,
73-
clientFromV1Bundle.privateKeyBundleV1?.preKeysList
73+
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
7474
)
7575
}
76+
7677
@Test
7778
fun testCanMessage() {
7879
val fixtures = fixtures()
@@ -82,4 +83,19 @@ class ClientTest {
8283
assert(canMessage)
8384
assert(!cannotMessage)
8485
}
86+
87+
@Test
88+
fun testPublicCanMessage() {
89+
val aliceWallet = PrivateKeyBuilder()
90+
val notOnNetwork = PrivateKeyBuilder()
91+
val opts = ClientOptions(ClientOptions.Api(XMTPEnvironment.LOCAL, false))
92+
val aliceClient = Client().create(aliceWallet, opts)
93+
aliceClient.ensureUserContactPublished()
94+
95+
val canMessage = Client.canMessage(aliceWallet.address, opts)
96+
val cannotMessage = Client.canMessage(notOnNetwork.address, opts)
97+
98+
assert(canMessage)
99+
assert(!cannotMessage)
100+
}
85101
}

library/src/main/java/org/xmtp/android/library/ApiClient.kt

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ data class GRPCApiClient(
189189

190190
return client.subscribe(request, headers)
191191
}
192+
192193
override suspend fun subscribe2(request: Flow<SubscribeRequest>): Flow<Envelope> {
193194
val headers = Metadata()
194195

library/src/main/java/org/xmtp/android/library/Client.kt

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ class Client() {
114114
)
115115
)
116116
}
117+
118+
fun canMessage(peerAddress: String, options: ClientOptions? = null): Boolean {
119+
val clientOptions = options ?: ClientOptions()
120+
val api = GRPCApiClient(
121+
environment = clientOptions.api.env,
122+
secure = clientOptions.api.isSecure
123+
)
124+
return runBlocking {
125+
val topics = api.queryTopic(Topic.contact(peerAddress)).envelopesList
126+
topics.isNotEmpty()
127+
}
128+
}
117129
}
118130

119131
constructor(

0 commit comments

Comments
 (0)