Skip to content

Commit

Permalink
setup the permission policy in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jul 2, 2024
1 parent cc3ffee commit 12e06fe
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import org.xmtp.android.library.messages.getPublicKeyBundle
import org.xmtp.android.library.push.Service
import org.xmtp.android.library.push.XMTPPush
import org.xmtp.android.library.toHex
import org.xmtp.android.library.hexToByteArray
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.contents.Invitation.ConsentProofPayload
Expand Down Expand Up @@ -1351,27 +1352,27 @@ class XMTPModule : Module() {
AsyncFunction("allowGroups") Coroutine { inboxId: String, groupIds: List<String> ->
logV("allowGroups")
val client = clients[inboxId] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
val groupDataIds = groupIds.map { Hex.hexStringToByteArray(it) }
client.contacts.allowGroups(groupDataIds)
}

AsyncFunction("denyGroups") Coroutine { inboxId: String, groupIds: List<String> ->
logV("denyGroups")
val client = clients[inboxId] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
val groupDataIds = groupIds.map { Hex.hexStringToByteArray(it) }
client.contacts.denyGroups(groupDataIds)
}

AsyncFunction("isGroupAllowed") { inboxId: String, groupId: String ->
logV("isGroupAllowed")
val client = clients[inboxId] ?: throw XMTPException("No client")
client.contacts.isGroupAllowed(Hex.hexStringToByteArray(groupId))
client.contacts.isGroupAllowed(groupId.hexToByteArray())
}

AsyncFunction("isGroupDenied") { inboxId: String, groupId: String ->
logV("isGroupDenied")
val client = clients[inboxId] ?: throw XMTPException("No client")
client.contacts.isGroupDenied(Hex.hexStringToByteArray(groupId))
client.contacts.isGroupDenied(groupId.hexToByteArray())
}
}

Expand Down
11 changes: 2 additions & 9 deletions ios/Wrappers/GroupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@ import XMTP
// Wrapper around XMTP.Group to allow passing these objects back into react native.
struct GroupWrapper {
static func encodeToObj(_ group: XMTP.Group, client: XMTP.Client) throws -> [String: Any] {
let permissionString = switch try group.permissionLevel() {
case .allMembers:
"all_members"
case .adminOnly:
"admin_only"
case .customPolicy:
"custom_policy"
}
let permissionPolicySet = try PermissionPolicySetWrapper.encodeToJsonString(group.permissionPolicySet())
return [
"clientAddress": client.address,
"id": group.id.toHex,
"createdAt": UInt64(group.createdAt.timeIntervalSince1970 * 1000),
"peerInboxIds": try group.peerInboxIds,
"version": "GROUP",
"topic": group.topic,
"permissionLevel": permissionString,
"permissionLevel": permissionPolicySet,
"creatorInboxId": try group.creatorInboxId(),
"name": try group.groupName(),
"isActive": try group.isActive(),
Expand Down
47 changes: 47 additions & 0 deletions ios/Wrappers/PermissionPolicySetWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PermissionPolicySetWrapper.swift
// XMTPReactNative
//
// Created by Naomi Plasterer on 7/1/24.
//

import Foundation
import XMTP

class PermissionPolicySetWrapper {
static func fromPermissionOption(_ permissionOption: XMTP.PermissionOption) -> String {
switch permissionOption {
case .allow:
return "allow"
case .deny:
return "deny"
case .admin:
return "admin"
case .superAdmin:
return "superAdmin"
case .unknown:
return "unknown"
}
}

static func encodeToObj(_ policySet: XMTP.PermissionPolicySet) -> [String: Any] {
return [
"addMemberPolicy": fromPermissionOption(policySet.addMemberPolicy),
"removeMemberPolicy": fromPermissionOption(policySet.removeMemberPolicy),
"addAdminPolicy": fromPermissionOption(policySet.addAdminPolicy),
"removeAdminPolicy": fromPermissionOption(policySet.removeAdminPolicy),
"updateGroupNamePolicy": fromPermissionOption(policySet.updateGroupNamePolicy),
"updateGroupDescriptionPolicy": fromPermissionOption(policySet.updateGroupDescriptionPolicy),
"updateGroupImagePolicy": fromPermissionOption(policySet.updateGroupImagePolicy)
]
}

static func encodeToJsonString(_ policySet: XMTP.PermissionPolicySet) throws -> String {
let obj = encodeToObj(policySet)
let data = try JSONSerialization.data(withJSONObject: obj)
guard let result = String(data: data, encoding: .utf8) else {
throw WrapperError.encodeError("could not encode permission policy")
}
return result
}
}
2 changes: 1 addition & 1 deletion ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public class XMTPModule: Module {
try await group.updateGroupImageUrlSquare(imageUrlSquare: groupImageUrl)
}

AsyncFunction("groupDescription") { (inboxId: String, id: String, description: String) in
AsyncFunction("groupDescription") { (inboxId: String, id: String) in
guard let client = await clientsManager.getClient(key: inboxId) else {
throw Error.noClient
}
Expand Down

0 comments on commit 12e06fe

Please sign in to comment.