1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use ruma_common::RoomVersionId;

use crate::{Error, Result};

#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
pub enum RoomDisposition {
    /// A room version that has a stable specification.
    Stable,
    /// A room version that is not yet fully specified.
    Unstable,
}

#[derive(Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum EventFormatVersion {
    /// $id:server event id format
    V1,
    /// MSC1659-style $hash event id format: introduced for room v3
    V2,
    /// MSC1884-style $hash format: introduced for room v4
    V3,
}

#[derive(Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum StateResolutionVersion {
    /// State resolution for rooms at version 1.
    V1,
    /// State resolution for room at version 2 or later.
    V2,
}

#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct RoomVersion {
    /// The stability of this room.
    pub disposition: RoomDisposition,
    /// The format of the EventId.
    pub event_format: EventFormatVersion,
    /// Which state resolution algorithm is used.
    pub state_res: StateResolutionVersion,
    // FIXME: not sure what this one means?
    pub enforce_key_validity: bool,

    /// `m.room.aliases` had special auth rules and redaction rules
    /// before room version 6.
    ///
    /// before MSC2261/MSC2432,
    pub special_case_aliases_auth: bool,
    /// Strictly enforce canonical json, do not allow:
    /// * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
    /// * Floats
    /// * NaN, Infinity, -Infinity
    pub strict_canonicaljson: bool,
    /// Verify notifications key while checking m.room.power_levels.
    ///
    /// bool: MSC2209: Check 'notifications'
    pub limit_notifications_power_levels: bool,
    /// Extra rules when verifying redaction events.
    pub extra_redaction_checks: bool,
    /// Allow knocking in event authentication.
    ///
    /// See [room v7 specification](https://spec.matrix.org/latest/rooms/v7/) for more information.
    pub allow_knocking: bool,
    /// Adds support for the restricted join rule.
    ///
    /// See: [MSC3289](https://github.com/matrix-org/matrix-spec-proposals/pull/3289) for more information.
    pub restricted_join_rules: bool,
    /// Adds support for the knock_restricted join rule.
    ///
    /// See: [MSC3787](https://github.com/matrix-org/matrix-spec-proposals/pull/3787) for more information.
    pub knock_restricted_join_rule: bool,
    /// Enforces integer power levels.
    ///
    /// See: [MSC3667](https://github.com/matrix-org/matrix-spec-proposals/pull/3667) for more information.
    pub integer_power_levels: bool,
    /// Determine the room creator using the `m.room.create` event's `sender`,
    /// instead of the event content's `creator` field.
    ///
    /// See: [MSC2175](https://github.com/matrix-org/matrix-spec-proposals/pull/2175) for more information.
    pub use_room_create_sender: bool,
}

impl RoomVersion {
    pub const V1: Self = Self {
        disposition: RoomDisposition::Stable,
        event_format: EventFormatVersion::V1,
        state_res: StateResolutionVersion::V1,
        enforce_key_validity: false,
        special_case_aliases_auth: true,
        strict_canonicaljson: false,
        limit_notifications_power_levels: false,
        extra_redaction_checks: false,
        allow_knocking: false,
        restricted_join_rules: false,
        knock_restricted_join_rule: false,
        integer_power_levels: false,
        use_room_create_sender: false,
    };

    pub const V2: Self = Self { state_res: StateResolutionVersion::V2, ..Self::V1 };

    pub const V3: Self =
        Self { event_format: EventFormatVersion::V2, extra_redaction_checks: true, ..Self::V2 };

    pub const V4: Self = Self { event_format: EventFormatVersion::V3, ..Self::V3 };

    pub const V5: Self = Self { enforce_key_validity: true, ..Self::V4 };

    pub const V6: Self = Self {
        special_case_aliases_auth: false,
        strict_canonicaljson: true,
        limit_notifications_power_levels: true,
        ..Self::V5
    };

    pub const V7: Self = Self { allow_knocking: true, ..Self::V6 };

    pub const V8: Self = Self { restricted_join_rules: true, ..Self::V7 };

    pub const V9: Self = Self::V8;

    pub const V10: Self =
        Self { knock_restricted_join_rule: true, integer_power_levels: true, ..Self::V9 };

    pub const V11: Self = Self { use_room_create_sender: true, ..Self::V10 };

    pub fn new(version: &RoomVersionId) -> Result<Self> {
        Ok(match version {
            RoomVersionId::V1 => Self::V1,
            RoomVersionId::V2 => Self::V2,
            RoomVersionId::V3 => Self::V3,
            RoomVersionId::V4 => Self::V4,
            RoomVersionId::V5 => Self::V5,
            RoomVersionId::V6 => Self::V6,
            RoomVersionId::V7 => Self::V7,
            RoomVersionId::V8 => Self::V8,
            RoomVersionId::V9 => Self::V9,
            RoomVersionId::V10 => Self::V10,
            RoomVersionId::V11 => Self::V11,
            ver => return Err(Error::Unsupported(format!("found version `{ver}`"))),
        })
    }
}