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

feat: Rust V2 #219

Merged
merged 40 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e26fe3a
add v2 rust client to the client creation methods
nplasterer Mar 2, 2024
211f6a4
fix up how v2 is setup
nplasterer Apr 9, 2024
cb2986d
get it working for publish
nplasterer Apr 9, 2024
b6e4646
do it all for query
nplasterer Apr 9, 2024
49b6553
fix it all for batch query
nplasterer Apr 9, 2024
c2a37fe
try and get subscriptions working
nplasterer Apr 9, 2024
93dd97f
set the app version still
nplasterer Apr 9, 2024
7f347b8
add back the env
nplasterer Apr 9, 2024
895820b
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/v…
nplasterer Apr 10, 2024
0935df3
some subscribe tweaks
nplasterer Apr 10, 2024
36cfcb1
make tweaks to stream all
nplasterer Apr 10, 2024
520896a
fix up the linter
nplasterer Apr 10, 2024
e607128
fix up tests
nplasterer Apr 10, 2024
71e2b15
tweaks to example for better debugging
nplasterer Apr 10, 2024
40181b7
fix lint
nplasterer Apr 10, 2024
2bbeb13
revert subscription work
nplasterer Apr 11, 2024
bc7d5ad
small tweaks
nplasterer Apr 11, 2024
8cab289
confirm all streaming tests work
nplasterer Apr 11, 2024
99ba60c
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/v…
nplasterer Apr 11, 2024
9742bcf
fix up linter
nplasterer Apr 11, 2024
634456e
fix up linter and some flaking test
nplasterer Apr 11, 2024
009de37
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/v…
nplasterer Jun 24, 2024
22a77d8
Merge branch 'main' of https://github.com/xmtp/xmtp-android into np/v…
nplasterer Jun 26, 2024
b93e351
add hex util to make group comparison easier
nplasterer Jun 26, 2024
9e57578
update the readme
nplasterer Jun 26, 2024
7b4810a
modify the bindings
nplasterer Jun 26, 2024
0a3f7e8
getting closer on the subscribe work
nplasterer Jun 27, 2024
7550a1d
update all the subscribe functions
nplasterer Jun 27, 2024
ab7293a
try to figure out how to resubscribe to the callback
nplasterer Jun 28, 2024
f998cf7
get the stream all working
nplasterer Jun 28, 2024
7dfa3e5
a small amount of clean up
nplasterer Jun 28, 2024
3fd7109
all tests passing but 1
nplasterer Jun 28, 2024
7f11382
bad test
nplasterer Jun 28, 2024
37d3742
fix up the linter
nplasterer Jun 28, 2024
ea8bdbd
reformat
nplasterer Jun 28, 2024
52b553e
bring back env so that the app works
nplasterer Jun 28, 2024
0899d42
missing one iterator
nplasterer Jun 28, 2024
25a8a48
fix up some tests
nplasterer Jun 28, 2024
5dd425b
remove some runblocking
nplasterer Jun 29, 2024
2785b4d
feat: get it working performantly
nplasterer Jul 1, 2024
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 @@ -8,6 +8,7 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
Expand Down Expand Up @@ -182,6 +183,7 @@ class MainActivity : AppCompatActivity(),

