Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new GroupUpdated proto #754

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading