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