Skip to main content

ruma_events/space/
parent.rs

1//! Types for the [`m.space.parent`] event.
2//!
3//! [`m.space.parent`]: https://spec.matrix.org/latest/client-server-api/#mspaceparent
4
5use ruma_common::{OwnedRoomId, OwnedServerName};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9/// The content of an `m.space.parent` event.
10///
11/// Rooms can claim parents via the `m.space.parent` state event.
12///
13/// Similar to `m.space.child`, the `state_key` is the ID of the parent space, and the content must
14/// contain a `via` key which gives a list of candidate servers that can be used to join the
15/// parent.
16#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
17#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
18#[ruma_event(type = "m.space.parent", kind = State, state_key_type = OwnedRoomId)]
19pub struct SpaceParentEventContent {
20    /// List of candidate servers that can be used to join the room.
21    pub via: Vec<OwnedServerName>,
22
23    /// Determines whether this is the main parent for the space.
24    ///
25    /// When a user joins a room with a canonical parent, clients may switch to view the room in
26    /// the context of that space, peeking into it in order to find other rooms and group them
27    /// together. In practice, well behaved rooms should only have one `canonical` parent, but
28    /// given this is not enforced: if multiple are present the client should select the one with
29    /// the lowest room ID, as determined via a lexicographic ordering of the Unicode code-points.
30    ///
31    /// Defaults to `false`.
32    #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
33    pub canonical: bool,
34}
35
36impl SpaceParentEventContent {
37    /// Creates a new `SpaceParentEventContent` with the given routing servers.
38    pub fn new(via: Vec<OwnedServerName>) -> Self {
39        Self { via, canonical: false }
40    }
41}
42
43impl PossiblyRedactedSpaceParentEventContent {
44    /// Whether this `PossiblyRedactedSpaceParentEventContent` is valid according to the Matrix
45    /// specification.
46    ///
47    /// The room in the state key of the event should only be considered a parent space of this room
48    /// if this returns `true`.
49    ///
50    /// Returns `false` if the `via` field is `None`.
51    pub fn is_valid(&self) -> bool {
52        self.via.is_some()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_server_name};
59    use serde_json::json;
60
61    use super::SpaceParentEventContent;
62
63    #[test]
64    fn space_parent_serialization() {
65        let content = SpaceParentEventContent {
66            via: vec![owned_server_name!("example.com")],
67            canonical: true,
68        };
69
70        assert_to_canonical_json_eq!(
71            content,
72            json!({
73                "via": ["example.com"],
74                "canonical": true,
75            })
76        );
77    }
78
79    #[test]
80    fn space_parent_empty_serialization() {
81        let content = SpaceParentEventContent { via: vec![], canonical: false };
82
83        assert_to_canonical_json_eq!(content, json!({ "via": [] }));
84    }
85}