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
9 changes: 7 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"changelog": [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the documentation isnt super clear on this, but hoping this will fix our changesets not creating github release / tag.

"@changesets/changelog-git",
{
"repo": "xmtp/xmtp-react-native"
}
],
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ 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.23"
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 @@ -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,18 @@ 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,
// Update if we add referenceInboxId to ../src/lib/types/ContentCodec.ts#L19-L24
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 +178,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 +258,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
18 changes: 9 additions & 9 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ PODS:
- hermes-engine/Pre-built (= 0.71.14)
- hermes-engine/Pre-built (0.71.14)
- libevent (2.1.12)
- LibXMTP (3.0.20)
- LibXMTP (3.0.21)
- MessagePacker (0.4.7)
- MMKV (2.0.2):
- MMKVCore (~> 2.0.2)
Expand Down Expand Up @@ -448,18 +448,18 @@ PODS:
- SQLCipher/standard (4.5.7):
- SQLCipher/common
- SwiftProtobuf (1.28.2)
- XMTP (3.0.24):
- XMTP (3.0.26):
- Connect-Swift (= 1.0.0)
- CryptoSwift (= 1.8.3)
- CSecp256k1 (~> 0.2)
- LibXMTP (= 3.0.20)
- LibXMTP (= 3.0.21)
- SQLCipher (= 4.5.7)
- XMTPReactNative (3.1.9):
- XMTPReactNative (3.1.10):
- CSecp256k1 (~> 0.2)
- ExpoModulesCore
- MessagePacker
- SQLCipher (= 4.5.7)
- XMTP (= 3.0.24)
- XMTP (= 3.0.26)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -685,7 +685,7 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/yoga"

SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
boost: 7dcd2de282d72e344012f7d6564d024930a6a440
CoinbaseWalletSDK: ea1f37512bbc69ebe07416e3b29bf840f5cc3152
CoinbaseWalletSDKExpo: c79420eb009f482f768c23b6768fc5b2d7c98777
Connect-Swift: 84e043b904f63dc93a2c01c6c125da25e765b50d
Expand All @@ -711,7 +711,7 @@ SPEC CHECKSUMS:
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: d7cc127932c89c53374452d6f93473f1970d8e88
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
LibXMTP: 5c8a858675af142e4a5336e27bb2d6fa6d633b18
LibXMTP: f58edec23d6e70b49b1eb2cfe0cb0308be3e5461
MessagePacker: ab2fe250e86ea7aedd1a9ee47a37083edd41fd02
MMKV: 3eacda84cd1c4fc95cf848d3ecb69d85ed56006c
MMKVCore: 508b4d3a8ce031f1b5c8bd235f0517fb3f4c73a9
Expand Down Expand Up @@ -762,8 +762,8 @@ SPEC CHECKSUMS:
RNSVG: d00c8f91c3cbf6d476451313a18f04d220d4f396
SQLCipher: 5e6bfb47323635c8b657b1b27d25c5f1baf63bf5
SwiftProtobuf: 4dbaffec76a39a8dc5da23b40af1a5dc01a4c02d
XMTP: 3d82fe8598fe043a67ad93276084ab46a30a1fa3
XMTPReactNative: 8758bb7d92ff8e6cf9a31cf0f57e5b09cc51319d
XMTP: fcbe033a5886adadfbdc190d8501e5f39c0b30b3
XMTPReactNative: d5cf653f0a71424bebb3181e395ff71b06f15541
Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9

PODFILE CHECKSUM: 0e6fe50018f34e575d38dc6a1fdf1f99c9596cdd
Expand Down
5 changes: 4 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"ios:clean": "expo run:ios --no-build-cache",
"web": "expo start --web",
"upload:up": "docker-compose -p xmtp -f dev/local/docker-compose.yml up -d",
"upload:down": "docker-compose -p xmtp -f dev/local/docker-compose.yml down"
"upload:down": "docker-compose -p xmtp -f dev/local/docker-compose.yml down",
"postinstall": "patch-package"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.21.0",
Expand Down Expand Up @@ -65,6 +66,8 @@
"eslint": "^8.54.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^3.1.0",
"typescript": "^4.9.4"
},
Expand Down
Loading
Loading