From 90e1994129953dbd5447361801aa0c9e01857c0e Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 21 Mar 2024 17:40:14 -0700 Subject: [PATCH 1/2] fix: bump the gradle --- android/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/build.gradle b/android/build.gradle index 8e6983abb..52a58578c 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -98,7 +98,7 @@ repositories { dependencies { implementation project(':expo-modules-core') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}" - implementation "org.xmtp:android:0.8.4" + implementation "org.xmtp:android:0.9.0" implementation 'com.google.code.gson:gson:2.10.1' implementation 'com.facebook.react:react-native:0.71.3' implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1" From 77ed05b16759bd573b7686983ee1048e53b47d87 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 21 Mar 2024 17:47:22 -0700 Subject: [PATCH 2/2] make this threading better --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 05c50ac9e..d8a7b09bc 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -7,6 +7,7 @@ import android.util.Log import androidx.core.net.toUri import com.google.gson.JsonParser import com.google.protobuf.kotlin.toByteString +import expo.modules.kotlin.functions.Coroutine import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition import expo.modules.xmtpreactnativesdk.wrappers.ConsentWrapper @@ -24,6 +25,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext import org.json.JSONObject import org.xmtp.android.library.Client import org.xmtp.android.library.ClientOptions @@ -264,7 +266,7 @@ class XMTPModule : Module() { } // Export the conversation's serialized topic data. - AsyncFunction("exportConversationTopicData") { clientAddress: String, topic: String -> + AsyncFunction("exportConversationTopicData") Coroutine { clientAddress: String, topic: String -> logV("exportConversationTopicData") val conversation = findConversation(clientAddress, topic) ?: throw XMTPException("no conversation found for $topic") @@ -365,7 +367,7 @@ class XMTPModule : Module() { ).toJson() } - AsyncFunction("sendEncodedContent") { clientAddress: String, topic: String, encodedContentData: List -> + AsyncFunction("sendEncodedContent") Coroutine { clientAddress: String, topic: String, encodedContentData: List -> val conversation = findConversation( clientAddress = clientAddress, @@ -386,7 +388,7 @@ class XMTPModule : Module() { conversation.send(encodedContent = encodedContent) } - AsyncFunction("listConversations") { clientAddress: String -> + AsyncFunction("listConversations") Coroutine { clientAddress: String -> logV("listConversations") val client = clients[clientAddress] ?: throw XMTPException("No client") val conversationList = client.conversations.list() @@ -399,7 +401,7 @@ class XMTPModule : Module() { } } - AsyncFunction("loadMessages") { clientAddress: String, topic: String, limit: Int?, before: Long?, after: Long?, direction: String? -> + AsyncFunction("loadMessages") Coroutine { clientAddress: String, topic: String, limit: Int?, before: Long?, after: Long?, direction: String? -> logV("loadMessages") val conversation = findConversation( @@ -420,7 +422,7 @@ class XMTPModule : Module() { .map { DecodedMessageWrapper.encode(it) } } - AsyncFunction("loadBatchMessages") { clientAddress: String, topics: List -> + AsyncFunction("loadBatchMessages") Coroutine { clientAddress: String, topics: List -> logV("loadBatchMessages") val client = clients[clientAddress] ?: throw XMTPException("No client") val topicsList = mutableListOf>() @@ -465,7 +467,7 @@ class XMTPModule : Module() { .map { DecodedMessageWrapper.encode(it) } } - AsyncFunction("sendMessage") { clientAddress: String, conversationTopic: String, contentJson: String -> + AsyncFunction("sendMessage") Coroutine { clientAddress: String, conversationTopic: String, contentJson: String -> logV("sendMessage") val conversation = findConversation( @@ -480,7 +482,7 @@ class XMTPModule : Module() { ) } - AsyncFunction("prepareMessage") { clientAddress: String, conversationTopic: String, contentJson: String -> + AsyncFunction("prepareMessage") Coroutine { clientAddress: String, conversationTopic: String, contentJson: String -> logV("prepareMessage") val conversation = findConversation( @@ -503,7 +505,7 @@ class XMTPModule : Module() { ).toJson() } - AsyncFunction("prepareEncodedMessage") { clientAddress: String, conversationTopic: String, encodedContentData: List -> + AsyncFunction("prepareEncodedMessage") Coroutine { clientAddress: String, conversationTopic: String, encodedContentData: List -> logV("prepareEncodedMessage") val conversation = findConversation( @@ -536,7 +538,7 @@ class XMTPModule : Module() { ).toJson() } - AsyncFunction("sendPreparedMessage") { clientAddress: String, preparedLocalMessageJson: String -> + AsyncFunction("sendPreparedMessage") Coroutine { clientAddress: String, preparedLocalMessageJson: String -> logV("sendPreparedMessage") val client = clients[clientAddress] ?: throw XMTPException("No client") val local = PreparedLocalMessage.fromJson(preparedLocalMessageJson) @@ -554,7 +556,7 @@ class XMTPModule : Module() { prepared.messageId } - AsyncFunction("createConversation") { clientAddress: String, peerAddress: String, contextJson: String -> + AsyncFunction("createConversation") Coroutine { clientAddress: String, peerAddress: String, contextJson: String -> logV("createConversation: $contextJson") val client = clients[clientAddress] ?: throw XMTPException("No client") val context = JsonParser.parseString(contextJson).asJsonObject @@ -591,7 +593,7 @@ class XMTPModule : Module() { subscribeToAllMessages(clientAddress = clientAddress) } - AsyncFunction("subscribeToMessages") { clientAddress: String, topic: String -> + AsyncFunction("subscribeToMessages") Coroutine { clientAddress: String, topic: String -> logV("subscribeToMessages") subscribeToMessages( clientAddress = clientAddress, @@ -609,7 +611,7 @@ class XMTPModule : Module() { subscriptions[getMessagesKey(clientAddress)]?.cancel() } - AsyncFunction("unsubscribeFromMessages") { clientAddress: String, topic: String -> + AsyncFunction("unsubscribeFromMessages") Coroutine { clientAddress: String, topic: String -> logV("unsubscribeFromMessages") unsubscribeFromMessages( clientAddress = clientAddress, @@ -633,7 +635,7 @@ class XMTPModule : Module() { } } - AsyncFunction("decodeMessage") { clientAddress: String, topic: String, encryptedMessage: String -> + AsyncFunction("decodeMessage") Coroutine { clientAddress: String, topic: String, encryptedMessage: String -> logV("decodeMessage") val encryptedMessageData = Base64.decode(encryptedMessage, NO_WRAP) val envelope = EnvelopeBuilder.buildFromString(topic, Date(), encryptedMessageData) @@ -659,13 +661,13 @@ class XMTPModule : Module() { client.contacts.isDenied(address) } - AsyncFunction("denyContacts") { clientAddress: String, addresses: List -> + AsyncFunction("denyContacts") Coroutine { clientAddress: String, addresses: List -> logV("denyContacts") val client = clients[clientAddress] ?: throw XMTPException("No client") client.contacts.deny(addresses) } - AsyncFunction("allowContacts") { clientAddress: String, addresses: List -> + AsyncFunction("allowContacts") Coroutine { clientAddress: String, addresses: List -> val client = clients[clientAddress] ?: throw XMTPException("No client") client.contacts.allow(addresses) } @@ -676,7 +678,7 @@ class XMTPModule : Module() { consentList.entries.map { ConsentWrapper.encode(it.value) } } - AsyncFunction("conversationConsentState") { clientAddress: String, conversationTopic: String -> + AsyncFunction("conversationConsentState") Coroutine { clientAddress: String, conversationTopic: String -> val conversation = findConversation(clientAddress, conversationTopic) ?: throw XMTPException("no conversation found for $conversationTopic") consentStateToString(conversation.consentState()) @@ -702,7 +704,7 @@ class XMTPModule : Module() { // Helpers // - private fun findConversation( + private suspend fun findConversation( clientAddress: String, topic: String, ): Conversation? { @@ -775,7 +777,7 @@ class XMTPModule : Module() { } } - private fun subscribeToMessages(clientAddress: String, topic: String) { + private suspend fun subscribeToMessages(clientAddress: String, topic: String) { val conversation = findConversation( clientAddress = clientAddress, @@ -809,7 +811,7 @@ class XMTPModule : Module() { return "conversations:$clientAddress" } - private fun unsubscribeFromMessages( + private suspend fun unsubscribeFromMessages( clientAddress: String, topic: String, ) {