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

Update to a V3 only SDK #526

Merged
merged 21 commits into from
Nov 12, 2024
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: 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:0.16.2"
implementation "org.xmtp:android:3.0.3"
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
1,616 changes: 307 additions & 1,309 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
Expand Up @@ -6,7 +6,6 @@ import org.xmtp.android.library.WalletType
class AuthParamsWrapper(
val environment: String,
val appVersion: String?,
val enableV3: Boolean = false,
val dbDirectory: String?,
val historySyncUrl: String?,
val walletType: WalletType = WalletType.EOA,
Expand All @@ -19,7 +18,6 @@ class AuthParamsWrapper(
return AuthParamsWrapper(
jsonOptions.get("environment").asString,
if (jsonOptions.has("appVersion")) jsonOptions.get("appVersion").asString else null,
if (jsonOptions.has("enableV3")) jsonOptions.get("enableV3").asBoolean else false,
if (jsonOptions.has("dbDirectory")) jsonOptions.get("dbDirectory").asString else null,
if (jsonOptions.has("historySyncUrl")) jsonOptions.get("historySyncUrl").asString else null,
if (jsonOptions.has("walletType")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package expo.modules.xmtpreactnativesdk.wrappers

import com.google.gson.GsonBuilder
import org.xmtp.android.library.Client

class ClientWrapper {
Expand All @@ -13,11 +12,5 @@ class ClientWrapper {
"dbPath" to client.dbPath
)
}

fun encode(client: Client): String {
val gson = GsonBuilder().create()
val obj = encodeToObj(client)
return gson.toJson(obj)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ContentJson(
return fromJsonObject(obj);
}

fun bytesFrom64(bytes64: String): ByteArray = Base64.decode(bytes64, Base64.NO_WRAP)
private fun bytesFrom64(bytes64: String): ByteArray = Base64.decode(bytes64, Base64.NO_WRAP)
fun bytesTo64(bytes: ByteArray): String = Base64.encodeToString(bytes, Base64.NO_WRAP)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
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 ConversationWrapper {

companion object {
fun encodeToObj(client: Client, conversation: Conversation): Map<String, Any?> {
val context = when (conversation.version) {
Conversation.Version.V2 -> mapOf<String, Any>(
"conversationID" to (conversation.conversationId ?: ""),
// TODO: expose the context/metadata explicitly in xmtp-android
"metadata" to conversation.toTopicData().invitation.context.metadataMap,
)
fun encodeToObj(
client: Client,
conversation: Conversation,
conversationParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): Map<String, Any?> {
return when (conversation.type) {
Conversation.Type.GROUP -> {
val group = (conversation as Conversation.Group).group
GroupWrapper.encodeToObj(client, group, conversationParams)
}

else -> mapOf()
Conversation.Type.DM -> {
val dm = (conversation as Conversation.Dm).dm
DmWrapper.encodeToObj(client, dm, conversationParams)
}
}
return mapOf(
"clientAddress" to client.address,
"createdAt" to conversation.createdAt.time,
"context" to context,
"topic" to conversation.topic,
"peerAddress" to conversation.peerAddress,
"version" to "DIRECT",
"conversationID" to (conversation.conversationId ?: ""),
"keyMaterial" to (conversation.keyMaterial?.let { Base64.encodeToString(it, Base64.NO_WRAP) } ?: ""),
"consentProof" to if (conversation.consentProof != null) Base64.encodeToString(conversation.consentProof?.toByteArray(), Base64.NO_WRAP) else null
)
}

fun encode(client: Client, conversation: Conversation): String {
fun encode(
client: Client,
conversation: Conversation,
conversationParams: ConversationParamsWrapper = ConversationParamsWrapper(),
): String {
val gson = GsonBuilder().create()
val obj = encodeToObj(client, conversation)
val obj =
encodeToObj(client, conversation, conversationParams)
return gson.toJson(obj)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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.messages.DecryptedMessage

class DecodedMessageWrapper {

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

fun encodeMap(model: DecryptedMessage): Map<String, Any?> {
fun encodeMap(model: DecodedMessage): Map<String, Any?> {
// Kotlin/Java Protos don't support null values and will always put the default ""
// Check if there is a fallback, if there is then make it the set fallback, if not null
val fallback = if (model.encodedContent.hasFallback()) model.encodedContent.fallback else null
Expand All @@ -23,7 +23,7 @@ class DecodedMessageWrapper {
"contentTypeId" to model.encodedContent.type.description,
"content" to ContentJson(model.encodedContent).toJsonMap(),
"senderAddress" to model.senderAddress,
"sent" to model.sentAt.time,
"sentNs" to model.sentNs,
"fallback" to fallback,
"deliveryStatus" to model.deliveryStatus.toString()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DecryptedLocalAttachment(
val filename: String,
) {
companion object {
fun fromJsonObject(obj: JsonObject) = DecryptedLocalAttachment(
private fun fromJsonObject(obj: JsonObject) = DecryptedLocalAttachment(
obj.get("fileUri").asString,
obj.get("mimeType")?.asString ?: "",
obj.get("filename")?.asString ?: "",
Expand All @@ -25,7 +25,7 @@ class DecryptedLocalAttachment(
}
}

fun toJsonMap(): Map<String, Any> = mapOf(
private fun toJsonMap(): Map<String, Any> = mapOf(
"fileUri" to fileUri,
"mimeType" to mimeType,
"filename" to filename,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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(
fun encodeToObj(
client: Client,
dm: Dm,
dmParams: ConversationParamsWrapper = ConversationParamsWrapper(),
Expand All @@ -25,15 +22,15 @@ class DmWrapper {
put("consentState", consentStateToString(dm.consentState()))
}
if (dmParams.lastMessage) {
val lastMessage = dm.decryptedMessages(limit = 1).firstOrNull()
val lastMessage = dm.messages(limit = 1).firstOrNull()
if (lastMessage != null) {
put("lastMessage", DecodedMessageWrapper.encode(lastMessage))
}
}
}
}

suspend fun encode(
fun encode(
client: Client,
dm: Dm,
dmParams: ConversationParamsWrapper = ConversationParamsWrapper(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EncryptedLocalAttachment(
}
}

fun toJsonMap(): Map<String, Any> = mapOf(
private fun toJsonMap(): Map<String, Any> = mapOf(
"encryptedLocalFileUri" to encryptedLocalFileUri,
"metadata" to metadata.toJsonMap()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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.ConsentState
import org.xmtp.android.library.Group

class GroupWrapper {

companion object {
suspend fun encodeToObj(
fun encodeToObj(
client: Client,
group: Group,
groupParams: ConversationParamsWrapper = ConversationParamsWrapper(),
Expand All @@ -29,15 +29,15 @@ class GroupWrapper {
put("consentState", consentStateToString(group.consentState()))
}
if (groupParams.lastMessage) {
val lastMessage = group.decryptedMessages(limit = 1).firstOrNull()
val lastMessage = group.messages(limit = 1).firstOrNull()
if (lastMessage != null) {
put("lastMessage", DecodedMessageWrapper.encode(lastMessage))
}
}
}
}

suspend fun encode(
fun encode(
client: Client,
group: Group,
groupParams: ConversationParamsWrapper = ConversationParamsWrapper(),
Expand All @@ -49,6 +49,14 @@ class GroupWrapper {
}
}

fun consentStateToString(state: ConsentState): String {
return when (state) {
ConsentState.ALLOWED -> "allowed"
ConsentState.DENIED -> "denied"
ConsentState.UNKNOWN -> "unknown"
}
}

class ConversationParamsWrapper(
val isActive: Boolean = true,
val addedByInboxId: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.InboxState
class InboxStateWrapper {
companion object {
val gson: Gson = GsonBuilder().create()
fun encodeToObj(inboxState: InboxState): Map<String, Any> {
private fun encodeToObj(inboxState: InboxState): Map<String, Any> {
return mapOf(
"inboxId" to inboxState.inboxId,
"addresses" to inboxState.addresses,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package expo.modules.xmtpreactnativesdk.wrappers

import com.google.gson.GsonBuilder
import org.xmtp.android.library.ConsentState
import org.xmtp.android.library.libxmtp.Member
import org.xmtp.android.library.libxmtp.PermissionLevel

class MemberWrapper {
companion object {
fun encodeToObj(member: Member): Map<String, Any> {
private fun encodeToObj(member: Member): Map<String, Any> {
val permissionString = when (member.permissionLevel) {
PermissionLevel.MEMBER -> "member"
PermissionLevel.ADMIN -> "admin"
Expand All @@ -16,7 +17,7 @@ class MemberWrapper {
"inboxId" to member.inboxId,
"addresses" to member.addresses,
"permissionLevel" to permissionString,
"consentState" to ConsentWrapper.consentStateToString(member.consentState)
"consentState" to consentStateToString(member.consentState)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
class PermissionPolicySetWrapper {

companion object {
fun fromPermissionOption(permissionOption: PermissionOption): String {
private fun fromPermissionOption(permissionOption: PermissionOption): String {
return when (permissionOption) {
PermissionOption.Allow -> "allow"
PermissionOption.Deny -> "deny"
Expand All @@ -18,7 +18,7 @@ class PermissionPolicySetWrapper {
}
}

fun createPermissionOptionFromString(permissionOptionString: String): PermissionOption {
private fun createPermissionOptionFromString(permissionOptionString: String): PermissionOption {
return when (permissionOptionString) {
"allow" -> PermissionOption.Allow
"deny" -> PermissionOption.Deny
Expand All @@ -27,7 +27,7 @@ class PermissionPolicySetWrapper {
else -> PermissionOption.Unknown
}
}
fun encodeToObj(policySet: PermissionPolicySet): Map<String, Any> {
private fun encodeToObj(policySet: PermissionPolicySet): Map<String, Any> {
return mapOf(
"addMemberPolicy" to fromPermissionOption(policySet.addMemberPolicy),
"removeMemberPolicy" to fromPermissionOption(policySet.removeMemberPolicy),
Expand Down
Loading
Loading