ruma_events/room/
canonical_alias.rs

1//! Types for the [`m.room.canonical_alias`] event.
2//!
3//! [`m.room.canonical_alias`]: https://spec.matrix.org/latest/client-server-api/#mroomcanonical_alias
4
5use ruma_common::OwnedRoomAliasId;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use crate::EmptyStateKey;
10
11/// The content of an `m.room.canonical_alias` event.
12///
13/// Informs the room as to which alias is the canonical one.
14#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.room.canonical_alias", kind = State, state_key_type = EmptyStateKey)]
17pub struct RoomCanonicalAliasEventContent {
18    /// The canonical alias.
19    ///
20    /// Rooms with `alias: None` should be treated the same as a room
21    /// with no canonical alias.
22    #[serde(
23        default,
24        deserialize_with = "ruma_common::serde::empty_string_as_none",
25        skip_serializing_if = "Option::is_none"
26    )]
27    pub alias: Option<OwnedRoomAliasId>,
28
29    /// List of alternative aliases to the room.
30    #[serde(default, skip_serializing_if = "Vec::is_empty")]
31    pub alt_aliases: Vec<OwnedRoomAliasId>,
32}
33
34impl RoomCanonicalAliasEventContent {
35    /// Creates an empty `RoomCanonicalAliasEventContent`.
36    pub fn new() -> Self {
37        Self { alias: None, alt_aliases: Vec::new() }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use ruma_common::owned_room_alias_id;
44    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
45
46    use super::RoomCanonicalAliasEventContent;
47    use crate::OriginalStateEvent;
48
49    #[test]
50    fn serialization_with_optional_fields_as_none() {
51        let content = RoomCanonicalAliasEventContent {
52            alias: Some(owned_room_alias_id!("#somewhere:localhost")),
53            alt_aliases: Vec::new(),
54        };
55
56        let actual = to_json_value(&content).unwrap();
57        let expected = json!({
58            "alias": "#somewhere:localhost",
59        });
60
61        assert_eq!(actual, expected);
62    }
63
64    #[test]
65    fn absent_field_as_none() {
66        let json_data = json!({
67            "content": {},
68            "event_id": "$h29iv0s8:example.com",
69            "origin_server_ts": 1,
70            "room_id": "!dummy:example.com",
71            "sender": "@carl:example.com",
72            "state_key": "",
73            "type": "m.room.canonical_alias"
74        });
75
76        assert_eq!(
77            from_json_value::<OriginalStateEvent<RoomCanonicalAliasEventContent>>(json_data)
78                .unwrap()
79                .content
80                .alias,
81            None
82        );
83    }
84
85    #[test]
86    fn null_field_as_none() {
87        let json_data = json!({
88            "content": {
89                "alias": null
90            },
91            "event_id": "$h29iv0s8:example.com",
92            "origin_server_ts": 1,
93            "room_id": "!dummy:example.com",
94            "sender": "@carl:example.com",
95            "state_key": "",
96            "type": "m.room.canonical_alias"
97        });
98        assert_eq!(
99            from_json_value::<OriginalStateEvent<RoomCanonicalAliasEventContent>>(json_data)
100                .unwrap()
101                .content
102                .alias,
103            None
104        );
105    }
106
107    #[test]
108    fn empty_field_as_none() {
109        let json_data = json!({
110            "content": {
111                "alias": ""
112            },
113            "event_id": "$h29iv0s8:example.com",
114            "origin_server_ts": 1,
115            "room_id": "!dummy:example.com",
116            "sender": "@carl:example.com",
117            "state_key": "",
118            "type": "m.room.canonical_alias"
119        });
120        assert_eq!(
121            from_json_value::<OriginalStateEvent<RoomCanonicalAliasEventContent>>(json_data)
122                .unwrap()
123                .content
124                .alias,
125            None
126        );
127    }
128
129    #[test]
130    fn nonempty_field_as_some() {
131        let alias = Some(owned_room_alias_id!("#somewhere:localhost"));
132        let json_data = json!({
133            "content": {
134                "alias": "#somewhere:localhost"
135            },
136            "event_id": "$h29iv0s8:example.com",
137            "origin_server_ts": 1,
138            "room_id": "!dummy:example.com",
139            "sender": "@carl:example.com",
140            "state_key": "",
141            "type": "m.room.canonical_alias"
142        });
143        assert_eq!(
144            from_json_value::<OriginalStateEvent<RoomCanonicalAliasEventContent>>(json_data)
145                .unwrap()
146                .content
147                .alias,
148            alias
149        );
150    }
151}