matrix_sdk_base/room/
create.rs1use matrix_sdk_common::ROOM_VERSION_RULES_FALLBACK;
16use ruma::{
17 OwnedUserId, RoomVersionId, assign,
18 events::{
19 EmptyStateKey, PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent,
20 StateEventContent, StateEventType, StaticEventContent,
21 macros::EventContent,
22 room::create::{PreviousRoom, RoomCreateEventContent},
23 },
24 room::RoomType,
25 room_version_rules::RedactionRules,
26};
27use serde::{Deserialize, Serialize};
28
29#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
39#[ruma_event(type = "m.room.create", kind = State, state_key_type = EmptyStateKey, custom_redacted)]
40pub struct RoomCreateWithCreatorEventContent {
41 pub creator: OwnedUserId,
48
49 #[serde(
52 rename = "m.federate",
53 default = "ruma::serde::default_true",
54 skip_serializing_if = "ruma::serde::is_true"
55 )]
56 pub federate: bool,
57
58 #[serde(default = "default_create_room_version_id")]
62 pub room_version: RoomVersionId,
63
64 #[serde(skip_serializing_if = "Option::is_none")]
67 pub predecessor: Option<PreviousRoom>,
68
69 #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
73 pub room_type: Option<RoomType>,
74
75 #[serde(skip_serializing_if = "Vec::is_empty", default)]
78 pub additional_creators: Vec<OwnedUserId>,
79}
80
81impl RoomCreateWithCreatorEventContent {
82 pub fn from_event_content(content: RoomCreateEventContent, sender: OwnedUserId) -> Self {
85 let RoomCreateEventContent {
86 federate,
87 room_version,
88 predecessor,
89 room_type,
90 additional_creators,
91 ..
92 } = content;
93 Self {
94 creator: sender,
95 federate,
96 room_version,
97 predecessor,
98 room_type,
99 additional_creators,
100 }
101 }
102
103 fn into_event_content(self) -> (RoomCreateEventContent, OwnedUserId) {
104 let Self { creator, federate, room_version, predecessor, room_type, additional_creators } =
105 self;
106
107 #[allow(deprecated)]
108 let content = assign!(RoomCreateEventContent::new_v11(), {
109 creator: Some(creator.clone()),
110 federate,
111 room_version,
112 predecessor,
113 room_type,
114 additional_creators,
115 });
116
117 (content, creator)
118 }
119
120 pub(crate) fn creators(&self) -> Vec<OwnedUserId> {
123 let rules = self.room_version.rules().unwrap_or(ROOM_VERSION_RULES_FALLBACK);
124
125 if rules.authorization.explicitly_privilege_room_creators {
126 std::iter::once(self.creator.clone())
127 .chain(self.additional_creators.iter().cloned())
128 .collect()
129 } else {
130 vec![self.creator.clone()]
131 }
132 }
133}
134
135pub type RedactedRoomCreateWithCreatorEventContent = RoomCreateWithCreatorEventContent;
137
138impl RedactedStateEventContent for RedactedRoomCreateWithCreatorEventContent {
139 type StateKey = <RoomCreateWithCreatorEventContent as StateEventContent>::StateKey;
140
141 fn event_type(&self) -> StateEventType {
142 RoomCreateWithCreatorEventContent::TYPE.into()
143 }
144}
145
146impl RedactContent for RoomCreateWithCreatorEventContent {
147 type Redacted = RedactedRoomCreateWithCreatorEventContent;
148
149 fn redact(self, rules: &RedactionRules) -> Self::Redacted {
150 let (content, sender) = self.into_event_content();
151 let content = content.redact(rules);
153 Self::from_event_content(content, sender)
154 }
155}
156
157fn default_create_room_version_id() -> RoomVersionId {
158 RoomVersionId::V1
159}
160
161impl PossiblyRedactedStateEventContent for RoomCreateWithCreatorEventContent {
162 type StateKey = <RoomCreateWithCreatorEventContent as StateEventContent>::StateKey;
163
164 fn event_type(&self) -> StateEventType {
165 RoomCreateWithCreatorEventContent::TYPE.into()
166 }
167}