Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make canMessage method accessible on the Client class #140

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading