From 2b0c30cd1c317d88adb1588cba609bc48cc4c9cd Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 13 Dec 2024 17:25:36 -0600 Subject: [PATCH] Support permissions policy updates in Node bindings --- bindings_node/src/conversation.rs | 31 ++++++++++++++++++++++- bindings_node/src/permissions.rs | 41 ++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/bindings_node/src/conversation.rs b/bindings_node/src/conversation.rs index 897662463..3cd1328be 100644 --- a/bindings_node/src/conversation.rs +++ b/bindings_node/src/conversation.rs @@ -9,6 +9,8 @@ use xmtp_cryptography::signature::ed25519_public_key_to_address; use xmtp_mls::{ groups::{ group_metadata::GroupMetadata as XmtpGroupMetadata, + group_mutable_metadata::MetadataField as XmtpMetadataField, + intents::PermissionUpdateType as XmtpPermissionUpdateType, members::PermissionLevel as XmtpPermissionLevel, MlsGroup, UpdateAdminListType, }, storage::{ @@ -23,7 +25,7 @@ use crate::{ consent_state::ConsentState, encoded_content::EncodedContent, message::{ListMessagesOptions, Message}, - permissions::GroupPermissions, + permissions::{GroupPermissions, MetadataField, PermissionPolicy, PermissionUpdateType}, streams::StreamCloser, ErrorWrapper, }; @@ -654,4 +656,31 @@ impl Conversation { Ok(group.dm_inbox_id().map_err(ErrorWrapper::from)?) } + + #[napi] + pub async fn update_permission_policy( + &self, + permission_update_type: PermissionUpdateType, + permission_policy_option: PermissionPolicy, + metadata_field: Option, + ) -> Result<()> { + let group = MlsGroup::new( + self.inner_client.clone(), + self.group_id.clone(), + self.created_at_ns, + ); + + group + .update_permission_policy( + XmtpPermissionUpdateType::from(&permission_update_type), + permission_policy_option + .try_into() + .map_err(ErrorWrapper::from)?, + metadata_field.map(|field| XmtpMetadataField::from(&field)), + ) + .await + .map_err(ErrorWrapper::from)?; + + Ok(()) + } } diff --git a/bindings_node/src/permissions.rs b/bindings_node/src/permissions.rs index 9f91f4c48..3008c4b4e 100644 --- a/bindings_node/src/permissions.rs +++ b/bindings_node/src/permissions.rs @@ -2,7 +2,7 @@ use napi::bindgen_prelude::Result; use napi_derive::napi; use std::collections::HashMap; use xmtp_mls::groups::{ - group_mutable_metadata::MetadataField, + group_mutable_metadata::MetadataField as XmtpMetadataField, group_permissions::{ BasePolicies, GroupMutablePermissions, GroupMutablePermissionsError, MembershipPolicies, MetadataBasePolicies, MetadataPolicies, PermissionsBasePolicies, PermissionsPolicies, @@ -207,10 +207,14 @@ impl GroupPermissions { remove_member_policy: PermissionPolicy::from(&policy_set.remove_member_policy), add_admin_policy: PermissionPolicy::from(&policy_set.add_admin_policy), remove_admin_policy: PermissionPolicy::from(&policy_set.remove_admin_policy), - update_group_name_policy: get_policy(MetadataField::GroupName.as_str()), - update_group_description_policy: get_policy(MetadataField::Description.as_str()), - update_group_image_url_square_policy: get_policy(MetadataField::GroupImageUrlSquare.as_str()), - update_group_pinned_frame_url_policy: get_policy(MetadataField::GroupPinnedFrameUrl.as_str()), + update_group_name_policy: get_policy(XmtpMetadataField::GroupName.as_str()), + update_group_description_policy: get_policy(XmtpMetadataField::Description.as_str()), + update_group_image_url_square_policy: get_policy( + XmtpMetadataField::GroupImageUrlSquare.as_str(), + ), + update_group_pinned_frame_url_policy: get_policy( + XmtpMetadataField::GroupPinnedFrameUrl.as_str(), + ), }) } } @@ -222,19 +226,19 @@ impl TryFrom for PolicySet { ) -> std::result::Result { let mut metadata_permissions_map: HashMap = HashMap::new(); metadata_permissions_map.insert( - MetadataField::GroupName.to_string(), + XmtpMetadataField::GroupName.to_string(), policy_set.update_group_name_policy.try_into()?, ); metadata_permissions_map.insert( - MetadataField::Description.to_string(), + XmtpMetadataField::Description.to_string(), policy_set.update_group_description_policy.try_into()?, ); metadata_permissions_map.insert( - MetadataField::GroupImageUrlSquare.to_string(), + XmtpMetadataField::GroupImageUrlSquare.to_string(), policy_set.update_group_image_url_square_policy.try_into()?, ); metadata_permissions_map.insert( - MetadataField::GroupPinnedFrameUrl.to_string(), + XmtpMetadataField::GroupPinnedFrameUrl.to_string(), policy_set.update_group_pinned_frame_url_policy.try_into()?, ); @@ -248,3 +252,22 @@ impl TryFrom for PolicySet { }) } } + +#[napi] +pub enum MetadataField { + GroupName, + Description, + ImageUrlSquare, + PinnedFrameUrl, +} + +impl From<&MetadataField> for XmtpMetadataField { + fn from(field: &MetadataField) -> Self { + match field { + MetadataField::GroupName => XmtpMetadataField::GroupName, + MetadataField::Description => XmtpMetadataField::Description, + MetadataField::ImageUrlSquare => XmtpMetadataField::GroupImageUrlSquare, + MetadataField::PinnedFrameUrl => XmtpMetadataField::GroupPinnedFrameUrl, + } + } +}