Skip to content

Commit

Permalink
add android permissionPolicySet
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Jul 2, 2024
1 parent 1f5c18d commit 04d4b80
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,22 @@ 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<String, Any> {
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(),
"createdAt" to group.createdAt.time,
"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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any> {

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)
}
}
}

0 comments on commit 04d4b80

Please sign in to comment.