#[allow(clippy::disallowed_types)]
use std::collections::HashSet;
use as_variant::as_variant;
use crate::OwnedUserId;
#[derive(Debug, Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct RoomVersionRules {
pub disposition: RoomVersionDisposition,
pub event_id_format: EventIdFormatVersion,
pub room_id_format: RoomIdFormatVersion,
pub events_reference_format: EventsReferenceFormatVersion,
pub state_res: StateResolutionVersion,
pub enforce_key_validity: bool,
pub authorization: AuthorizationRules,
pub redaction: RedactionRules,
pub signatures: SignaturesRules,
pub event_format: EventFormatRules,
}
impl RoomVersionRules {
pub const V1: Self = Self {
disposition: RoomVersionDisposition::Stable,
event_id_format: EventIdFormatVersion::V1,
room_id_format: RoomIdFormatVersion::V1,
events_reference_format: EventsReferenceFormatVersion::V1,
state_res: StateResolutionVersion::V1,
enforce_key_validity: false,
authorization: AuthorizationRules::V1,
redaction: RedactionRules::V1,
signatures: SignaturesRules::V1,
event_format: EventFormatRules::V1,
};
pub const V2: Self =
Self { state_res: StateResolutionVersion::V2(StateResolutionV2Rules::V2_0), ..Self::V1 };
pub const V3: Self = Self {
event_id_format: EventIdFormatVersion::V2,
events_reference_format: EventsReferenceFormatVersion::V2,
authorization: AuthorizationRules::V3,
signatures: SignaturesRules::V3,
event_format: EventFormatRules::V3,
..Self::V2
};
pub const V4: Self = Self { event_id_format: EventIdFormatVersion::V3, ..Self::V3 };
pub const V5: Self = Self { enforce_key_validity: true, ..Self::V4 };
pub const V6: Self =
Self { authorization: AuthorizationRules::V6, redaction: RedactionRules::V6, ..Self::V5 };
pub const V7: Self = Self { authorization: AuthorizationRules::V7, ..Self::V6 };
pub const V8: Self = Self {
authorization: AuthorizationRules::V8,
redaction: RedactionRules::V8,
signatures: SignaturesRules::V8,
..Self::V7
};
pub const V9: Self = Self { redaction: RedactionRules::V9, ..Self::V8 };
pub const V10: Self = Self { authorization: AuthorizationRules::V10, ..Self::V9 };
pub const V11: Self = Self {
authorization: AuthorizationRules::V11,
redaction: RedactionRules::V11,
..Self::V10
};
pub const V12: Self = Self {
room_id_format: RoomIdFormatVersion::V2,
authorization: AuthorizationRules::V12,
event_format: EventFormatRules::V12,
state_res: StateResolutionVersion::V2(StateResolutionV2Rules::V2_1),
..Self::V11
};
#[cfg(feature = "unstable-msc2870")]
pub const MSC2870: Self = Self {
disposition: RoomVersionDisposition::Unstable,
redaction: RedactionRules::MSC2870,
..Self::V11
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)]
pub enum RoomVersionDisposition {
Stable,
Unstable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum EventIdFormatVersion {
V1,
V2,
V3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum RoomIdFormatVersion {
V1,
V2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum EventsReferenceFormatVersion {
V1,
V2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub enum StateResolutionVersion {
V1,
V2(StateResolutionV2Rules),
}
impl StateResolutionVersion {
pub fn v2_rules(&self) -> Option<&StateResolutionV2Rules> {
as_variant!(self, StateResolutionVersion::V2)
}
}
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StateResolutionV2Rules {
pub begin_iterative_auth_checks_with_empty_state_map: bool,
pub consider_conflicted_state_subgraph: bool,
}
impl StateResolutionV2Rules {
pub const V2_0: Self = Self {
begin_iterative_auth_checks_with_empty_state_map: false,
consider_conflicted_state_subgraph: false,
};
pub const V2_1: Self = Self {
begin_iterative_auth_checks_with_empty_state_map: true,
consider_conflicted_state_subgraph: true,
..Self::V2_0
};
}
#[derive(Debug, Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct AuthorizationRules {
pub special_case_room_redaction: bool,
pub special_case_room_aliases: bool,
pub strict_canonical_json: bool,
pub limit_notifications_power_levels: bool,
pub knocking: bool,
pub restricted_join_rule: bool,
pub knock_restricted_join_rule: bool,
pub integer_power_levels: bool,
pub use_room_create_sender: bool,
pub explicitly_privilege_room_creators: bool,
pub additional_room_creators: bool,
pub room_create_event_id_as_room_id: bool,
}
impl AuthorizationRules {
pub const V1: Self = Self {
special_case_room_redaction: true,
special_case_room_aliases: true,
strict_canonical_json: false,
limit_notifications_power_levels: false,
knocking: false,
restricted_join_rule: false,
knock_restricted_join_rule: false,
integer_power_levels: false,
use_room_create_sender: false,
explicitly_privilege_room_creators: false,
additional_room_creators: false,
room_create_event_id_as_room_id: false,
};
pub const V3: Self = Self { special_case_room_redaction: false, ..Self::V1 };
pub const V6: Self = Self {
special_case_room_aliases: false,
strict_canonical_json: true,
limit_notifications_power_levels: true,
..Self::V3
};
pub const V7: Self = Self { knocking: true, ..Self::V6 };
pub const V8: Self = Self { restricted_join_rule: true, ..Self::V7 };
pub const V10: Self =
Self { knock_restricted_join_rule: true, integer_power_levels: true, ..Self::V8 };
pub const V11: Self = Self { use_room_create_sender: true, ..Self::V10 };
pub const V12: Self = Self {
explicitly_privilege_room_creators: true,
additional_room_creators: true,
room_create_event_id_as_room_id: true,
..Self::V11
};
}
#[derive(Debug, Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct RedactionRules {
pub keep_room_aliases_aliases: bool,
pub keep_room_join_rules_allow: bool,
pub keep_room_member_join_authorised_via_users_server: bool,
pub keep_origin_membership_prev_state: bool,
pub keep_room_create_content: bool,
pub keep_room_redaction_redacts: bool,
pub keep_room_power_levels_invite: bool,
pub keep_room_member_third_party_invite_signed: bool,
pub content_field_redacts: bool,
#[cfg(feature = "unstable-msc2870")]
pub keep_room_server_acl_allow_deny_allow_ip_literals: bool,
}
impl RedactionRules {
pub const V1: Self = Self {
keep_room_aliases_aliases: true,
keep_room_join_rules_allow: false,
keep_room_member_join_authorised_via_users_server: false,
keep_origin_membership_prev_state: true,
keep_room_create_content: false,
keep_room_redaction_redacts: false,
keep_room_power_levels_invite: false,
keep_room_member_third_party_invite_signed: false,
content_field_redacts: false,
#[cfg(feature = "unstable-msc2870")]
keep_room_server_acl_allow_deny_allow_ip_literals: false,
};
pub const V6: Self = Self { keep_room_aliases_aliases: false, ..Self::V1 };
pub const V8: Self = Self { keep_room_join_rules_allow: true, ..Self::V6 };
pub const V9: Self =
Self { keep_room_member_join_authorised_via_users_server: true, ..Self::V8 };
pub const V11: Self = Self {
keep_origin_membership_prev_state: false,
keep_room_create_content: true,
keep_room_redaction_redacts: true,
keep_room_power_levels_invite: true,
keep_room_member_third_party_invite_signed: true,
content_field_redacts: true,
..Self::V9
};
#[cfg(feature = "unstable-msc2870")]
pub const MSC2870: Self =
Self { keep_room_server_acl_allow_deny_allow_ip_literals: true, ..Self::V11 };
}
#[derive(Debug, Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct SignaturesRules {
pub check_event_id_server: bool,
pub check_join_authorised_via_users_server: bool,
}
impl SignaturesRules {
pub const V1: Self =
Self { check_event_id_server: true, check_join_authorised_via_users_server: false };
pub const V3: Self = Self { check_event_id_server: false, ..Self::V1 };
pub const V8: Self = Self { check_join_authorised_via_users_server: true, ..Self::V3 };
}
#[derive(Debug, Clone)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct EventFormatRules {
pub require_event_id: bool,
pub require_room_create_room_id: bool,
pub allow_room_create_in_auth_events: bool,
}
impl EventFormatRules {
pub const V1: Self = Self {
require_event_id: true,
require_room_create_room_id: true,
allow_room_create_in_auth_events: true,
};
pub const V3: Self = Self { require_event_id: false, ..Self::V1 };
pub const V12: Self = Self {
require_room_create_room_id: false,
allow_room_create_in_auth_events: false,
..Self::V3
};
}
#[derive(Clone, Debug)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct RoomPowerLevelsRules {
#[allow(clippy::disallowed_types)]
pub privileged_creators: Option<HashSet<OwnedUserId>>,
}
impl RoomPowerLevelsRules {
pub fn new(
rules: &AuthorizationRules,
creators: impl IntoIterator<Item = OwnedUserId>,
) -> Self {
Self {
privileged_creators: rules
.explicitly_privilege_room_creators
.then(|| creators.into_iter().collect()),
}
}
}