-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,147 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.