Skip to main content

xmtp_content_types/
delete_message.rs

1use std::collections::HashMap;
2
3use prost::Message;
4
5use super::{CodecError, ContentCodec};
6use xmtp_proto::xmtp::mls::message_contents::content_types::DeleteMessage;
7use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent};
8
9pub struct DeleteMessageCodec;
10
11impl DeleteMessageCodec {
12    const AUTHORITY_ID: &'static str = "xmtp.org";
13    pub const TYPE_ID: &'static str = "deleteMessage";
14    pub const MAJOR_VERSION: u32 = 1;
15    pub const MINOR_VERSION: u32 = 0;
16}
17
18impl ContentCodec<DeleteMessage> for DeleteMessageCodec {
19    fn content_type() -> ContentTypeId {
20        ContentTypeId {
21            authority_id: DeleteMessageCodec::AUTHORITY_ID.to_string(),
22            type_id: DeleteMessageCodec::TYPE_ID.to_string(),
23            version_major: DeleteMessageCodec::MAJOR_VERSION,
24            version_minor: DeleteMessageCodec::MINOR_VERSION,
25        }
26    }
27
28    fn encode(data: DeleteMessage) -> Result<EncodedContent, CodecError> {
29        let mut buf = Vec::new();
30        data.encode(&mut buf)
31            .map_err(|e| CodecError::Encode(e.to_string()))?;
32
33        Ok(EncodedContent {
34            r#type: Some(DeleteMessageCodec::content_type()),
35            parameters: HashMap::new(),
36            fallback: None,
37            compression: None,
38            content: buf,
39        })
40    }
41
42    fn decode(content: EncodedContent) -> Result<DeleteMessage, CodecError> {
43        let decoded = DeleteMessage::decode(content.content.as_slice())
44            .map_err(|e| CodecError::Decode(e.to_string()))?;
45
46        Ok(decoded)
47    }
48
49    fn should_push() -> bool {
50        false
51    }
52}
53
54#[cfg(test)]
55pub(crate) mod tests {
56    use super::*;
57
58    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
59    #[cfg_attr(not(target_arch = "wasm32"), test)]
60    fn test_encode_decode() {
61        let data = DeleteMessage {
62            message_id: "test_message_id_123".to_string(),
63        };
64
65        let encoded = DeleteMessageCodec::encode(data.clone()).unwrap();
66        assert_eq!(encoded.clone().r#type.unwrap().type_id, "deleteMessage");
67
68        let decoded = DeleteMessageCodec::decode(encoded).unwrap();
69        assert_eq!(decoded.message_id, data.message_id);
70    }
71}