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

Send/Receive Encoded Content #63

Merged
merged 21 commits into from
Jun 30, 2023
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
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.3.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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import expo.modules.kotlin.modules.ModuleDefinition
import expo.modules.xmtpreactnativesdk.wrappers.ConversationWithClientAddress
import expo.modules.xmtpreactnativesdk.wrappers.ConversationWrapper
import expo.modules.xmtpreactnativesdk.wrappers.DecodedMessageWrapper
import expo.modules.xmtpreactnativesdk.wrappers.EncodedMessageWrapper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -26,8 +27,9 @@ import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.Signature
import org.xmtp.android.library.push.XMTPPush
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
import org.xmtp.proto.message.contents.SignatureOuterClass
import org.xmtp.proto.message.contents.Content.EncodedContent
import org.xmtp.proto.message.contents.PrivateKeyOuterClass
import org.xmtp.proto.message.contents.SignatureOuterClass
import java.util.Date
import java.util.UUID
import kotlin.coroutines.Continuation
Expand Down Expand Up @@ -192,23 +194,29 @@ class XMTPModule : Module() {
val afterDate = if (after != null) Date(after) else null

client.conversations.listBatchMessages(topics, limit, beforeDate, afterDate).map {
DecodedMessageWrapper.encode(it)
EncodedMessageWrapper.encode(it)
}
}

// TODO: Support content types
AsyncFunction("sendMessage") { clientAddress: String, conversationTopic: String, conversationID: String?, content: String ->
AsyncFunction("sendEncodedContentData") { clientAddress: String, conversationTopic: String, conversationID: String?, content: List<Int> ->
logV("sendMessage")
val conversation =
findConversation(
clientAddress = clientAddress,
topic = conversationTopic
)
?: throw XMTPException("no conversation found for $conversationTopic")
val preparedMessage = conversation.prepareMessage(content = content)
val decodedMessage = preparedMessage.decodedMessage()
preparedMessage.send()
DecodedMessageWrapper.encode(decodedMessage)

val contentData = content.foldIndexed(ByteArray(content.size)) { i, a, v ->
a.apply {
set(
i,
v.toByte()
)
}
}
val encodedContent = EncodedContent.parseFrom(contentData)
conversation.send(encodedContent = encodedContent)
}

AsyncFunction("createConversation") { clientAddress: String, peerAddress: String, conversationID: String? ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package expo.modules.xmtpreactnativesdk.wrappers

import com.daveanthonythomas.moshipack.MoshiPack
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableNativeArray
import org.xmtp.android.library.DecodedMessage

class EncodedMessageWrapper {

companion object {
fun encode(model: DecodedMessage): ReadableArray {
val message = mapOf(
"id" to model.id,
"content" to model.encodedContent.toByteArray(),
"senderAddress" to model.senderAddress.toByteArray(),
"sent" to model.sent.time.toString()
)
val encodedContent = MoshiPack().pack(message).readByteArray()

val byteArray = WritableNativeArray()
encodedContent.forEach {
byteArray.pushString(it.toString())
}
return byteArray
}
}
}
16 changes: 11 additions & 5 deletions example/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
const path = require('path');
const path = require("path");
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
presets: ["babel-preset-expo"],
plugins: [
[
'module-resolver',
"module-resolver",
{
extensions: ['.tsx', '.ts', '.js', '.json'],
extensions: [".tsx", ".ts", ".js", ".json"],
alias: {
// For development, we want to alias the library to the source
'xmtp-react-native-sdk': path.join(__dirname, '..', 'src', 'index.ts'),
"xmtp-react-native-sdk": path.join(
__dirname,
"..",
"src",
"index.ts"
),
},
},
],
"@babel/plugin-proposal-export-namespace-from",
],
};
};
Loading