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

Messages with reactions function, dm fix #596

Merged
merged 12 commits into from
Feb 3, 2025
22 changes: 11 additions & 11 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:3.0.22"
// implementation "org.xmtp:android:3.0.22"
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"
// xmtp-android local testing setup below (comment org.xmtp:android above)
// implementation files('<PATH_TO_LIBRARY>/xmtp-android/library/build/outputs/aar/library-debug.aar')
// implementation 'com.google.crypto.tink:tink-android:1.8.0'
// implementation 'io.grpc:grpc-kotlin-stub:1.4.1'
// implementation 'io.grpc:grpc-okhttp:1.62.2'
// implementation 'io.grpc:grpc-protobuf-lite:1.62.2'
// implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0'
// implementation 'org.web3j:crypto:4.9.4'
// implementation "net.java.dev.jna:jna:5.14.0@aar"
// api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
// api 'org.xmtp:proto-kotlin:3.72.4'
implementation files('/Users/cameronvoell/XMTP/xmtp-android/library/build/outputs/aar/library-debug.aar')
implementation 'com.google.crypto.tink:tink-android:1.8.0'
implementation 'io.grpc:grpc-kotlin-stub:1.4.1'
implementation 'io.grpc:grpc-okhttp:1.62.2'
implementation 'io.grpc:grpc-protobuf-lite:1.62.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0'
implementation 'org.web3j:crypto:4.9.4'
implementation "net.java.dev.jna:jna:5.14.0@aar"
api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
api 'org.xmtp:proto-kotlin:3.72.4'
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,22 @@ class XMTPModule : Module() {
}
}

AsyncFunction("conversationMessagesWithReactions") Coroutine { installationId: String, conversationId: String, limit: Int?, beforeNs: Long?, afterNs: Long?, direction: String? ->
withContext(Dispatchers.IO) {
logV("conversationMessagesWithReactions")
val client = clients[installationId] ?: throw XMTPException("No client")
val conversation = client.findConversation(conversationId)
conversation?.messagesWithReactions(
limit = limit,
beforeNs = beforeNs,
afterNs = afterNs,
direction = Message.SortDirection.valueOf(
direction ?: "DESCENDING"
)
)?.map { MessageWrapper.encode(it) }
}
}

