From 51f4de9c9f52a30477cf138f441cbb9002de1898 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 13 Nov 2024 08:46:48 -0800 Subject: [PATCH 1/2] add inbox states to the client --- .../java/org/xmtp/android/library/DmTest.kt | 6 +++--- .../java/org/xmtp/android/library/Client.kt | 17 ++++++++++++++--- .../org/xmtp/android/library/Conversations.kt | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt b/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt index b61b80db6..db7c390c0 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt @@ -65,8 +65,8 @@ class DmTest { runBlocking { val dm = boClient.conversations.findOrCreateDm(caro.walletAddress) - val caroDm = boClient.findDm(caro.walletAddress) - val alixDm = boClient.findDm(alix.walletAddress) + val caroDm = boClient.findDmFromAddress(caro.walletAddress) + val alixDm = boClient.findDmFromAddress(alix.walletAddress) assertNull(alixDm) assertEquals(caroDm?.id, dm.id) } @@ -253,7 +253,7 @@ class DmTest { fun testCanStreamDmMessages() = kotlinx.coroutines.test.runTest { val group = boClient.conversations.findOrCreateDm(alix.walletAddress.lowercase()) alixClient.conversations.sync() - val alixDm = alixClient.findDm(bo.walletAddress) + val alixDm = alixClient.findDmFromAddress(bo.walletAddress) group.streamMessages().test { alixDm?.send("hi") assertEquals("hi", awaitItem().body) diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index dd4e16ad0..ef19db408 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -248,9 +248,7 @@ class Client() { } } - suspend fun findDm(address: String): Dm? { - val inboxId = - inboxIdFromAddress(address.lowercase()) ?: throw XMTPException("No inboxId present") + fun findDmFromInboxId(inboxId: String): Dm? { return try { Dm(this, ffiClient.dmConversation(inboxId)) } catch (e: Exception) { @@ -258,6 +256,12 @@ class Client() { } } + suspend fun findDmFromAddress(address: String): Dm? { + val inboxId = + inboxIdFromAddress(address.lowercase()) ?: throw XMTPException("No inboxId present") + return findDmFromInboxId(inboxId) + } + fun findMessage(messageId: String): Message? { return try { Message(this, ffiClient.message(messageId.hexToByteArray())) @@ -306,6 +310,13 @@ class Client() { } } + suspend fun inboxStatesForInboxIds( + refreshFromNetwork: Boolean, + inboxIds: List, + ): List { + return ffiClient.addressesFromInboxId(refreshFromNetwork, inboxIds).map { InboxState(it) } + } + suspend fun inboxState(refreshFromNetwork: Boolean): InboxState { return InboxState(ffiClient.inboxState(refreshFromNetwork)) } diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index 30604b110..c42279d97 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -146,7 +146,7 @@ data class Conversations( if (falseAddresses.isNotEmpty()) { throw XMTPException("${falseAddresses.joinToString()} not on network") } - var dm = client.findDm(peerAddress) + var dm = client.findDmFromAddress(peerAddress) if (dm == null) { val dmConversation = ffiConversations.createDm(peerAddress.lowercase()) dm = Dm(client, dmConversation) From afe3a6b69749799896083b59d2c18dafc1f62788 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 13 Nov 2024 08:56:44 -0800 Subject: [PATCH 2/2] write tests for it --- .../org/xmtp/android/library/ClientTest.kt | 14 ++++++++++++++ .../java/org/xmtp/android/library/DmTest.kt | 18 +++++++++++++++--- .../java/org/xmtp/android/library/Client.kt | 6 +++--- .../org/xmtp/android/library/Conversations.kt | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt index 59bcb4956..45b0eb209 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -9,6 +9,7 @@ import org.junit.Assert.fail import org.junit.Test import org.junit.runner.RunWith import org.xmtp.android.library.messages.PrivateKeyBuilder +import org.xmtp.android.library.messages.walletAddress import uniffi.xmtpv3.GenericException import java.security.SecureRandom import java.util.concurrent.CompletableFuture @@ -320,4 +321,17 @@ class ClientTest { state = runBlocking { alixClient3.inboxState(true) } assertEquals(state.installations.size, 1) } + + @Test + fun testsCanFindOthersInboxStates() { + val fixtures = fixtures() + val states = runBlocking { + fixtures.alixClient.inboxStatesForInboxIds( + true, + listOf(fixtures.boClient.inboxId, fixtures.caroClient.inboxId) + ) + } + assertEquals(states.first().recoveryAddress.lowercase(), fixtures.bo.walletAddress.lowercase()) + assertEquals(states.last().recoveryAddress.lowercase(), fixtures.caro.walletAddress.lowercase()) + } } diff --git a/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt b/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt index db7c390c0..c1b144253 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/DmTest.kt @@ -60,13 +60,25 @@ class DmTest { } } + @Test + fun testsCanFindDmByInboxId() { + runBlocking { + val dm = boClient.conversations.findOrCreateDm(caro.walletAddress) + + val caroDm = boClient.findDmByInboxId(caroClient.inboxId) + val alixDm = boClient.findDmByInboxId(alixClient.inboxId) + assertNull(alixDm) + assertEquals(caroDm?.id, dm.id) + } + } + @Test fun testsCanFindDmByAddress() { runBlocking { val dm = boClient.conversations.findOrCreateDm(caro.walletAddress) - val caroDm = boClient.findDmFromAddress(caro.walletAddress) - val alixDm = boClient.findDmFromAddress(alix.walletAddress) + val caroDm = boClient.findDmByAddress(caro.walletAddress) + val alixDm = boClient.findDmByAddress(alix.walletAddress) assertNull(alixDm) assertEquals(caroDm?.id, dm.id) } @@ -253,7 +265,7 @@ class DmTest { fun testCanStreamDmMessages() = kotlinx.coroutines.test.runTest { val group = boClient.conversations.findOrCreateDm(alix.walletAddress.lowercase()) alixClient.conversations.sync() - val alixDm = alixClient.findDmFromAddress(bo.walletAddress) + val alixDm = alixClient.findDmByAddress(bo.walletAddress) group.streamMessages().test { alixDm?.send("hi") assertEquals("hi", awaitItem().body) diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index ef19db408..38636af1d 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -248,7 +248,7 @@ class Client() { } } - fun findDmFromInboxId(inboxId: String): Dm? { + fun findDmByInboxId(inboxId: String): Dm? { return try { Dm(this, ffiClient.dmConversation(inboxId)) } catch (e: Exception) { @@ -256,10 +256,10 @@ class Client() { } } - suspend fun findDmFromAddress(address: String): Dm? { + suspend fun findDmByAddress(address: String): Dm? { val inboxId = inboxIdFromAddress(address.lowercase()) ?: throw XMTPException("No inboxId present") - return findDmFromInboxId(inboxId) + return findDmByInboxId(inboxId) } fun findMessage(messageId: String): Message? { diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index c42279d97..84199873d 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -146,7 +146,7 @@ data class Conversations( if (falseAddresses.isNotEmpty()) { throw XMTPException("${falseAddresses.joinToString()} not on network") } - var dm = client.findDmFromAddress(peerAddress) + var dm = client.findDmByAddress(peerAddress) if (dm == null) { val dmConversation = ffiConversations.createDm(peerAddress.lowercase()) dm = Dm(client, dmConversation)