Skip to content

Commit

Permalink
utilize mutable metadata map for validating unknown fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 22, 2024
1 parent 01bf0e3 commit d9c26d5
Show file tree
Hide file tree
Showing 17 changed files with 3,318 additions and 2,805 deletions.
2 changes: 1 addition & 1 deletion dev/gen_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if ! cargo install --list | grep "protoc-gen-prost-crate" > /dev/null; then
fi
fi

if ! buf generate https://github.com/xmtp/proto.git#branch=cv/metadata-policy,subdir=proto; then
if ! buf generate https://github.com/xmtp/proto.git#branch=cv/mutable-metadata-mapping,subdir=proto; then
echo "Failed to generate protobuf definitions"
exit 1
fi
Expand Down
1 change: 1 addition & 0 deletions xmtp_mls/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ pub const MAX_GROUP_SIZE: u8 = 250;
pub const MUTABLE_METADATA_EXTENSION_ID: u16 = 0xff00;

pub const DEFAULT_GROUP_NAME: &str = "New Group";
pub const DEFAULT_GROUP_DESCRIPTION: &str = "New Group Description";
63 changes: 57 additions & 6 deletions xmtp_mls/src/groups/group_mutable_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{collections::HashMap, fmt};

use openmls::{
extensions::{Extension, UnknownExtension},
group::MlsGroup as OpenMlsGroup,
Expand All @@ -7,7 +9,9 @@ use thiserror::Error;

use xmtp_proto::xmtp::mls::message_contents::GroupMutableMetadataV1 as GroupMutableMetadataProto;

use crate::configuration::MUTABLE_METADATA_EXTENSION_ID;
use crate::configuration::{
DEFAULT_GROUP_DESCRIPTION, DEFAULT_GROUP_NAME, MUTABLE_METADATA_EXTENSION_ID,
};

#[derive(Debug, Error)]
pub enum GroupMutableMetadataError {
Expand All @@ -19,16 +23,63 @@ pub enum GroupMutableMetadataError {
MissingExtension,
#[error("mutable extension updates only")]
NonMutableExtensionUpdate,
#[error("only one change per update permitted")]
TooManyUpdates,
#[error("no changes in this update")]
NoUpdates,
}

// Fields should be added to supported_fields fn for Metadata Update Support
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MetadataField {
GroupName,
Description,
}

impl MetadataField {
fn as_str(self) -> &'static str {
match self {
MetadataField::GroupName => "group_name",
MetadataField::Description => "description",
}
}
}

impl fmt::Display for MetadataField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct GroupMutableMetadata {
pub group_name: String,
// Allow libxmtp to receive attributes from updated versions not yet captured in MetadataField
pub attributes: HashMap<String, String>,
}

impl Default for GroupMutableMetadata {
fn default() -> Self {
let mut attributes = HashMap::new();
attributes.insert(
MetadataField::GroupName.to_string(),
DEFAULT_GROUP_NAME.to_string(),
);
attributes.insert(
MetadataField::Description.to_string(),
DEFAULT_GROUP_DESCRIPTION.to_string(),
);
Self { attributes }
}
}

impl GroupMutableMetadata {
pub fn new(group_name: String) -> Self {
Self { group_name }
pub fn new(attributes: HashMap<String, String>) -> Self {
Self { attributes }
}

// These fields will receive default permission policies for new groups
pub fn supported_fields() -> Vec<MetadataField> {
vec![MetadataField::GroupName, MetadataField::Description]
}
}

Expand All @@ -38,7 +89,7 @@ impl TryFrom<GroupMutableMetadata> for Vec<u8> {
fn try_from(value: GroupMutableMetadata) -> Result<Self, Self::Error> {
let mut buf = Vec::new();
let proto_val = GroupMutableMetadataProto {
group_name: value.group_name.clone(),
attributes: value.attributes.clone(),
};
proto_val.encode(&mut buf)?;

Expand All @@ -59,7 +110,7 @@ impl TryFrom<GroupMutableMetadataProto> for GroupMutableMetadata {
type Error = GroupMutableMetadataError;

fn try_from(value: GroupMutableMetadataProto) -> Result<Self, Self::Error> {
Ok(Self::new(value.group_name.clone()))
Ok(Self::new(value.attributes.clone()))
}
}

Expand Down
Loading

0 comments on commit d9c26d5

Please sign in to comment.