Skip to content

Commit

Permalink
Group Can Message Hash Map (#235)
Browse files Browse the repository at this point in the history
* add generated rust

* implement the new can message function

* fix up the tests

* add back the iterators

* update the jni libs

* update readme

* fix up the test

* fix the linter
  • Loading branch information
nplasterer authored Apr 24, 2024
1 parent b9dc5ff commit 0654665
Show file tree
Hide file tree
Showing 10 changed files with 1,682 additions and 902 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.xmtp.android.library
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
Expand Down Expand Up @@ -90,7 +91,10 @@ class ClientTest {
)
val client =
Client().create(account = fakeWallet, options = options)
assert(client.canMessageV3(listOf(client.address)))

runBlocking {
client.canMessageV3(listOf(client.address))[client.address]?.let { assert(it) }
}

val bundle = client.privateKeyBundle
val clientFromV1Bundle =
Expand All @@ -101,7 +105,9 @@ class ClientTest {
clientFromV1Bundle.privateKeyBundleV1.identityKey,
)

assert(clientFromV1Bundle.canMessageV3(listOf(client.address)))
runBlocking {
clientFromV1Bundle.canMessageV3(listOf(client.address))[client.address]?.let { assert(it) }
}

assertEquals(
client.address,
Expand All @@ -122,7 +128,9 @@ class ClientTest {
appContext = context
)
)
assert(client.canMessageV3(listOf(client.address)))
runBlocking {
client.canMessageV3(listOf(client.address))[client.address]?.let { assert(it) }
}
assert(client.installationId.isNotEmpty())
}

Expand Down Expand Up @@ -187,14 +195,20 @@ class ClientTest {
appContext = context
)
)
assert(client.canMessageV3(listOf(client.address)))
runBlocking {
client.canMessageV3(listOf(client.address))[client.address]?.let { assert(it) }
}
}

@Test
fun testDoesNotCreateAV3Client() {
val fakeWallet = PrivateKeyBuilder()
val client = Client().create(account = fakeWallet)
assert(!client.canMessageV3(listOf(client.address)))
Assert.assertThrows("Error no V3 client initialized", XMTPException::class.java) {
runBlocking {
client.canMessageV3(listOf(client.address))[client.address]?.let { assert(!it) }
}
}
}

@Test
Expand Down
5 changes: 3 additions & 2 deletions library/src/main/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Kotlin code emitted by the `bindings_ffi` crate in [libxmtp](https://github.com/
2. Navigate to the `bindings_ffi` folder
3. Follow the instructions for "Rebuilding this crate in the `bindings_ffi` [README](https://github.com/xmtp/libxmtp/tree/main/bindings_ffi#rebuilding-this-crate)"
4. Copy the contents of `libxmtp/bindings_ffi/src/uniffi/xmtpv3/xmtpv3.kt` to `xmtp-android/library/src/main/java/xmtpv3.kt`
5. All instances of `value.forEach` should be changed to `value.iterator().forEach` to be compatible with API 23
6. Copy the jniLibs from `libxmtp/bindings_ffi/jniLibs` to `xmtp-android/library/src/main/jniLibs`
5. Run format (cmd + opt + l) function to keep the code format consistent and diff small
6. All instances of `value.forEach` should be changed to `value.iterator().forEach` to be compatible with API 23
7. Copy the jniLibs from `libxmtp/bindings_ffi/jniLibs` to `xmtp-android/library/src/main/jniLibs`

You should now be on the latest libxmtp. Tests will fail if the jniLibs do not match the version of xmtpv3.
6 changes: 3 additions & 3 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 40dedd8
Branch: HEAD
Date: 2024-04-23 22:24:01 +0000
Version: 49f945a
Branch: main
Date: 2024-04-24 17:02:27 +0000
9 changes: 4 additions & 5 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,11 @@ class Client() {
return runBlocking { query(Topic.contact(peerAddress)).envelopesList.size > 0 }
}

fun canMessageV3(addresses: List<String>): Boolean {
if (libXMTPClient == null) return false
val statuses = runBlocking {
libXMTPClient!!.canMessage(addresses)
suspend fun canMessageV3(addresses: List<String>): Map<String, Boolean> {
libXMTPClient?.let {
return it.canMessage(addresses)
}
return !statuses.contains(false)
throw XMTPException("Error no V3 client initialized")
}

fun deleteLocalDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ data class Conversations(
) {
throw XMTPException("Recipient is sender")
}
if (!client.canMessageV3(accountAddresses)) {
throw XMTPException("Recipient not on network")
val falseAddresses =
client.canMessageV3(accountAddresses).filter { !it.value }.map { it.key }
if (falseAddresses.isNotEmpty()) {
throw XMTPException("${falseAddresses.joinToString()} not on network")
}

val group =
Expand Down
Loading

0 comments on commit 0654665

Please sign in to comment.