ruma_common/
room_version_rules.rs

1//! Types for the rules applied to the different [room versions].
2//!
3//! [room versions]: https://spec.matrix.org/latest/rooms/
4
5#[allow(clippy::disallowed_types)]
6use std::collections::HashSet;
7
8use as_variant::as_variant;
9
10use crate::OwnedUserId;
11
12/// The rules applied to a [room version].
13///
14/// This type can be constructed from one of its constants (like [`RoomVersionRules::V1`]), or from
15/// [`RoomVersionId::rules()`].
16///
17/// [room version]: https://spec.matrix.org/latest/rooms/
18/// [`RoomVersionId::rules()`]: crate::RoomVersionId::rules
19#[derive(Debug, Clone)]
20#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
21pub struct RoomVersionRules {
22    /// The stability of the room version.
23    pub disposition: RoomVersionDisposition,
24
25    /// The format of event IDs.
26    pub event_id_format: EventIdFormatVersion,
27
28    /// The format of room IDs.
29    pub room_id_format: RoomIdFormatVersion,
30
31    /// The format of arrays referencing events in PDUs.
32    pub events_reference_format: EventsReferenceFormatVersion,
33
34    /// The state resolution algorithm used.
35    pub state_res: StateResolutionVersion,
36
37    /// Whether to enforce the key validity period when verifying signatures ([spec]), introduced
38    /// in room version 5.
39    ///
40    /// [spec]: https://spec.matrix.org/latest/rooms/v5/#signing-key-validity-period
41    pub enforce_key_validity: bool,
42
43    /// The tweaks in the authorization rules.
44    pub authorization: AuthorizationRules,
45
46    /// The tweaks in the redaction algorithm.
47    pub redaction: RedactionRules,
48
49    /// The tweaks for verifying signatures.
50    pub signatures: SignaturesRules,
51
52    /// The tweaks for verifying the event format.
53    pub event_format: EventFormatRules,
54}
55
56impl RoomVersionRules {
57    /// Rules for [room version 1].
58    ///
59    /// [room version 1]: https://spec.matrix.org/latest/rooms/v1/
60    pub const V1: Self = Self {
61        disposition: RoomVersionDisposition::Stable,
62        event_id_format: EventIdFormatVersion::V1,
63        room_id_format: RoomIdFormatVersion::V1,
64        events_reference_format: EventsReferenceFormatVersion::V1,
65        state_res: StateResolutionVersion::V1,
66        enforce_key_validity: false,
67        authorization: AuthorizationRules::V1,
68        redaction: RedactionRules::V1,
69        signatures: SignaturesRules::V1,
70        event_format: EventFormatRules::V1,
71    };
72
73    /// Rules for [room version 2].
74    ///
75    /// [room version 2]: https://spec.matrix.org/latest/rooms/v2/
76    pub const V2: Self =
77        Self { state_res: StateResolutionVersion::V2(StateResolutionV2Rules::V2_0), ..Self::V1 };
78
79    /// Rules for [room version 3].
80    ///
81    /// [room version 3]: https://spec.matrix.org/latest/rooms/v3/
82    pub const V3: Self = Self {
83        event_id_format: EventIdFormatVersion::V2,
84        events_reference_format: EventsReferenceFormatVersion::V2,
85        authorization: AuthorizationRules::V3,
86        signatures: SignaturesRules::V3,
87        event_format: EventFormatRules::V3,
88        ..Self::V2
89    };
90
91    /// Rules for [room version 4].
92    ///
93    /// [room version 4]: https://spec.matrix.org/latest/rooms/v4/
94    pub const V4: Self = Self { event_id_format: EventIdFormatVersion::V3, ..Self::V3 };
95
96    /// Rules for [room version 5].
97    ///
98    /// [room version 5]: https://spec.matrix.org/latest/rooms/v5/
99    pub const V5: Self = Self { enforce_key_validity: true, ..Self::V4 };
100
101    /// Rules for [room version 6].
102    ///
103    /// [room version 6]: https://spec.matrix.org/latest/rooms/v6/
104    pub const V6: Self =
105        Self { authorization: AuthorizationRules::V6, redaction: RedactionRules::V6, ..Self::V5 };
106
107    /// Rules for [room version 7].
108    ///
109    /// [room version 7]: https://spec.matrix.org/latest/rooms/v7/
110    pub const V7: Self = Self { authorization: AuthorizationRules::V7, ..Self::V6 };
111
112    /// Rules for [room version 8].
113    ///
114    /// [room version 8]: https://spec.matrix.org/latest/rooms/v8/
115    pub const V8: Self = Self {
116        authorization: AuthorizationRules::V8,
117        redaction: RedactionRules::V8,
118        signatures: SignaturesRules::V8,
119        ..Self::V7
120    };
121
122    /// Rules for [room version 9].
123    ///
124    /// [room version 9]: https://spec.matrix.org/latest/rooms/v9/
125    pub const V9: Self = Self { redaction: RedactionRules::V9, ..Self::V8 };
126
127    /// Rules for [room version 10].
128    ///
129    /// [room version 10]: https://spec.matrix.org/latest/rooms/v10/
130    pub const V10: Self = Self { authorization: AuthorizationRules::V10, ..Self::V9 };
131
132    /// Rules for [room version 11].
133    ///
134    /// [room version 11]: https://spec.matrix.org/latest/rooms/v11/
135    pub const V11: Self = Self {
136        authorization: AuthorizationRules::V11,
137        redaction: RedactionRules::V11,
138        ..Self::V10
139    };
140
141    /// Rules for room version 12.
142    pub const V12: Self = Self {
143        room_id_format: RoomIdFormatVersion::V2,
144        authorization: AuthorizationRules::V12,
145        event_format: EventFormatRules::V12,
146        state_res: StateResolutionVersion::V2(StateResolutionV2Rules::V2_1),
147        ..Self::V11
148    };
149
150    /// Rules for room version `org.matrix.msc2870` ([MSC2870]).
151    ///
152    /// [MSC2870]: https://github.com/matrix-org/matrix-spec-proposals/pull/2870
153    #[cfg(feature = "unstable-msc2870")]
154    pub const MSC2870: Self = Self {
155        disposition: RoomVersionDisposition::Unstable,
156        redaction: RedactionRules::MSC2870,
157        ..Self::V11
158    };
159}
160
161/// The stability of a room version.
162#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163#[allow(clippy::exhaustive_enums)]
164pub enum RoomVersionDisposition {
165    /// A room version that has a stable specification.
166    Stable,
167
168    /// A room version that is not yet fully specified.
169    Unstable,
170}
171
172/// The format of [event IDs] for a room version.
173///
174/// [event IDs]: https://spec.matrix.org/latest/appendices/#event-ids
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
176#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
177pub enum EventIdFormatVersion {
178    /// `$id:server` format ([spec]), introduced in room version 1.
179    ///
180    /// [spec]: https://spec.matrix.org/latest/rooms/v1/#event-ids
181    V1,
182
183    /// `$hash` format using standard unpadded base64 ([spec]), introduced in room version 3.
184    ///
185    /// [spec]: https://spec.matrix.org/latest/rooms/v3/#event-ids
186    V2,
187
188    /// `$hash` format using URL-safe unpadded base64 ([spec]), introduced in room version 4.
189    ///
190    /// [spec]: https://spec.matrix.org/latest/rooms/v4/#event-ids
191    V3,
192}
193
194/// The format of [room IDs] for a room version.
195///
196/// [room IDs]: https://spec.matrix.org/latest/appendices/#room-ids
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
198#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
199pub enum RoomIdFormatVersion {
200    /// `!id:server` format, introduced in room version 1.
201    V1,
202
203    /// `!hash` format using the reference hash of the `m.room.create` event of the room,
204    /// introduced in room version 12.
205    V2,
206}
207
208/// The format of [PDU] `auth_events` and `prev_events` for a room version.
209///
210/// [PDU]: https://spec.matrix.org/latest/server-server-api/#pdus
211#[derive(Debug, Clone, Copy, PartialEq, Eq)]
212#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
213pub enum EventsReferenceFormatVersion {
214    /// `[["$id:server", {"sha256": "hash"}]]` format ([spec]), introduced in room version 1.
215    ///
216    /// [spec]: https://spec.matrix.org/latest/rooms/v1/#event-format
217    V1,
218
219    /// `["$hash"]` format ([spec]), introduced in room version 3.
220    ///
221    /// [spec]: https://spec.matrix.org/latest/rooms/v3/#event-format
222    V2,
223}
224
225/// The version of [state resolution] for a room version.
226///
227/// [state resolution]: https://spec.matrix.org/latest/server-server-api/#room-state-resolution
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
230pub enum StateResolutionVersion {
231    /// First version of the state resolution algorithm ([spec]), introduced in room version 1.
232    ///
233    /// [spec]: https://spec.matrix.org/latest/rooms/v1/#state-resolution
234    V1,
235
236    /// Second version of the state resolution algorithm ([spec]), introduced in room version 2.
237    ///
238    /// [spec]: https://spec.matrix.org/latest/rooms/v2/#state-resolution
239    V2(StateResolutionV2Rules),
240}
241
242impl StateResolutionVersion {
243    /// Gets the `StateResolutionV2Rules` for the room version, if it uses the second version of
244    /// the state resolution algorithm.
245    pub fn v2_rules(&self) -> Option<&StateResolutionV2Rules> {
246        as_variant!(self, StateResolutionVersion::V2)
247    }
248}
249
250/// The tweaks in the [state resolution v2 algorithm] for a room version.
251///
252/// This type can be constructed from one of its constants (like [`StateResolutionV2Rules::V2_0`]),
253/// or by constructing a [`RoomVersionRules`] first and using the `state_res` field (if the room
254/// version uses version 2 of the state resolution algorithm).
255///
256/// [state resolution v2 algorithm]: https://spec.matrix.org/latest/rooms/v2/#state-resolution
257#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
259pub struct StateResolutionV2Rules {
260    /// Whether to begin the first phase of iterative auth checks with an empty state map, as
261    /// opposed to one containing the unconflicted state, enabled since room version 12.
262    pub begin_iterative_auth_checks_with_empty_state_map: bool,
263
264    /// Whether to include the conflicted state subgraph in the full conflicted state, enabled
265    /// since room version 12.
266    pub consider_conflicted_state_subgraph: bool,
267}
268
269impl StateResolutionV2Rules {
270    /// The first version of the second iteration of the state resolution algorithm, introduced in
271    /// room version 2.
272    pub const V2_0: Self = Self {
273        begin_iterative_auth_checks_with_empty_state_map: false,
274        consider_conflicted_state_subgraph: false,
275    };
276
277    /// The second version of the second iteration of the state resolution algorithm, introduced in
278    /// room version 12.
279    pub const V2_1: Self = Self {
280        begin_iterative_auth_checks_with_empty_state_map: true,
281        consider_conflicted_state_subgraph: true,
282        ..Self::V2_0
283    };
284}
285
286/// The tweaks in the [authorization rules] for a room version.
287///
288/// This type can be constructed from one of its constants (like [`AuthorizationRules::V1`]), or by
289/// constructing a [`RoomVersionRules`] first and using the `authorization` field.
290///
291/// [authorization rules]: https://spec.matrix.org/latest/server-server-api/#authorization-rules
292#[derive(Debug, Clone)]
293#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
294pub struct AuthorizationRules {
295    /// Whether to apply special authorization rules for `m.room.redaction` events ([spec]),
296    /// disabled since room version 3.
297    ///
298    /// [spec]: https://spec.matrix.org/latest/rooms/v3/#handling-redactions
299    pub special_case_room_redaction: bool,
300
301    /// Whether to apply special authorization rules for `m.room.aliases` events ([spec]), disabled
302    /// since room version 6.
303    ///
304    /// [spec]: https://spec.matrix.org/latest/rooms/v6/#authorization-rules
305    pub special_case_room_aliases: bool,
306
307    /// Whether to strictly enforce [canonical JSON] ([spec]), introduced in room version 6.
308    ///
309    /// Numbers in Canonical JSON must be integers in the range [-2<sup>53</sup> + 1,
310    /// 2<sup>53</sup> - 1], represented without exponents or decimal places, and negative zero
311    /// (`-0`) MUST NOT appear.
312    ///
313    /// [canonical JSON]: https://spec.matrix.org/latest/appendices/#canonical-json
314    /// [spec]: https://spec.matrix.org/latest/rooms/v6/#canonical-json
315    pub strict_canonical_json: bool,
316
317    /// Whether to check the `notifications` field when checking `m.room.power_levels` events
318    /// ([spec]), introduced in room version 6.
319    ///
320    /// [spec]: https://spec.matrix.org/latest/rooms/v6/#authorization-rules
321    pub limit_notifications_power_levels: bool,
322
323    /// Whether to allow the `knock` membership for `m.room.member` events and the `knock` join
324    /// rule for `m.room.join_rules` events ([spec]), introduced in room version 7.
325    ///
326    /// [spec]: https://spec.matrix.org/latest/rooms/v7/#authorization-rules
327    pub knocking: bool,
328
329    /// Whether to allow the `restricted` join rule for `m.room.join_rules` events ([spec]),
330    /// introduced in room version 8.
331    ///
332    /// [spec]: https://spec.matrix.org/latest/rooms/v8/#authorization-rules
333    pub restricted_join_rule: bool,
334
335    /// Whether to allow the `knock_restricted` join rule for `m.room.join_rules` events ([spec]),
336    /// introduced in room version 10.
337    ///
338    /// [spec]: https://spec.matrix.org/latest/rooms/v10/#authorization-rules
339    pub knock_restricted_join_rule: bool,
340
341    /// Whether to enforce that power levels values in `m.room.power_levels` events be integers
342    /// ([spec]), introduced in room version 10.
343    ///
344    /// [spec]: https://spec.matrix.org/latest/rooms/v10/#values-in-mroompower_levels-events-must-be-integers
345    pub integer_power_levels: bool,
346
347    /// Whether the room creator should be determined using the `m.room.create` event's `sender`,
348    /// instead of the event content's `creator` field ([spec]), introduced in room version 11.
349    ///
350    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#event-format
351    pub use_room_create_sender: bool,
352
353    /// Whether room creators should always be considered to have "infinite" power level,
354    /// introduced in room version 12.
355    pub explicitly_privilege_room_creators: bool,
356
357    /// Whether additional room creators can be set with the `content.additional_creators` field of
358    /// an `m.room.create` event, introduced in room version 12.
359    pub additional_room_creators: bool,
360
361    /// Whether to use the event ID of the `m.room.create` event of the room as the room ID,
362    /// introduced in room version 12.
363    pub room_create_event_id_as_room_id: bool,
364}
365
366impl AuthorizationRules {
367    /// Authorization rules as introduced in room version 1 ([spec]).
368    ///
369    /// [spec]: https://spec.matrix.org/latest/rooms/v1/#authorization-rules
370    pub const V1: Self = Self {
371        special_case_room_redaction: true,
372        special_case_room_aliases: true,
373        strict_canonical_json: false,
374        limit_notifications_power_levels: false,
375        knocking: false,
376        restricted_join_rule: false,
377        knock_restricted_join_rule: false,
378        integer_power_levels: false,
379        use_room_create_sender: false,
380        explicitly_privilege_room_creators: false,
381        additional_room_creators: false,
382        room_create_event_id_as_room_id: false,
383    };
384
385    /// Authorization rules with tweaks introduced in room version 3 ([spec]).
386    ///
387    /// [spec]: https://spec.matrix.org/latest/rooms/v3/#authorization-rules
388    pub const V3: Self = Self { special_case_room_redaction: false, ..Self::V1 };
389
390    /// Authorization rules with tweaks introduced in room version 6 ([spec]).
391    ///
392    /// [spec]: https://spec.matrix.org/latest/rooms/v6/#authorization-rules
393    pub const V6: Self = Self {
394        special_case_room_aliases: false,
395        strict_canonical_json: true,
396        limit_notifications_power_levels: true,
397        ..Self::V3
398    };
399
400    /// Authorization rules with tweaks introduced in room version 7 ([spec]).
401    ///
402    /// [spec]: https://spec.matrix.org/latest/rooms/v7/#authorization-rules
403    pub const V7: Self = Self { knocking: true, ..Self::V6 };
404
405    /// Authorization rules with tweaks introduced in room version 8 ([spec]).
406    ///
407    /// [spec]: https://spec.matrix.org/latest/rooms/v8/#authorization-rules
408    pub const V8: Self = Self { restricted_join_rule: true, ..Self::V7 };
409
410    /// Authorization rules with tweaks introduced in room version 10 ([spec]).
411    ///
412    /// [spec]: https://spec.matrix.org/latest/rooms/v10/#authorization-rules
413    pub const V10: Self =
414        Self { knock_restricted_join_rule: true, integer_power_levels: true, ..Self::V8 };
415
416    /// Authorization rules with tweaks introduced in room version 11 ([spec]).
417    ///
418    /// [spec]: https://spec.matrix.org/latest/rooms/v11/#authorization-rules
419    pub const V11: Self = Self { use_room_create_sender: true, ..Self::V10 };
420
421    /// Authorization rules with tweaks introduced in room version 12.
422    pub const V12: Self = Self {
423        explicitly_privilege_room_creators: true,
424        additional_room_creators: true,
425        room_create_event_id_as_room_id: true,
426        ..Self::V11
427    };
428}
429
430/// The tweaks in the [redaction] algorithm for a room version.
431///
432/// This type can be constructed from one of its constants (like [`RedactionRules::V1`]), or by
433/// constructing a [`RoomVersionRules`] first and using the `redaction` field.
434///
435/// [redaction]: https://spec.matrix.org/latest/client-server-api/#redactions
436#[derive(Debug, Clone)]
437#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
438pub struct RedactionRules {
439    /// Whether to keep the `aliases` field in the `content` of `m.room.aliases` events ([spec]),
440    /// disabled since room version 6.
441    ///
442    /// [spec]: https://spec.matrix.org/v1.16/rooms/v6/#redactions
443    pub keep_room_aliases_aliases: bool,
444
445    /// Whether to keep the `allow` field in the `content` of `m.room.join_rules` events ([spec]),
446    /// introduced in room version 8.
447    ///
448    /// [spec]: https://spec.matrix.org/v1.16/rooms/v8/#redactions
449    pub keep_room_join_rules_allow: bool,
450
451    /// Whether to keep the `join_authorised_via_users_server` field in the `content` of
452    /// `m.room.member` events ([spec]), introduced in room version 9.
453    ///
454    /// [spec]: https://spec.matrix.org/v1.16/rooms/v9/#redactions
455    pub keep_room_member_join_authorised_via_users_server: bool,
456
457    /// Whether to keep the `origin`, `membership` and `prev_state` fields a the top-level of all
458    /// events ([spec]), disabled since room version 11.
459    ///
460    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
461    pub keep_origin_membership_prev_state: bool,
462
463    /// Whether to keep the entire `content` of `m.room.create` events ([spec]), introduced in room
464    /// version 11.
465    ///
466    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
467    pub keep_room_create_content: bool,
468
469    /// Whether to keep the `redacts` field in the `content` of `m.room.redaction` events ([spec]),
470    /// introduced in room version 11.
471    ///
472    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
473    pub keep_room_redaction_redacts: bool,
474
475    /// Whether to keep the `invite` field in the `content` of `m.room.power_levels` events
476    /// ([spec]), introduced in room version 11.
477    ///
478    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
479    pub keep_room_power_levels_invite: bool,
480
481    /// Whether to keep the `signed` field in `third_party_invite` of the `content` of
482    /// `m.room.member` events ([spec]), introduced in room version 11.
483    ///
484    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
485    pub keep_room_member_third_party_invite_signed: bool,
486
487    /// Whether the `content.redacts` field should be used to determine the event an event
488    /// redacts, as opposed to the top-level `redacts` field ([spec]), introduced in room version
489    /// 11.
490    ///
491    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
492    pub content_field_redacts: bool,
493
494    /// Whether to keep the `allow`, `deny` and `allow_ip_literals` in the `content` of
495    /// `m.room.server_acl` events ([MSC2870]).
496    ///
497    /// [MSC2870]: https://github.com/matrix-org/matrix-spec-proposals/pull/2870
498    #[cfg(feature = "unstable-msc2870")]
499    pub keep_room_server_acl_allow_deny_allow_ip_literals: bool,
500}
501
502impl RedactionRules {
503    /// Redaction rules as introduced in room version 1 ([spec]).
504    ///
505    /// [spec]: https://spec.matrix.org/v1.16/rooms/v1/#redactions
506    pub const V1: Self = Self {
507        keep_room_aliases_aliases: true,
508        keep_room_join_rules_allow: false,
509        keep_room_member_join_authorised_via_users_server: false,
510        keep_origin_membership_prev_state: true,
511        keep_room_create_content: false,
512        keep_room_redaction_redacts: false,
513        keep_room_power_levels_invite: false,
514        keep_room_member_third_party_invite_signed: false,
515        content_field_redacts: false,
516        #[cfg(feature = "unstable-msc2870")]
517        keep_room_server_acl_allow_deny_allow_ip_literals: false,
518    };
519
520    /// Redaction rules with tweaks introduced in room version 6 ([spec]).
521    ///
522    /// [spec]: https://spec.matrix.org/v1.16/rooms/v6/#redactions
523    pub const V6: Self = Self { keep_room_aliases_aliases: false, ..Self::V1 };
524
525    /// Redaction rules with tweaks introduced in room version 8 ([spec]).
526    ///
527    /// [spec]: https://spec.matrix.org/v1.16/rooms/v8/#redactions
528    pub const V8: Self = Self { keep_room_join_rules_allow: true, ..Self::V6 };
529
530    /// Redaction rules with tweaks introduced in room version 9 ([spec]).
531    ///
532    /// [spec]: https://spec.matrix.org/v1.16/rooms/v9/#redactions
533    pub const V9: Self =
534        Self { keep_room_member_join_authorised_via_users_server: true, ..Self::V8 };
535
536    /// Redaction rules with tweaks introduced in room version 11 ([spec]).
537    ///
538    /// [spec]: https://spec.matrix.org/v1.16/rooms/v11/#redactions
539    pub const V11: Self = Self {
540        keep_origin_membership_prev_state: false,
541        keep_room_create_content: true,
542        keep_room_redaction_redacts: true,
543        keep_room_power_levels_invite: true,
544        keep_room_member_third_party_invite_signed: true,
545        content_field_redacts: true,
546        ..Self::V9
547    };
548
549    /// Redaction rules with tweaks introduced in [MSC2870].
550    ///
551    /// [MSC2870]: https://github.com/matrix-org/matrix-spec-proposals/pull/2870
552    #[cfg(feature = "unstable-msc2870")]
553    pub const MSC2870: Self =
554        Self { keep_room_server_acl_allow_deny_allow_ip_literals: true, ..Self::V11 };
555}
556
557/// The tweaks for [verifying the signatures] for a room version.
558///
559/// This type can be constructed from one of its constants (like [`SignaturesRules::V1`]), or by
560/// constructing a [`RoomVersionRules`] first and using the `signatures` field.
561///
562/// [verifying the signatures]: https://spec.matrix.org/latest/server-server-api/#validating-hashes-and-signatures-on-received-events
563#[derive(Debug, Clone)]
564#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
565pub struct SignaturesRules {
566    /// Whether to check the server of the event ID, disabled since room version 3.
567    pub check_event_id_server: bool,
568
569    /// Whether to check the server of the `join_authorised_via_users_server` field in the
570    /// `content` of `m.room.member` events ([spec]), introduced in room version 8.
571    ///
572    /// [spec]: https://spec.matrix.org/latest/rooms/v8/#authorization-rules
573    pub check_join_authorised_via_users_server: bool,
574}
575
576impl SignaturesRules {
577    /// Signatures verification rules as introduced in room version 1.
578    pub const V1: Self =
579        Self { check_event_id_server: true, check_join_authorised_via_users_server: false };
580
581    /// Signatures verification rules with tweaks introduced in room version 3.
582    pub const V3: Self = Self { check_event_id_server: false, ..Self::V1 };
583
584    /// Signatures verification rules with tweaks introduced in room version 8.
585    pub const V8: Self = Self { check_join_authorised_via_users_server: true, ..Self::V3 };
586}
587
588/// The tweaks for verifying the event format for a room version.
589///
590/// This type can be constructed from one of its constants (like [`EventFormatRules::V1`]), or by
591/// constructing a [`RoomVersionRules`] first and using the `event_format` field.
592#[derive(Debug, Clone)]
593#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
594pub struct EventFormatRules {
595    /// Whether the `event_id` field is required, disabled since room version 3.
596    pub require_event_id: bool,
597
598    /// Whether the `room_id` field is required on the `m.room.create` event, disabled since room
599    /// version 12.
600    pub require_room_create_room_id: bool,
601
602    /// Whether the `m.room.create` event is allowed to be in the `auth_events`, disabled since
603    /// room version 12.
604    pub allow_room_create_in_auth_events: bool,
605}
606
607impl EventFormatRules {
608    /// Event format rules as introduced in room version 1.
609    pub const V1: Self = Self {
610        require_event_id: true,
611        require_room_create_room_id: true,
612        allow_room_create_in_auth_events: true,
613    };
614
615    /// Event format rules with tweaks introduced in room version 3.
616    pub const V3: Self = Self { require_event_id: false, ..Self::V1 };
617
618    /// Event format rules with tweaks introduced in room version 12.
619    pub const V12: Self = Self {
620        require_room_create_room_id: false,
621        allow_room_create_in_auth_events: false,
622        ..Self::V3
623    };
624}
625
626/// The tweaks for determining the power level of a user.
627#[derive(Clone, Debug)]
628#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
629pub struct RoomPowerLevelsRules {
630    /// The creator(s) of the room which are considered to have "infinite" power level, introduced
631    /// in room version 12.
632    #[allow(clippy::disallowed_types)]
633    pub privileged_creators: Option<HashSet<OwnedUserId>>,
634}
635
636impl RoomPowerLevelsRules {
637    /// Creates a new `RoomPowerLevelsRules` from the given parameters, using the `creators` if
638    /// the rule `explicitly_privilege_room_creators` is `true`.
639    pub fn new(
640        rules: &AuthorizationRules,
641        creators: impl IntoIterator<Item = OwnedUserId>,
642    ) -> Self {
643        Self {
644            privileged_creators: rules
645                .explicitly_privilege_room_creators
646                .then(|| creators.into_iter().collect()),
647        }
648    }
649}