Skip to content

Commit

Permalink
Support permissions policy updates in WASM bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Dec 13, 2024
1 parent f055f84 commit a98f2e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
21 changes: 21 additions & 0 deletions bindings_wasm/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use xmtp_mls::storage::group::ConversationType;
use crate::client::RustXmtpClient;
use crate::encoded_content::EncodedContent;
use crate::messages::{ListMessagesOptions, Message};
use crate::permissions::{MetadataField, PermissionPolicy, PermissionUpdateType};
use crate::{consent_state::ConsentState, permissions::GroupPermissions};
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,
};
use xmtp_mls::storage::group_message::{GroupMessageKind as XmtpGroupMessageKind, MsgQueryArgs};
Expand Down Expand Up @@ -543,4 +546,22 @@ impl Conversation {
.dm_inbox_id()
.map_err(|e| JsError::new(&format!("{e}")))
}

#[wasm_bindgen(js_name = updatePermissionPolicy)]
pub async fn update_permission_policy(
&self,
permission_update_type: PermissionUpdateType,
permission_policy_option: PermissionPolicy,
metadata_field: Option<MetadataField>,
) -> Result<(), JsError> {
self
.to_mls_group()
.update_permission_policy(
XmtpPermissionUpdateType::from(&permission_update_type),
permission_policy_option.try_into()?,
metadata_field.map(|field| XmtpMetadataField::from(&field)),
)
.await
.map_err(Into::into)
}
}
41 changes: 32 additions & 9 deletions bindings_wasm/src/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use wasm_bindgen::{prelude::wasm_bindgen, JsError};
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 @@ -245,10 +245,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 @@ -258,19 +262,19 @@ impl TryFrom<PermissionPolicySet> for PolicySet {
fn try_from(policy_set: PermissionPolicySet) -> 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 @@ -284,3 +288,22 @@ impl TryFrom<PermissionPolicySet> for PolicySet {
})
}
}

#[wasm_bindgen]
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 a98f2e2

Please sign in to comment.