Skip to content

Commit

Permalink
Add new GroupUpdated proto (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas authored May 20, 2024
2 parents f103d3b + 74afa30 commit 66e2bd3
Show file tree
Hide file tree
Showing 7 changed files with 1,147 additions and 242 deletions.
75 changes: 75 additions & 0 deletions xmtp_mls/src/codecs/group_updated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::collections::HashMap;

use prost::Message;

use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent, GroupUpdated};

use super::{CodecError, ContentCodec};

pub struct GroupUpdatedCodec {}

impl GroupUpdatedCodec {
const AUTHORITY_ID: &'static str = "xmtp.org";
const TYPE_ID: &'static str = "group_updated";
}

impl ContentCodec<GroupUpdated> for GroupUpdatedCodec {
fn content_type() -> ContentTypeId {
ContentTypeId {
authority_id: GroupUpdatedCodec::AUTHORITY_ID.to_string(),
type_id: GroupUpdatedCodec::TYPE_ID.to_string(),
version_major: 1,
version_minor: 0,
}
}

fn encode(data: GroupUpdated) -> Result<EncodedContent, CodecError> {
let mut buf = Vec::new();
data.encode(&mut buf)
.map_err(|e| CodecError::Encode(e.to_string()))?;

Ok(EncodedContent {
r#type: Some(GroupUpdatedCodec::content_type()),
parameters: HashMap::new(),
fallback: None,
compression: None,
content: buf,
})
}

fn decode(content: EncodedContent) -> Result<GroupUpdated, CodecError> {
let decoded = GroupUpdated::decode(content.content.as_slice())
.map_err(|e| CodecError::Decode(e.to_string()))?;

Ok(decoded)
}
}

#[cfg(test)]
mod tests {
use xmtp_proto::xmtp::mls::message_contents::{group_updated::Inbox, GroupUpdated};

use crate::utils::test::rand_string;

use super::*;

#[test]
fn test_encode_decode() {
let new_member = Inbox {
inbox_id: rand_string(),
};
let data = GroupUpdated {
initiated_by_inbox_id: rand_string(),
added_inboxes: vec![new_member.clone()],
removed_inboxes: vec![],
metadata_field_changes: vec![],
};

let encoded = GroupUpdatedCodec::encode(data).unwrap();
assert_eq!(encoded.clone().r#type.unwrap().type_id, "group_updated");
assert!(!encoded.content.is_empty());

let decoded = GroupUpdatedCodec::decode(encoded).unwrap();
assert_eq!(decoded.added_inboxes[0], new_member);
}
}
1 change: 1 addition & 0 deletions xmtp_mls/src/codecs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod group_updated;
pub mod membership_change;
pub mod text;

Expand Down
46 changes: 45 additions & 1 deletion xmtp_mls/src/groups/validated_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ use xmtp_id::associations::AssociationState;
use xmtp_id::InboxId;
use xmtp_proto::{
api_client::{XmtpIdentityClient, XmtpMlsClient},
xmtp::{identity::MlsCredential, mls::message_contents::GroupMembershipChanges},
xmtp::{
identity::MlsCredential,
mls::message_contents::{
group_updated::{Inbox as InboxProto, MetadataFieldChange as MetadataFieldChangeProto},
GroupMembershipChanges, GroupUpdated as GroupUpdatedProto,
},
},
};

use crate::{
Expand Down Expand Up @@ -729,6 +735,44 @@ fn extract_actor(
Err(CommitValidationError::ActorCouldNotBeFound)
}

impl From<&MetadataFieldChange> for MetadataFieldChangeProto {
fn from(change: &MetadataFieldChange) -> Self {
MetadataFieldChangeProto {
field_name: change.field_name.clone(),
old_value: change.old_value.clone(),
new_value: change.new_value.clone(),
}
}
}

impl From<&Inbox> for InboxProto {
fn from(inbox: &Inbox) -> Self {
InboxProto {
inbox_id: inbox.inbox_id.clone(),
}
}
}

impl From<ValidatedCommit> for GroupUpdatedProto {
fn from(commit: ValidatedCommit) -> Self {
GroupUpdatedProto {
initiated_by_inbox_id: commit.actor.inbox_id.clone(),
added_inboxes: commit.added_inboxes.iter().map(InboxProto::from).collect(),
removed_inboxes: commit
.removed_inboxes
.iter()
.map(InboxProto::from)
.collect(),
metadata_field_changes: commit
.metadata_changes
.metadata_field_changes
.iter()
.map(MetadataFieldChangeProto::from)
.collect(),
}
}
}

// TODO:nm bring these tests back in add/remove members PR

// #[cfg(test)]
Expand Down
Loading

0 comments on commit 66e2bd3

Please sign in to comment.