Skip to main content

xmtp_content_types/
read_receipt.rs

1use std::collections::HashMap;
2
3use crate::{CodecError, ContentCodec};
4use serde::{Deserialize, Serialize};
5
6use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent};
7
8pub struct ReadReceiptCodec {}
9
10/// Legacy content type id at <https://github.com/xmtp/xmtp-js/blob/main/content-types/content-type-read-receipt/src/ReadReceipt.ts>
11impl ReadReceiptCodec {
12    const AUTHORITY_ID: &'static str = "xmtp.org";
13    pub const TYPE_ID: &'static str = "readReceipt";
14    pub const MAJOR_VERSION: u32 = 1;
15    pub const MINOR_VERSION: u32 = 0;
16}
17
18impl ContentCodec<ReadReceipt> for ReadReceiptCodec {
19    fn content_type() -> ContentTypeId {
20        ContentTypeId {
21            authority_id: Self::AUTHORITY_ID.to_string(),
22            type_id: Self::TYPE_ID.to_string(),
23            version_major: ReadReceiptCodec::MAJOR_VERSION,
24            version_minor: ReadReceiptCodec::MINOR_VERSION,
25        }
26    }
27
28    fn encode(_: ReadReceipt) -> Result<EncodedContent, CodecError> {
29        Ok(EncodedContent {
30            r#type: Some(Self::content_type()),
31            parameters: HashMap::new(),
32            fallback: None,
33            compression: None,
34            content: vec![],
35        })
36    }
37
38    fn decode(_: EncodedContent) -> Result<ReadReceipt, CodecError> {
39        Ok(ReadReceipt {})
40    }
41
42    fn should_push() -> bool {
43        false
44    }
45}
46
47/// The main content type for read receipts
48#[derive(Debug, Serialize, Deserialize, Clone)]
49pub struct ReadReceipt {}
50
51#[cfg(test)]
52pub(crate) mod tests {
53    use super::*;
54
55    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
56    #[cfg_attr(not(target_arch = "wasm32"), test)]
57    fn test_encode_decode_read_receipt() {
58        let read_receipt = ReadReceipt {};
59
60        let encoded = ReadReceiptCodec::encode(read_receipt.clone()).unwrap();
61        ReadReceiptCodec::decode(encoded).unwrap();
62    }
63}