ruma_events/
marked_unread.rs

1//! Types for the [`m.marked_unread`] event.
2//!
3//! [`m.marked_unread`]: https://spec.matrix.org/latest/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/// The content of a [`com.famedly.marked_unread`] event, the unstable version of
29/// [MarkedUnreadEventContent].
30///
31/// Whether the room has been explicitly marked as unread.
32///
33/// This event appears in the user's room account data for the room the marker is applicable for.
34///
35/// [`com.famedly.marked_unread`]: https://github.com/matrix-org/matrix-spec-proposals/pull/2867
36#[cfg(feature = "unstable-msc2867")]
37#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
38#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
39#[ruma_event(type = "com.famedly.marked_unread", kind = RoomAccountData)]
40#[serde(transparent)]
41pub struct UnstableMarkedUnreadEventContent(pub MarkedUnreadEventContent);
42
43#[cfg(feature = "unstable-msc2867")]
44impl std::ops::Deref for UnstableMarkedUnreadEventContent {
45    type Target = MarkedUnreadEventContent;
46
47    fn deref(&self) -> &Self::Target {
48        &self.0
49    }
50}
51
52#[cfg(feature = "unstable-msc2867")]
53impl From<MarkedUnreadEventContent> for UnstableMarkedUnreadEventContent {
54    fn from(value: MarkedUnreadEventContent) -> Self {
55        Self(value)
56    }
57}
58
59#[cfg(feature = "unstable-msc2867")]
60impl From<UnstableMarkedUnreadEventContent> for MarkedUnreadEventContent {
61    fn from(value: UnstableMarkedUnreadEventContent) -> Self {
62        value.0
63    }
64}
65
66#[cfg(all(test, feature = "unstable-msc2867"))]
67mod tests {
68    use assert_matches2::assert_matches;
69    use ruma_common::canonical_json::assert_to_canonical_json_eq;
70    use serde_json::{from_value as from_json_value, json};
71
72    use super::{MarkedUnreadEventContent, UnstableMarkedUnreadEventContent};
73    use crate::{AnyRoomAccountDataEvent, RoomAccountDataEvent};
74
75    #[test]
76    fn deserialize() {
77        let raw_unstable_marked_unread = json!({
78            "type": "com.famedly.marked_unread",
79            "content": {
80                "unread": true,
81            },
82        });
83        let unstable_marked_unread_account_data =
84            from_json_value::<AnyRoomAccountDataEvent>(raw_unstable_marked_unread).unwrap();
85        assert_matches!(
86            unstable_marked_unread_account_data,
87            AnyRoomAccountDataEvent::UnstableMarkedUnread(unstable_marked_unread)
88        );
89        assert!(unstable_marked_unread.content.unread);
90
91        let raw_marked_unread = json!({
92            "type": "m.marked_unread",
93            "content": {
94                "unread": true,
95            },
96        });
97        let marked_unread_account_data =
98            from_json_value::<AnyRoomAccountDataEvent>(raw_marked_unread).unwrap();
99        assert_matches!(
100            marked_unread_account_data,
101            AnyRoomAccountDataEvent::MarkedUnread(marked_unread)
102        );
103        assert!(marked_unread.content.unread);
104    }
105
106    #[test]
107    fn serialize() {
108        let marked_unread = MarkedUnreadEventContent::new(true);
109        let marked_unread_account_data = RoomAccountDataEvent { content: marked_unread.clone() };
110        assert_to_canonical_json_eq!(
111            marked_unread_account_data,
112            json!({
113                "type": "m.marked_unread",
114                "content": {
115                    "unread": true,
116                },
117            })
118        );
119
120        let unstable_marked_unread = UnstableMarkedUnreadEventContent::from(marked_unread);
121        let unstable_marked_unread_account_data =
122            RoomAccountDataEvent { content: unstable_marked_unread };
123        assert_to_canonical_json_eq!(
124            unstable_marked_unread_account_data,
125            json!({
126                "type": "com.famedly.marked_unread",
127                "content": {
128                    "unread": true,
129                },
130            })
131        );
132    }
133}