matrix_sdk_base/room/
create.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ruma::{
16    assign,
17    events::{
18        macros::EventContent,
19        room::create::{PreviousRoom, RoomCreateEventContent},
20        EmptyStateKey, RedactContent, RedactedStateEventContent,
21    },
22    room::RoomType,
23    OwnedUserId, RoomVersionId,
24};
25use serde::{Deserialize, Serialize};
26
27/// The content of an `m.room.create` event, with a required `creator` field.
28///
29/// Starting with room version 11, the `creator` field should be removed and the
30/// `sender` field of the event should be used instead. This is reflected on
31/// [`RoomCreateEventContent`].
32///
33/// This type was created as an alternative for ease of use. When it is used in
34/// the SDK, it is constructed by copying the `sender` of the original event as
35/// the `creator`.
36#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
37#[ruma_event(type = "m.room.create", kind = State, state_key_type = EmptyStateKey, custom_redacted)]
38pub struct RoomCreateWithCreatorEventContent {
39    /// The `user_id` of the room creator.
40    ///
41    /// This is set by the homeserver.
42    ///
43    /// While this should be optional since room version 11, we copy the sender
44    /// of the event so we can still access it.
45    pub creator: OwnedUserId,
46
47    /// Whether or not this room's data should be transferred to other
48    /// homeservers.
49    #[serde(
50        rename = "m.federate",
51        default = "ruma::serde::default_true",
52        skip_serializing_if = "ruma::serde::is_true"
53    )]
54    pub federate: bool,
55
56    /// The version of the room.
57    ///
58    /// Defaults to `RoomVersionId::V1`.
59    #[serde(default = "default_create_room_version_id")]
60    pub room_version: RoomVersionId,
61
62    /// A reference to the room this room replaces, if the previous room was
63    /// upgraded.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub predecessor: Option<PreviousRoom>,
66
67    /// The room type.
68    ///
69    /// This is currently only used for spaces.
70    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
71    pub room_type: Option<RoomType>,
72}
73
74impl RoomCreateWithCreatorEventContent {
75    /// Constructs a `RoomCreateWithCreatorEventContent` with the given original
76    /// content and sender.
77    pub fn from_event_content(content: RoomCreateEventContent, sender: OwnedUserId) -> Self {
78        let RoomCreateEventContent { federate, room_version, predecessor, room_type, .. } = content;
79        Self { creator: sender, federate, room_version, predecessor, room_type }
80    }
81
82    fn into_event_content(self) -> (RoomCreateEventContent, OwnedUserId) {
83        let Self { creator, federate, room_version, predecessor, room_type } = self;
84
85        #[allow(deprecated)]
86        let content = assign!(RoomCreateEventContent::new_v11(), {
87            creator: Some(creator.clone()),
88            federate,
89            room_version,
90            predecessor,
91            room_type,
92        });
93
94        (content, creator)
95    }
96}
97
98/// Redacted form of [`RoomCreateWithCreatorEventContent`].
99pub type RedactedRoomCreateWithCreatorEventContent = RoomCreateWithCreatorEventContent;
100
101impl RedactedStateEventContent for RedactedRoomCreateWithCreatorEventContent {
102    type StateKey = EmptyStateKey;
103}
104
105impl RedactContent for RoomCreateWithCreatorEventContent {
106    type Redacted = RedactedRoomCreateWithCreatorEventContent;
107
108    fn redact(self, version: &RoomVersionId) -> Self::Redacted {
109        let (content, sender) = self.into_event_content();
110        // Use Ruma's redaction algorithm.
111        let content = content.redact(version);
112        Self::from_event_content(content, sender)
113    }
114}
115
116fn default_create_room_version_id() -> RoomVersionId {
117    RoomVersionId::V1
118}