private fun showError(message: String) {
val error = message.ifBlank { resources.getString(R.string.error) }
Log.e("MainActivity", message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want this log still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya it helps with logging errors in the example. Otherwise it just says "An Error Happened" with no details.

Toast.makeText(this, error, Toast.LENGTH_SHORT).show()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.xmtp.android.library

import androidx.test.ext.junit.runners.AndroidJUnit4
import app.cash.turbine.test
import com.google.protobuf.kotlin.toByteString
import com.google.protobuf.kotlin.toByteStringUtf8
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -588,11 +590,19 @@ class ConversationTest {

@Test
fun testCanStreamConversationsV2() = kotlinx.coroutines.test.runTest {
bobClient.conversations.stream().test {
val conversation = bobClient.conversations.newConversation(alice.walletAddress)
conversation.send(content = "hi")
assertEquals("hi", awaitItem().messages(limit = 1).first().body)
val flow = bobClient.conversations.stream()
val job = launch {
flow.catch { e ->
throw Exception("Error collecting flow: $e")
}.collect { convo ->
assert(convo.topic.isNotEmpty())
this.cancel()
}
}

bobClient.conversations.newConversation(alice.walletAddress)

job.join()
}

@Test
Expand All @@ -601,19 +611,37 @@ class ConversationTest {
fixtures.publishLegacyContact(client = bobClient)
fixtures.publishLegacyContact(client = aliceClient)
val conversation = aliceClient.conversations.newConversation(bob.walletAddress)
conversation.streamMessages().test {
conversation.send("hi alice")
assertEquals("hi alice", awaitItem().encodedContent.content.toStringUtf8())

val flow = conversation.streamMessages()
val job = launch {
flow.catch { e ->
throw Exception("Error collecting flow: $e")
}.collect { message ->
assertEquals("hi alice", message.encodedContent.content.toStringUtf8())
this.cancel()
}
}
}

conversation.send("hi alice")

job.join()
}
@Test
fun testStreamingMessagesFromV2Conversations() = kotlinx.coroutines.test.runTest {
val conversation = aliceClient.conversations.newConversation(bob.walletAddress)
conversation.streamMessages().test {
conversation.send("hi alice")
assertEquals("hi alice", awaitItem().encodedContent.content.toStringUtf8())
val flow = conversation.streamMessages()
val job = launch {
flow.catch { e ->
throw Exception("Error collecting flow: $e")
}.collect { message ->
assertEquals("hi alice", message.encodedContent.content.toStringUtf8())
this.cancel()
}
}

conversation.send("hi alice")

job.join()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,32 @@ class GroupTest {

@Test
fun testCanStreamGroupsAndConversations() = kotlinx.coroutines.test.runTest {
boClient.conversations.streamAll().test {
val group =
caroClient.conversations.newGroup(listOf(bo.walletAddress))
assertEquals(group.topic, awaitItem().topic)
val conversation =
alixClient.conversations.newConversation(bo.walletAddress)
assertEquals(conversation.topic, awaitItem().topic)
val flow = boClient.conversations.streamAll()
var counter = 0
val job = launch {
flow.catch { e ->
throw Exception("Error collecting flow: $e")
}.collect { convo ->
counter++
when (convo) {
is Conversation.Group -> {
assert(true)
}
is Conversation.V2 -> {
assert(true)
}
else -> {
assert(false)
}
}
if (counter == 2) this.cancel()
}
}

caroClient.conversations.newGroup(listOf(bo.walletAddress))
alixClient.conversations.newConversation(bo.walletAddress)

job.join()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.xmtp.proto.message.contents.Contact
import org.xmtp.proto.message.contents.InvitationV1Kt.context
import org.xmtp.proto.message.contents.PrivateKeyOuterClass
import org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKeyBundle
import uniffi.xmtpv3.createV2Client
import java.util.Date

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -63,9 +64,15 @@ class LocalInstrumentedTest {
val identity = PrivateKey.newBuilder().build().generate()
val authorized = alice.createIdentity(identity)
val authToken = authorized.createAuthToken()
val v2Client = runBlocking {
createV2Client(
host = XMTPEnvironment.LOCAL.getUrl(),
isSecure = false
)
}
val api = GRPCApiClient(
environment = XMTPEnvironment.LOCAL,
secure = false,
rustV2Client = v2Client
)
api.setAuthToken(authToken)
val encryptedBundle = authorized.toBundle.encrypted(alice)
Expand Down Expand Up @@ -93,9 +100,15 @@ class LocalInstrumentedTest {
val identity = PrivateKeyBuilder().getPrivateKey()
val authorized = aliceWallet.createIdentity(identity)
val authToken = authorized.createAuthToken()
val v2Client = runBlocking {
createV2Client(
host = XMTPEnvironment.LOCAL.getUrl(),
isSecure = false
)
}
val api = GRPCApiClient(
environment = XMTPEnvironment.LOCAL,
secure = false,
rustV2Client = v2Client
)
api.setAuthToken(authToken)
val encryptedBundle =
Expand Down Expand Up @@ -754,9 +767,15 @@ class LocalInstrumentedTest {
val identity = PrivateKey.newBuilder().build().generate()
val authorized = alice.createIdentity(identity)
val authToken = authorized.createAuthToken()
val v2Client = runBlocking {
createV2Client(
host = XMTPEnvironment.LOCAL.getUrl(),
isSecure = false
)
}
val api = GRPCApiClient(
environment = XMTPEnvironment.LOCAL,
secure = false,
rustV2Client = v2Client
)
api.setAuthToken(authToken)
val encryptedBundle = authorized.toBundle.encrypted(alice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class MessageTest {
}

@Test
@Ignore("Dev network flaky should be moved to local")
fun testCanLoadAllConversations() {
val ints = arrayOf(
105, 207, 193, 11, 240, 115, 115, 204,
Expand All @@ -233,6 +234,7 @@ class MessageTest {
}.build()
}.build()
val client = Client().create(account = PrivateKeyBuilder(key))
assertEquals(client.apiClient.environment, XMTPEnvironment.DEV)
val conversations = runBlocking { client.conversations.list() }
assertEquals(201, conversations.size)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ class FakeApiClient : ApiClient {
}.build()
}

override suspend fun publish(envelopes: List<MessageApiOuterClass.Envelope>): MessageApiOuterClass.PublishResponse {
override suspend fun publish(envelopes: List<MessageApiOuterClass.Envelope>) {
for (envelope in envelopes) {
send(envelope)
}
published.addAll(envelopes)
return PublishResponse.newBuilder().build()
PublishResponse.newBuilder().build()
}

override suspend fun subscribe(request: Flow<MessageApiOuterClass.SubscribeRequest>): Flow<MessageApiOuterClass.Envelope> {
override suspend fun subscribe(topics: List<String>): Flow<MessageApiOuterClass.Envelope> {
val env = stream.counts().first()

if (request.first().contentTopicsList.contains(env.contentTopic)) {
if (topics.contains(env.contentTopic)) {
return flowOf(env)
}
return flowOf()
Expand Down
Loading
Loading