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

Android side of custom content types #156

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.6.12"
implementation "org.xmtp:android:0.6.13"
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ class XMTPModule : Module() {
filename = attachment.filename
).toJson()
}

AsyncFunction("sendEncodedContent") { clientAddress: String, topic: String, encodedContentData: ByteArray ->
val conversation =
findConversation(
clientAddress = clientAddress,
topic = topic
) ?: throw XMTPException("no conversation found for $topic")

val encodedContent = EncodedContent.parseFrom(encodedContentData.toByteString())

conversation.send(encodedContent = encodedContent)
}

AsyncFunction("listConversations") { clientAddress: String ->
logV("listConversations")
Expand All @@ -288,7 +300,7 @@ class XMTPModule : Module() {
val beforeDate = if (before != null) Date(before) else null
val afterDate = if (after != null) Date(after) else null

conversation.messages(
conversation.decryptedMessages(
limit = limit,
before = beforeDate,
after = afterDate,
Expand Down Expand Up @@ -340,7 +352,7 @@ class XMTPModule : Module() {
topicsList.add(Pair(topic, page))
}

client.conversations.listBatchMessages(topicsList)
client.conversations.listBatchDecryptedMessages(topicsList)
.map { DecodedMessageWrapper.encode(it) }
}

Expand Down Expand Up @@ -486,7 +498,7 @@ class XMTPModule : Module() {
topic = topic
)
?: throw XMTPException("no conversation found for $topic")
val decodedMessage = conversation.decode(envelope)
val decodedMessage = conversation.decrypt(envelope)
DecodedMessageWrapper.encode(decodedMessage)
}

Expand Down Expand Up @@ -581,7 +593,7 @@ class XMTPModule : Module() {
subscriptions[getMessagesKey(clientAddress)]?.cancel()
subscriptions[getMessagesKey(clientAddress)] = CoroutineScope(Dispatchers.IO).launch {
try {
client.conversations.streamAllMessages().collect { message ->
client.conversations.streamAllDecryptedMessages().collect { message ->
sendEvent(
"message",
mapOf(
Expand All @@ -607,7 +619,7 @@ class XMTPModule : Module() {
subscriptions[conversation.cacheKey(clientAddress)] =
CoroutineScope(Dispatchers.IO).launch {
try {
conversation.streamMessages().collect { message ->
conversation.streamDecryptedMessages().collect { message ->
sendEvent(
"message",
mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package expo.modules.xmtpreactnativesdk.wrappers

import android.util.Base64
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.protobuf.ByteString
Expand All @@ -14,6 +15,7 @@ import org.xmtp.android.library.codecs.ContentTypeReadReceipt
import org.xmtp.android.library.codecs.ContentTypeRemoteAttachment
import org.xmtp.android.library.codecs.ContentTypeReply
import org.xmtp.android.library.codecs.ContentTypeText
import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.codecs.Reaction
import org.xmtp.android.library.codecs.ReactionCodec
import org.xmtp.android.library.codecs.ReadReceipt
Expand All @@ -28,16 +30,17 @@ import org.xmtp.android.library.codecs.description
import org.xmtp.android.library.codecs.getReactionAction
import org.xmtp.android.library.codecs.getReactionSchema
import org.xmtp.android.library.codecs.id
import org.xmtp.proto.message.contents.Content.EncodedContent
import java.net.URL

class ContentJson(
val type: ContentTypeId,
val content: Any?,
val encodedContent: EncodedContent? = null,
) {
constructor(encoded: EncodedContent) : this(
type = encoded.type,
content = encoded.decoded(),
encodedContent = encoded
);

companion object {
Expand Down Expand Up @@ -157,19 +160,35 @@ class ContentJson(
ContentTypeReply.id -> mapOf(
"reply" to mapOf(
"reference" to (content as Reply).reference,
"content" to ContentJson(content.contentType, content.content).toJsonMap(),
"content" to ContentJson(
content.contentType,
content.content,
encodedContent
).toJsonMap(),
)
)

ContentTypeReadReceipt.id -> mapOf(
"readReceipt" to ""
)

else -> mapOf(
"unknown" to mapOf(
"contentTypeId" to type.description
)
)
else -> {
val gson = GsonBuilder().create()
val encodedContentJSON = try {
gson.toJson(encodedContent).toString()
} catch (e: Exception) {
null
}
if (!encodedContentJSON.isNullOrBlank()) {
mapOf("encoded" to encodedContentJSON)
} else {
mapOf(
"unknown" to mapOf(
"contentTypeId" to type.description
)
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package expo.modules.xmtpreactnativesdk.wrappers

import com.google.gson.GsonBuilder
import org.xmtp.android.library.DecodedMessage
import org.xmtp.android.library.codecs.description
import org.xmtp.android.library.codecs.id
import uniffi.xmtp_dh.org.xmtp.android.library.messages.DecryptedMessage

class DecodedMessageWrapper {

companion object {
fun encode(model: DecodedMessage): String {
fun encode(model: DecryptedMessage): String {
val gson = GsonBuilder().create()
val message = encodeMap(model)
return gson.toJson(message)
}

fun encodeMap(model: DecodedMessage): Map<String, Any> = mapOf(
fun encodeMap(model: DecryptedMessage): Map<String, Any> = mapOf(
"id" to model.id,
"topic" to model.topic,
"contentTypeId" to model.encodedContent.type.description,
"content" to ContentJson(model.encodedContent).toJsonMap(),
"senderAddress" to model.senderAddress,
"sent" to model.sent.time,
"fallback" to model.fallbackContent
"sent" to model.sentAt.time,
"fallback" to model.encodedContent.fallback
)
}
}
Loading