Skip to content

Commit

Permalink
List of inbox states for inbox ids (#324)
Browse files Browse the repository at this point in the history
* add inbox states to the client

* write tests for it
  • Loading branch information
nplasterer authored Nov 13, 2024
1 parent fb35fad commit 078f32b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
18 changes: 15 additions & 3 deletions library/src/androidTest/java/org/xmtp/android/library/DmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.findDm(caro.walletAddress)
val alixDm = boClient.findDm(alix.walletAddress)
val caroDm = boClient.findDmByAddress(caro.walletAddress)
val alixDm = boClient.findDmByAddress(alix.walletAddress)
assertNull(alixDm)
assertEquals(caroDm?.id, dm.id)
}
Expand Down Expand Up @@ -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.findDm(bo.walletAddress)
val alixDm = alixClient.findDmByAddress(bo.walletAddress)
group.streamMessages().test {
alixDm?.send("hi")
assertEquals("hi", awaitItem().body)
Expand Down
17 changes: 14 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,20 @@ class Client() {
}
}

suspend fun findDm(address: String): Dm? {
val inboxId =
inboxIdFromAddress(address.lowercase()) ?: throw XMTPException("No inboxId present")
fun findDmByInboxId(inboxId: String): Dm? {
return try {
Dm(this, ffiClient.dmConversation(inboxId))
} catch (e: Exception) {
null
}
}

suspend fun findDmByAddress(address: String): Dm? {
val inboxId =
inboxIdFromAddress(address.lowercase()) ?: throw XMTPException("No inboxId present")
return findDmByInboxId(inboxId)
}

fun findMessage(messageId: String): Message? {
return try {
Message(this, ffiClient.message(messageId.hexToByteArray()))
Expand Down Expand Up @@ -306,6 +310,13 @@ class Client() {
}
}

suspend fun inboxStatesForInboxIds(
refreshFromNetwork: Boolean,
inboxIds: List<String>,
): List<InboxState> {
return ffiClient.addressesFromInboxId(refreshFromNetwork, inboxIds).map { InboxState(it) }
}

suspend fun inboxState(refreshFromNetwork: Boolean): InboxState {
return InboxState(ffiClient.inboxState(refreshFromNetwork))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.findDmByAddress(peerAddress)
if (dm == null) {
val dmConversation = ffiConversations.createDm(peerAddress.lowercase())
dm = Dm(client, dmConversation)
Expand Down

0 comments on commit 078f32b

Please sign in to comment.