1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Types for the *m.room.server_acl* event.

use ruma_events_macros::ruma_event;

ruma_event! {
    /// An event to indicate which servers are permitted to participate in the room.
    ServerAclEvent {
        kind: StateEvent,
        event_type: "m.room.server_acl",
        content: {
            /// True to allow server names that are IP address literals. False to deny.
            ///
            /// This is strongly recommended to be set to false as servers running with IP literal
            /// names are strongly discouraged in order to require legitimate homeservers to be
            /// backed by a valid registered domain name.
            #[serde(
                default = "ruma_serde::default_true",
                skip_serializing_if = "ruma_serde::is_true"
            )]
            pub allow_ip_literals: bool,

            /// The server names to allow in the room, excluding any port information. Wildcards may
            /// be used to cover a wider range of hosts, where `*` matches zero or more characters
            /// and `?` matches exactly one character.
            ///
            /// **This defaults to an empty list when not provided, effectively disallowing every
            /// server.**
            #[serde(default, skip_serializing_if = "Vec::is_empty")]
            pub allow: Vec<String>,

            /// The server names to disallow in the room, excluding any port information. Wildcards may
            /// be used to cover a wider range of hosts, where * matches zero or more characters and ?
            /// matches exactly one character.
            ///
            /// This defaults to an empty list when not provided.
            #[serde(default, skip_serializing_if = "Vec::is_empty")]
            pub deny: Vec<String>,
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{from_value as from_json_value, json};

    use super::ServerAclEvent;
    use crate::EventJson;

    #[test]
    fn default_values() {
        let json_data = json!({
            "content": {},
            "event_id": "$h29iv0s8:example.com","origin_server_ts":1,
            "sender": "@carl:example.com",
            "state_key": "",
            "type": "m.room.server_acl"
        });
        let server_acl_event: ServerAclEvent = from_json_value::<EventJson<_>>(json_data)
            .unwrap()
            .deserialize()
            .unwrap();

        assert_eq!(server_acl_event.content.allow_ip_literals, true);
        assert!(server_acl_event.content.allow.is_empty());
        assert!(server_acl_event.content.deny.is_empty());
    }
}