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

List of inbox states for inbox ids #324

Merged
merged 2 commits into from
Nov 13, 2024
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 @@ -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
Loading