From 04d4b80fc9ea998d07c9f5820b974357d70a7ad2 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Mon, 1 Jul 2024 19:05:33 -0700 Subject: [PATCH] add android permissionPolicySet --- .../wrappers/GroupWrapper.kt | 11 +--- .../wrappers/PermissionPolicySet.kt | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/PermissionPolicySet.kt diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/GroupWrapper.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/GroupWrapper.kt index 367bf8e6c..b3363e52c 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/GroupWrapper.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/GroupWrapper.kt @@ -2,21 +2,14 @@ package expo.modules.xmtpreactnativesdk.wrappers import com.google.gson.GsonBuilder import org.xmtp.android.library.Client -import org.xmtp.android.library.Conversation import org.xmtp.android.library.Group -import org.xmtp.android.library.codecs.Attachment import org.xmtp.android.library.toHex -import uniffi.xmtpv3.GroupPermissions class GroupWrapper { companion object { fun encodeToObj(client: Client, group: Group): Map { - val permissionString = when (group.permissionLevel()) { - GroupPermissions.ALL_MEMBERS -> "all_members" - GroupPermissions.ADMIN_ONLY -> "admin_only" - GroupPermissions.CUSTOM_POLICY -> "custom_policy" - } + val permissionPolicySet = PermissionPolicySetWrapper.encodeToObj(group.permissionPolicySet()) return mapOf( "clientAddress" to client.address, "id" to group.id.toHex(), @@ -24,7 +17,7 @@ class GroupWrapper { "peerInboxIds" to group.peerInboxIds(), "version" to "GROUP", "topic" to group.topic, - "permissionLevel" to permissionString, + "permissionPolicySet" to permissionPolicySet, "creatorInboxId" to group.creatorInboxId(), "name" to group.name, "isActive" to group.isActive(), diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/PermissionPolicySet.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/PermissionPolicySet.kt new file mode 100644 index 000000000..bd940cdb7 --- /dev/null +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/PermissionPolicySet.kt @@ -0,0 +1,51 @@ +package expo.modules.xmtpreactnativesdk.wrappers + +import com.google.gson.GsonBuilder +import expo.modules.kotlin.types.Enumerable +import org.xmtp.android.library.Client +import org.xmtp.android.library.Group +import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption +import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet + +enum class ExpoPermissionOption(val value: String) : Enumerable { + Allow("Allow"), + Deny("Deny"), + Admin("Admin"), + SuperAdmin("SuperAdmin"), + Unknown("Unknown"); + companion object { + fun fromPermissionOption(permissionOption: PermissionOption): ExpoPermissionOption { + return when (permissionOption) { + PermissionOption.Allow -> Allow + PermissionOption.Deny -> Deny + PermissionOption.Admin -> Admin + PermissionOption.SuperAdmin -> SuperAdmin + PermissionOption.Unknown -> Unknown + } + } + } +} + +class PermissionPolicySetWrapper { + + companion object { + fun encodeToObj(policySet: PermissionPolicySet): Map { + + return mapOf( + "addMemberPolicy" to ExpoPermissionOption.fromPermissionOption(policySet.addMemberPolicy), + "removeMemberPolicy" to ExpoPermissionOption.fromPermissionOption(policySet.removeMemberPolicy), + "addAdminPolicy" to ExpoPermissionOption.fromPermissionOption(policySet.addAdminPolicy), + "removeAdminPolicy" to ExpoPermissionOption.fromPermissionOption(policySet.removeAdminPolicy), + "updateGroupNamePolicy" to ExpoPermissionOption.fromPermissionOption(policySet.updateGroupNamePolicy), + "updateGroupDescriptionPolicy" to ExpoPermissionOption.fromPermissionOption(policySet.updateGroupDescriptionPolicy), + "updateGroupImagePolicy" to ExpoPermissionOption.fromPermissionOption(policySet.updateGroupImagePolicy), + ) + } + + fun encode(client: Client, group: Group): String { + val gson = GsonBuilder().create() + val obj = GroupWrapper.encodeToObj(client, group) + return gson.toJson(obj) + } + } +} \ No newline at end of file