Skip to content

Commit

Permalink
Merge pull request #517 from xmtp/np/v3-only-dms
Browse files Browse the repository at this point in the history
feat: V3 only dms
  • Loading branch information
nplasterer authored Oct 26, 2024
2 parents e1862c0 + e9221c9 commit c08ebf8
Show file tree
Hide file tree
Showing 25 changed files with 1,943 additions and 319 deletions.
363 changes: 300 additions & 63 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package expo.modules.xmtpreactnativesdk.wrappers

import android.util.Base64
import com.google.gson.GsonBuilder
import org.xmtp.android.library.Client
import org.xmtp.android.library.Conversation

class ConversationContainerWrapper {

companion object {
suspend fun encodeToObj(client: Client, conversation: Conversation): Map<String, Any?> {
when (conversation.version) {
suspend fun encodeToObj(
client: Client,
conversation: Conversation,
conversationParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): Map<String, Any?> {
return when (conversation.version) {
Conversation.Version.GROUP -> {
val group = (conversation as Conversation.Group).group
return GroupWrapper.encodeToObj(client, group)
GroupWrapper.encodeToObj(client, group, conversationParams)
}

Conversation.Version.DM -> {
val dm = (conversation as Conversation.Dm).dm
DmWrapper.encodeToObj(client, dm, conversationParams)
}

else -> {
return ConversationWrapper.encodeToObj(client, conversation)
ConversationWrapper.encodeToObj(client, conversation)
}
}
}

suspend fun encode(client: Client, conversation: Conversation): String {
suspend fun encode(
client: Client,
conversation: Conversation,
conversationParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): String {
val gson = GsonBuilder().create()
val obj = ConversationContainerWrapper.encodeToObj(client, conversation)
val obj =
ConversationContainerWrapper.encodeToObj(client, conversation, conversationParams)
return gson.toJson(obj)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package expo.modules.xmtpreactnativesdk.wrappers

import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import expo.modules.xmtpreactnativesdk.wrappers.ConsentWrapper.Companion.consentStateToString
import org.xmtp.android.library.Client
import org.xmtp.android.library.Dm
import org.xmtp.android.library.Group

class DmWrapper {
companion object {
suspend fun encodeToObj(
client: Client,
dm: Dm,
dmParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): Map<String, Any> {
return buildMap {
put("clientAddress", client.address)
put("id", dm.id)
put("createdAt", dm.createdAt.time)
put("version", "DM")
put("topic", dm.topic)
put("peerInboxId", dm.peerInboxId())
if (dmParams.members) {
put("members", dm.members().map { MemberWrapper.encode(it) })
}
if (dmParams.creatorInboxId) put("creatorInboxId", dm.creatorInboxId())
if (dmParams.consentState) {
put("consentState", consentStateToString(dm.consentState()))
}
if (dmParams.lastMessage) {
val lastMessage = dm.decryptedMessages(limit = 1).firstOrNull()
if (lastMessage != null) {
put("lastMessage", DecodedMessageWrapper.encode(lastMessage))
}
}
}
}

suspend fun encode(
client: Client,
dm: Dm,
dmParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): String {
val gson = GsonBuilder().create()
val obj = encodeToObj(client, dm, dmParams)
return gson.toJson(obj)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import expo.modules.xmtpreactnativesdk.wrappers.ConsentWrapper.Companion.consent
import org.xmtp.android.library.Client
import org.xmtp.android.library.Group

enum class ConversationOrder {
LAST_MESSAGE, CREATED_AT
}

class GroupWrapper {

companion object {
suspend fun encodeToObj(
client: Client,
group: Group,
groupParams: GroupParamsWrapper = GroupParamsWrapper(),
groupParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): Map<String, Any> {
return buildMap {
put("clientAddress", client.address)
Expand Down Expand Up @@ -48,7 +44,7 @@ class GroupWrapper {
suspend fun encode(
client: Client,
group: Group,
groupParams: GroupParamsWrapper = GroupParamsWrapper(),
groupParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): String {
val gson = GsonBuilder().create()
val obj = encodeToObj(client, group, groupParams)
Expand All @@ -57,7 +53,7 @@ class GroupWrapper {
}
}

class GroupParamsWrapper(
class ConversationParamsWrapper(
val members: Boolean = true,
val creatorInboxId: Boolean = true,
val isActive: Boolean = true,
Expand All @@ -69,10 +65,10 @@ class GroupParamsWrapper(
val lastMessage: Boolean = false,
) {
companion object {
fun groupParamsFromJson(groupParams: String): GroupParamsWrapper {
if (groupParams.isEmpty()) return GroupParamsWrapper()
val jsonOptions = JsonParser.parseString(groupParams).asJsonObject
return GroupParamsWrapper(
fun conversationParamsFromJson(conversationParams: String): ConversationParamsWrapper {
if (conversationParams.isEmpty()) return ConversationParamsWrapper()
val jsonOptions = JsonParser.parseString(conversationParams).asJsonObject
return ConversationParamsWrapper(
if (jsonOptions.has("members")) jsonOptions.get("members").asBoolean else true,
if (jsonOptions.has("creatorInboxId")) jsonOptions.get("creatorInboxId").asBoolean else true,
if (jsonOptions.has("isActive")) jsonOptions.get("isActive").asBoolean else true,
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.15.2):
- XMTP (0.16.0):
- Connect-Swift (= 0.12.0)
- GzipSwift
- LibXMTP (= 0.5.10)
Expand All @@ -458,7 +458,7 @@ PODS:
- ExpoModulesCore
- MessagePacker
- secp256k1.swift
- XMTP (= 0.15.2)
- XMTP (= 0.16.0)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -763,8 +763,8 @@ SPEC CHECKSUMS:
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftProtobuf: 407a385e97fd206c4fbe880cc84123989167e0d1
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: 7d47e6bc507db66dd01116ce2b4ed04dd3560a4f
XMTPReactNative: 1a946cd697598fb4bc560a637094e63c4d553df3
XMTP: 18d555dbf5afd3dcafa11b108042f9673da3c6b9
XMTPReactNative: cd8be3d8547d116005f3d0f4f207f19c7b34d035
Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9

PODFILE CHECKSUM: 0e6fe50018f34e575d38dc6a1fdf1f99c9596cdd
Expand Down
7 changes: 7 additions & 0 deletions example/src/TestScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useRoute } from '@react-navigation/native'
import React, { useEffect, useState } from 'react'
import { View, Text, Button, ScrollView } from 'react-native'

import { conversationTests } from './tests/conversationTests'
import { groupPerformanceTests } from './tests/groupPerformanceTests'
import { groupPermissionsTests } from './tests/groupPermissionsTests'
import { groupTests } from './tests/groupTests'
Expand Down Expand Up @@ -107,6 +108,7 @@ function TestView({
export enum TestCategory {
all = 'all',
tests = 'tests',
conversation = 'conversation',
group = 'group',
v3Only = 'v3Only',
restartStreans = 'restartStreams',
Expand All @@ -123,6 +125,7 @@ export default function TestScreen(): JSX.Element {
const allTests = [
...tests,
...groupTests,
...conversationTests,
...v3OnlyTests,
...restartStreamTests,
...groupPermissionsTests,
Expand All @@ -142,6 +145,10 @@ export default function TestScreen(): JSX.Element {
activeTests = groupTests
title = 'Group Unit Tests'
break
case TestCategory.conversation:
activeTests = conversationTests
title = 'Conversation Unit Tests'
break
case TestCategory.v3Only:
activeTests = v3OnlyTests
title = 'V3 Only Tests'
Expand Down
Loading

0 comments on commit c08ebf8

Please sign in to comment.