Skip to content

Commit

Permalink
Support permissions policy updates in Node bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Dec 13, 2024
1 parent a98f2e2 commit 2b0c30c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
31 changes: 30 additions & 1 deletion bindings_node/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
};
Expand Down Expand Up @@ -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<MetadataField>,
) -> 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(())
}
}
41 changes: 32 additions & 9 deletions bindings_node/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
),
})
}
}
Expand All @@ -222,19 +226,19 @@ impl TryFrom<PermissionPolicySet> for PolicySet {
) -> std::result::Result<Self, GroupMutablePermissionsError> {
let mut metadata_permissions_map: HashMap<String, MetadataPolicies> = 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()?,
);

Expand All @@ -248,3 +252,22 @@ impl TryFrom<PermissionPolicySet> 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,
}
}
}

0 comments on commit 2b0c30c

Please sign in to comment.