Skip to content

Commit

Permalink
Add new fetch methods for V3 local DB (#262)
Browse files Browse the repository at this point in the history
* add new fetch methods

* add request message history sync

* write tests for the functionality

* fix up the test

* fix the linter
  • Loading branch information
nplasterer authored Jun 24, 2024
1 parent aa74545 commit 0b6f4fa
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,33 @@ class ClientTest {
assertEquals(boClient.conversations.listGroups().size, 1)
}
}

@Test
fun testCanGetAnInboxIdFromAddress() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val alixWallet = PrivateKeyBuilder()
val boWallet = PrivateKeyBuilder()
val alixClient =
Client().create(
account = alixWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableV3 = true,
appContext = context
)
)
val boClient =
Client().create(
account = boWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableV3 = true,
appContext = context
)
)
val boInboxId = runBlocking {
alixClient.inboxIdFromAddress(boClient.address)
}
assertEquals(boClient.inboxId, boInboxId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.web3j.utils.Numeric
import org.xmtp.android.library.codecs.ContentTypeGroupUpdated
import org.xmtp.android.library.codecs.ContentTypeReaction
import org.xmtp.android.library.codecs.GroupUpdatedCodec
import org.xmtp.android.library.codecs.Reaction
import org.xmtp.android.library.codecs.ReactionAction
import org.xmtp.android.library.codecs.ReactionCodec
import org.xmtp.android.library.codecs.ReactionSchema
import org.xmtp.android.library.codecs.id
import org.xmtp.android.library.messages.MessageDeliveryStatus
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
Expand Down Expand Up @@ -789,4 +789,39 @@ class GroupTest {
assert(!boClient.contacts.isInboxAllowed(alixClient.inboxId))
assert(boClient.contacts.isInboxDenied(alixClient.inboxId))
}

@Test
fun testCanFetchGroupById() {
val boGroup = runBlocking {
boClient.conversations.newGroup(
listOf(
alix.walletAddress,
caro.walletAddress
)
)
}
runBlocking { alixClient.conversations.syncGroups() }
val alixGroup = alixClient.findGroup(boGroup.id)

assertEquals(alixGroup?.id?.toHex(), boGroup.id.toHex())
}

@Test
fun testCanFetchMessageById() {
val boGroup = runBlocking {
boClient.conversations.newGroup(
listOf(
alix.walletAddress,
caro.walletAddress
)
)
}
val boMessageId = runBlocking { boGroup.send("Hello") }
runBlocking { alixClient.conversations.syncGroups() }
val alixGroup = alixClient.findGroup(boGroup.id)
runBlocking { alixGroup?.sync() }
val alixMessage = alixClient.findMessage(Numeric.hexStringToByteArray(boMessageId))

assertEquals(alixMessage?.id?.toHex(), boMessageId)
}
}
4 changes: 2 additions & 2 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 43e21e0d
Version: 1e096906
Branch: main
Date: 2024-06-20 20:35:11 +0000
Date: 2024-06-24 17:58:00 +0000
38 changes: 37 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.web3j.crypto.Keys.toChecksumAddress
import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.TextCodec
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.libxmtp.XMTPLogger
import org.xmtp.android.library.messages.ContactBundle
import org.xmtp.android.library.messages.EncryptedPrivateKeyBundle
Expand Down Expand Up @@ -71,6 +72,7 @@ data class ClientOptions(
val enableV3: Boolean = false,
val dbDirectory: String? = null,
val dbEncryptionKey: ByteArray? = null,
val historySyncUrl: String? = null,
) {
data class Api(
val env: XMTPEnvironment = XMTPEnvironment.DEV,
Expand Down Expand Up @@ -368,7 +370,8 @@ class Client() {
accountAddress = accountAddress,
inboxId = inboxId,
nonce = 0.toULong(),
legacySignedPrivateKeyProto = privateKeyBundleV1.toV2().identityKey.toByteArray()
legacySignedPrivateKeyProto = privateKeyBundleV1.toV2().identityKey.toByteArray(),
historySyncUrl = options.historySyncUrl
)
} else {
null
Expand Down Expand Up @@ -515,6 +518,28 @@ class Client() {
}
}

fun findGroup(groupId: ByteArray): Group? {
v3Client?.let {
try {
return Group(this, it.group(groupId))
} catch (e: Exception) {
return null
}
}
throw XMTPException("Error no V3 client initialized")
}

fun findMessage(messageId: ByteArray): MessageV3? {
v3Client?.let {
try {
return MessageV3(this, it.message(messageId))
} catch (e: Exception) {
return null
}
}
throw XMTPException("Error no V3 client initialized")
}

suspend fun publish(envelopes: List<Envelope>): PublishResponse {
val authorized = AuthorizedIdentity(
address = address,
Expand Down Expand Up @@ -609,6 +634,13 @@ class Client() {
throw XMTPException("Error no V3 client initialized")
}

suspend fun inboxIdFromAddress(address: String): String? {
v3Client?.let {
return it.findInboxId(address.lowercase())
}
throw XMTPException("Error no V3 client initialized")
}

fun deleteLocalDatabase() {
File(dbPath).delete()
}
Expand All @@ -624,6 +656,10 @@ class Client() {
v3Client?.dbReconnect() ?: throw XMTPException("Error no V3 client initialized")
}

suspend fun requestMessageHistorySync() {
v3Client?.requestHistorySync() ?: throw XMTPException("Error no V3 client initialized")
}

val privateKeyBundle: PrivateKeyBundle
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)

Expand Down
Loading

0 comments on commit 0b6f4fa

Please sign in to comment.