Skip to content

Commit

Permalink
make canMessage method accessible on the Client class (#140)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Naomi Plasterer <[email protected]>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent e5b8995 commit 3c6594f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ClientTest {
val client = Client().buildFrom(v1Copy)
assertEquals(
wallet.address,
client.address
client.address,
)
}

Expand All @@ -49,11 +49,11 @@ class ClientTest {
assertEquals(client.address, clientFromV1Bundle.address)
assertEquals(
client.privateKeyBundleV1?.identityKey,
clientFromV1Bundle.privateKeyBundleV1?.identityKey
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
)
assertEquals(
client.privateKeyBundleV1?.preKeysList,
clientFromV1Bundle.privateKeyBundleV1?.preKeysList
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
)
}

Expand All @@ -66,13 +66,14 @@ class ClientTest {
assertEquals(client.address, clientFromV1Bundle.address)
assertEquals(
client.privateKeyBundleV1?.identityKey,
clientFromV1Bundle.privateKeyBundleV1?.identityKey
clientFromV1Bundle.privateKeyBundleV1?.identityKey,
)
assertEquals(
client.privateKeyBundleV1?.preKeysList,
clientFromV1Bundle.privateKeyBundleV1?.preKeysList
clientFromV1Bundle.privateKeyBundleV1?.preKeysList,
)
}

@Test
fun testCanMessage() {
val fixtures = fixtures()
Expand All @@ -82,4 +83,19 @@ class ClientTest {
assert(canMessage)
assert(!cannotMessage)
}

@Test
fun testPublicCanMessage() {
val aliceWallet = PrivateKeyBuilder()
val notOnNetwork = PrivateKeyBuilder()
val opts = ClientOptions(ClientOptions.Api(XMTPEnvironment.LOCAL, false))
val aliceClient = Client().create(aliceWallet, opts)
aliceClient.ensureUserContactPublished()

val canMessage = Client.canMessage(aliceWallet.address, opts)
val cannotMessage = Client.canMessage(notOnNetwork.address, opts)

assert(canMessage)
assert(!cannotMessage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ data class GRPCApiClient(

return client.subscribe(request, headers)
}

override suspend fun subscribe2(request: Flow<SubscribeRequest>): Flow<Envelope> {
val headers = Metadata()

Expand Down
12 changes: 12 additions & 0 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class Client() {
)
)
}

fun canMessage(peerAddress: String, options: ClientOptions? = null): Boolean {
val clientOptions = options ?: ClientOptions()
val api = GRPCApiClient(
environment = clientOptions.api.env,
secure = clientOptions.api.isSecure
)
return runBlocking {
val topics = api.queryTopic(Topic.contact(peerAddress)).envelopesList
topics.isNotEmpty()
}
}
}

constructor(
Expand Down

0 comments on commit 3c6594f

Please sign in to comment.