Skip to content

Commit

Permalink
added update metadata intent and mutablemetada obj
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 10, 2024
1 parent f980cea commit 052dc43
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
90 changes: 90 additions & 0 deletions xmtp_mls/src/groups/group_mutable_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use openmls::{
extensions::{Extension, UnknownExtension},
group::MlsGroup as OpenMlsGroup,
};
use prost::Message;
use thiserror::Error;

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

#[derive(Debug, Error)]
pub enum GroupMutableMetadataError {
#[error("serialization: {0}")]
Serialization(#[from] prost::EncodeError),
#[error("deserialization: {0}")]
Deserialization(#[from] prost::DecodeError),
#[error("missing extension")]
MissingExtension,
}

#[derive(Debug, Clone, PartialEq)]
pub struct GroupMutableMetadata {
pub group_name: String,
}

impl GroupMutableMetadata {
pub fn new(group_name: String) -> Self {
Self {
group_name
}
}

pub(crate) fn from_proto(
proto: GroupMutableMetadataProto,
) -> Result<Self, GroupMutableMetadataError> {
Ok(Self::new(
proto.group_name.clone()
))
}

pub(crate) fn to_proto(&self) -> Result<GroupMutableMetadataProto, GroupMutableMetadataError> {
Ok(GroupMutableMetadataProto {
group_name: self.group_name.clone()
})
}
}

impl TryFrom<GroupMutableMetadata> for Vec<u8> {
type Error = GroupMutableMetadataError;

fn try_from(value: GroupMutableMetadata) -> Result<Self, Self::Error> {
let mut buf = Vec::new();
let proto_val = value.to_proto()?;
proto_val.encode(&mut buf)?;

Ok(buf)
}
}

impl TryFrom<&Vec<u8>> for GroupMutableMetadata {
type Error = GroupMutableMetadataError;

fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
let proto_val = GroupMutableMetadataProto::decode(value.as_slice())?;
Self::from_proto(proto_val)
}
}

impl TryFrom<GroupMutableMetadataProto> for GroupMutableMetadata {
type Error = GroupMutableMetadataError;

fn try_from(value: GroupMutableMetadataProto) -> Result<Self, Self::Error> {
Self::from_proto(value)
}
}

pub fn extract_group_mutable_metadata(
group: &OpenMlsGroup,
) -> Result<GroupMutableMetadata, GroupMutableMetadataError> {
todo!()
}

#[cfg(test)]
mod tests {

use super::*;
#[test]
fn test_preconfigured_mutable_metadata() {
// TODO add test here
}
}
52 changes: 52 additions & 0 deletions xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ use xmtp_proto::xmtp::mls::database::{
},
remove_members_data::{Version as RemoveMembersVersion, V1 as RemoveMembersV1},
send_message_data::{Version as SendMessageVersion, V1 as SendMessageV1},
update_metadata_data::{Version as UpdateMetadataVersion, V1 as UpdateMetadataV1},
AccountAddresses, AddMembersData,
AddressesOrInstallationIds as AddressesOrInstallationIdsProtoWrapper, InstallationIds,
PostCommitAction as PostCommitActionProto, RemoveMembersData, SendMessageData,
UpdateMetadataData,
};

use crate::{
Expand Down Expand Up @@ -222,6 +224,47 @@ impl From<RemoveMembersIntentData> for Vec<u8> {
}
}

#[derive(Debug, Clone)]
pub struct UpdateMetadataIntentData {
pub group_name: String,
}

impl UpdateMetadataIntentData {
pub fn new(group_name: String) -> Self {
Self { group_name }
}

pub(crate) fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::new();

UpdateMetadataData {
version: Some(UpdateMetadataVersion::V1(UpdateMetadataV1 {
group_name: self.group_name.clone(),
})),
}
.encode(&mut buf)
.expect("encode error");

buf
}

pub(crate) fn from_bytes(data: &[u8]) -> Result<Self, IntentError> {

Check warning on line 251 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / Test

associated function `from_bytes` is never used

Check warning on line 251 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / Test

associated function `from_bytes` is never used

Check warning on line 251 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / Test

associated function `from_bytes` is never used

Check warning on line 251 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / Test

associated function `from_bytes` is never used

Check warning on line 251 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / workspace

associated function `from_bytes` is never used

warning: associated function `from_bytes` is never used --> xmtp_mls/src/groups/intents.rs:251:19 | 232 | impl UpdateMetadataIntentData { | ----------------------------- associated function in this implementation ... 251 | pub(crate) fn from_bytes(data: &[u8]) -> Result<Self, IntentError> { | ^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
let msg = UpdateMetadataData::decode(data)?;
let group_name = match msg.version {
Some(UpdateMetadataVersion::V1(ref v1)) => v1.group_name.clone(),
None => return Err(IntentError::Generic("missing payload".to_string())),
};

Ok(Self::new(group_name))
}
}

impl From<UpdateMetadataIntentData> for Vec<u8> {
fn from(intent: UpdateMetadataIntentData) -> Self {
intent.to_bytes()
}
}

#[derive(Debug, Clone)]
pub enum PostCommitAction {
SendWelcomes(SendWelcomesAction),
Expand Down Expand Up @@ -363,4 +406,13 @@ mod tests {

assert_eq!(intent.address_or_id, restored_intent.address_or_id);
}

#[tokio::test]
async fn test_serialize_update_metadata() {
let intent = UpdateMetadataIntentData::new("group name".to_string());
let as_bytes: Vec<u8> = intent.clone().try_into().unwrap();
let restored_intent = UpdateMetadataIntentData::from_bytes(as_bytes.as_slice()).unwrap();

assert_eq!(intent.group_name, restored_intent.group_name);
}
}
2 changes: 2 additions & 0 deletions xmtp_mls/src/groups/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ where
None => return Err(MessageProcessingError::InvalidPayload),
};
}
IntentKind::MetadataUpdate => todo!(),
};

conn.set_group_intent_committed(intent.id)?;
Expand Down Expand Up @@ -654,6 +655,7 @@ where

Ok((commit.tls_serialize_detached()?, None))
}
IntentKind::MetadataUpdate => todo!(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions xmtp_mls/src/storage/encrypted_store/group_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum IntentKind {
AddMembers = 2,
RemoveMembers = 3,
KeyUpdate = 4,
MetadataUpdate = 5,
}

#[repr(i32)]
Expand Down Expand Up @@ -261,6 +262,7 @@ where
2 => Ok(IntentKind::AddMembers),
3 => Ok(IntentKind::RemoveMembers),
4 => Ok(IntentKind::KeyUpdate),
5 => Ok(IntentKind::MetadataUpdate),
x => Err(format!("Unrecognized variant {}", x).into()),
}
}
Expand Down

0 comments on commit 052dc43

Please sign in to comment.