AsyncFunction("findMessage") Coroutine { installationId: String, messageId: String ->
withContext(Dispatchers.IO) {
logV("findMessage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.xmtp.android.library.codecs.ContentTypeAttachment
import org.xmtp.android.library.codecs.ContentTypeGroupUpdated
import org.xmtp.android.library.codecs.ContentTypeId
import org.xmtp.android.library.codecs.ContentTypeReaction
import org.xmtp.android.library.codecs.ContentTypeReactionV2
import org.xmtp.android.library.codecs.ContentTypeReadReceipt
import org.xmtp.android.library.codecs.ContentTypeRemoteAttachment
import org.xmtp.android.library.codecs.ContentTypeReply
Expand All @@ -21,6 +22,7 @@ import org.xmtp.android.library.codecs.GroupUpdated
import org.xmtp.android.library.codecs.GroupUpdatedCodec
import org.xmtp.android.library.codecs.Reaction
import org.xmtp.android.library.codecs.ReactionCodec
import org.xmtp.android.library.codecs.ReactionV2Codec
import org.xmtp.android.library.codecs.ReadReceipt
import org.xmtp.android.library.codecs.ReadReceiptCodec
import org.xmtp.android.library.codecs.RemoteAttachment
Expand All @@ -33,6 +35,10 @@ 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 uniffi.xmtpv3.FfiReaction
import uniffi.xmtpv3.FfiReactionAction
import uniffi.xmtpv3.FfiReactionSchema
import uniffi.xmtpv3.decodeReaction
import java.net.URL

class ContentJson(
Expand All @@ -55,6 +61,7 @@ class ContentJson(
Client.register(ReplyCodec())
Client.register(ReadReceiptCodec())
Client.register(GroupUpdatedCodec())
Client.register(ReactionV2Codec())
}

fun fromJsonObject(obj: JsonObject): ContentJson {
Expand Down Expand Up @@ -95,6 +102,17 @@ class ContentJson(
content = reaction.get("content").asString,
)
)
} else if (obj.has("reactionV2")) {
val reaction = obj.get("reactionV2").asJsonObject
return ContentJson(
ContentTypeReactionV2, FfiReaction(
reference = reaction.get("reference").asString,
action = getReactionV2Action(reaction.get("action").asString.lowercase()),
schema = getReactionV2Schema(reaction.get("schema").asString.lowercase()),
content = reaction.get("content").asString,
referenceInboxId = ""
Copy link

Choose a reason for hiding this comment

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

Question: is that intentional left empty?

Copy link
Contributor Author

@cameronvoell cameronvoell Feb 3, 2025

Choose a reason for hiding this comment

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

yea that property is sort of stubbed in for now, as I noticed it was included in the xmtp-js reaction type: https://github.com/xmtp/xmtp-js/blob/80aa5ec4fb24b60def222537cebcb20c818ca993/content-types/content-type-reaction/src/Reaction.ts#L24

However xmtp-react-native has an existing ReactionContent type that didn't feel worth updating at this point since I'm not sure what referenceInboxId would actually be used for.

definitely a code smell though, I added a clarifying comments here 👌: faad8de

)
)
} else if (obj.has("reply")) {
val reply = obj.get("reply").asJsonObject
val nested = fromJsonObject(reply.get("content").asJsonObject)
Expand Down Expand Up @@ -159,6 +177,18 @@ class ContentJson(
)
)

ContentTypeReactionV2.id -> {
val reaction: FfiReaction = decodeReaction(encodedContent!!.toByteArray())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reactionV2 in this PR mainly serves to show how proto based ContentTypes that are used in libxmtp and upstream xmtp-android/ios can be utilized in React Native.

In this PR, we're basically converting the proto type back to json before utilizing the Reaction content in RN typescript code. An alternative would be to send the reaction content as bytes over the RN bridge and deserialize the bytes to the Reaction proto object in typescript. This pattern would generally lead to less data going over the RN bridge and would probably be a net gain for pretty much all data classes we use. However, it probably makes sense to start implementing that pattern in a separate PR.

Lastly, in a follow up, we can delete the reactionV2Codec in RN, and simply update the ContentTypeId version from 1.0 to 2.0, and the underlying SDK will switch from using JSON serializing to proto serializing without RN integrators knowing the difference. One exception is that old clients will not be able to deserialize messages sent with the proto backed reactions, but they should recognize that the version has been updated, and display the fallback instead. maintaining the two different Reaction and ReactionV2 for a short while in RN will help us test those paths.

mapOf(
"reaction" to mapOf(
"reference" to reaction.reference,
"action" to getReactionV2ActionString(reaction.action),
"schema" to getReactionV2SchemaString(reaction.schema),
"content" to reaction.content,
)
)
}

ContentTypeReply.id -> mapOf(
"reply" to mapOf(
"reference" to (content as Reply).reference,
Expand Down Expand Up @@ -227,4 +257,38 @@ class ContentJson(
}
}
}
}

fun getReactionV2Schema(schema: String): FfiReactionSchema {
return when (schema) {
"unicode" -> FfiReactionSchema.UNICODE
"shortcode" -> FfiReactionSchema.SHORTCODE
"custom" -> FfiReactionSchema.CUSTOM
else -> FfiReactionSchema.UNKNOWN
}
}

fun getReactionV2Action(action: String): FfiReactionAction {
return when (action) {
"removed" -> FfiReactionAction.REMOVED
"added" -> FfiReactionAction.ADDED
else -> FfiReactionAction.UNKNOWN
}
}

fun getReactionV2SchemaString(schema: FfiReactionSchema): String {
return when (schema) {
FfiReactionSchema.UNICODE -> "unicode"
FfiReactionSchema.SHORTCODE -> "shortcode"
FfiReactionSchema.CUSTOM -> "custom"
FfiReactionSchema.UNKNOWN -> "unknown"
}
}

fun getReactionV2ActionString(action: FfiReactionAction): String {
return when (action) {
FfiReactionAction.REMOVED -> "removed"
FfiReactionAction.ADDED -> "added"
FfiReactionAction.UNKNOWN -> "unknown"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class MessageWrapper {
"senderInboxId" to model.senderInboxId,
"sentNs" to model.sentAtNs,
"fallback" to fallback,
"deliveryStatus" to model.deliveryStatus.toString()
"deliveryStatus" to model.deliveryStatus.toString(),
"childMessages" to model.childMessages?.map { childMessage -> encodeMap(childMessage) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PermissionPolicySetWrapper {
"updateGroupDescriptionPolicy" to fromPermissionOption(policySet.updateGroupDescriptionPolicy),
"updateGroupImagePolicy" to fromPermissionOption(policySet.updateGroupImagePolicy),
"updateGroupPinnedFrameUrlPolicy" to fromPermissionOption(policySet.updateGroupPinnedFrameUrlPolicy),
"updateMessageExpirationPolicy" to fromPermissionOption(policySet.updateMessageExpirationPolicy),
"updateMessageDisappearingPolicy" to fromPermissionOption(policySet.updateMessageDisappearingPolicy),
)
}

Expand All @@ -52,7 +52,7 @@ class PermissionPolicySetWrapper {
updateGroupDescriptionPolicy = createPermissionOptionFromString(jsonObj.get("updateGroupDescriptionPolicy").asString),
updateGroupImagePolicy = createPermissionOptionFromString(jsonObj.get("updateGroupImagePolicy").asString),
updateGroupPinnedFrameUrlPolicy = createPermissionOptionFromString(jsonObj.get("updateGroupPinnedFrameUrlPolicy").asString),
updateMessageExpirationPolicy = createPermissionOptionFromString(jsonObj.get("updateMessageExpirationPolicy").asString),
updateMessageDisappearingPolicy = createPermissionOptionFromString(jsonObj.get("updateMessageDisappearingPolicy").asString),
)
}

Expand Down
2 changes: 2 additions & 0 deletions example/src/contentTypes/contentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
GroupUpdatedCodec,
ReactionCodec,
ReactionV2Codec,
ReplyCodec,
RemoteAttachmentCodec,
StaticAttachmentCodec,
} from 'xmtp-react-native-sdk'

export const supportedCodecs = [
new ReactionCodec(),
new ReactionV2Codec(),
new ReplyCodec(),
new RemoteAttachmentCodec(),
new StaticAttachmentCodec(),
Expand Down
Loading
Loading