Skip to main content

ruma_events/
marked_unread.rs

1//! Types for the [`m.marked_unread`] event.
2//!
3//! [`m.marked_unread`]: https://spec.matrix.org/v1.19/client-server-api/#unread-markers
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8/// The content of an `m.marked_unread` event.
9///
10/// Whether the room has been explicitly marked as unread.
11///
12/// This event appears in the user's room account data for the room the marker is applicable for.
13#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15#[ruma_event(type = "m.marked_unread", kind = RoomAccountData)]
16pub struct MarkedUnreadEventContent {
17    /// The current unread state.
18    pub unread: bool,
19}
20
21impl MarkedUnreadEventContent {
22    /// Creates a new `MarkedUnreadEventContent` with the given value.
23    pub fn new(unread: bool) -> Self {
24        Self { unread }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use ruma_common::canonical_json::assert_to_canonical_json_eq;
31    use serde_json::{from_value as from_json_value, json};
32    use strass::assert_let;
33
34    use super::MarkedUnreadEventContent;
35    use crate::{AnyRoomAccountDataEvent, RoomAccountDataEvent};
36
37    #[test]
38    fn deserialize() {
39        let raw_marked_unread = json!({
40            "type": "m.marked_unread",
41            "content": {
42                "unread": true,
43            },
44        });
45        let marked_unread_account_data =
46            from_json_value::<AnyRoomAccountDataEvent>(raw_marked_unread).unwrap();
47        assert_let!(
48            AnyRoomAccountDataEvent::MarkedUnread(marked_unread) = marked_unread_account_data
49        );
50        assert!(marked_unread.content.unread);
51    }
52
53    #[test]
54    fn serialize() {
55        let marked_unread = MarkedUnreadEventContent::new(true);
56        let marked_unread_account_data = RoomAccountDataEvent { content: marked_unread };
57        assert_to_canonical_json_eq!(
58            marked_unread_account_data,
59            json!({
60                "type": "m.marked_unread",
61                "content": {
62                    "unread": true,
63                },
64            })
65        );
66    }
67}