From a9f23f663834db818118379da00162c17d4756fa Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Sep 2021 20:28:52 +0200 Subject: [PATCH 1/2] Add allow to JoinRulesEventContent Co-authored-by: Amanda Graven --- crates/ruma-events/CHANGELOG.md | 4 + crates/ruma-events/src/room/join_rules.rs | 98 ++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index ea540838..064822c6 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Improvements: + +* Add (unstable) support for [MSC3083](https://github.com/matrix-org/matrix-doc/blob/main/proposals/3083-restricted-rooms.md) + # 0.24.5 Improvements: diff --git a/crates/ruma-events/src/room/join_rules.rs b/crates/ruma-events/src/room/join_rules.rs index 93415246..486a6508 100644 --- a/crates/ruma-events/src/room/join_rules.rs +++ b/crates/ruma-events/src/room/join_rules.rs @@ -1,8 +1,17 @@ //! Types for the *m.room.join_rules* event. +#[cfg(feature = "unstable-pre-spec")] +use std::collections::BTreeMap; + use ruma_events_macros::EventContent; +#[cfg(feature = "unstable-pre-spec")] +use ruma_identifiers::RoomId; use ruma_serde::StringEnum; +#[cfg(feature = "unstable-pre-spec")] +use serde::de::{DeserializeOwned, Deserializer, Error}; use serde::{Deserialize, Serialize}; +#[cfg(feature = "unstable-pre-spec")] +use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue}; use crate::StateEvent; @@ -17,12 +26,29 @@ pub struct JoinRulesEventContent { /// The type of rules used for users wishing to join this room. #[ruma_event(skip_redaction)] pub join_rule: JoinRule, + + /// Allow rules used for the `restricted` join rule. + #[cfg(feature = "unstable-pre-spec")] + #[serde(default)] + #[ruma_event(skip_redaction)] + pub allow: Vec, } impl JoinRulesEventContent { /// Creates a new `JoinRulesEventContent` with the given rule. pub fn new(join_rule: JoinRule) -> Self { - Self { join_rule } + Self { + join_rule, + #[cfg(feature = "unstable-pre-spec")] + allow: Vec::new(), + } + } + + /// Creates a new `JoinRulesEventContent` with the restricted rule and the given set of allow + /// rules. + #[cfg(feature = "unstable-pre-spec")] + pub fn restricted(allow: Vec) -> Self { + Self { join_rule: JoinRule::Restricted, allow } } } @@ -44,6 +70,11 @@ pub enum JoinRule { /// Reserved but not yet implemented by the Matrix specification. Private, + /// Users can join the room if they are invited, or if they meet any of the conditions + /// described in a set of [`AllowRule`]s. + #[cfg(feature = "unstable-pre-spec")] + Restricted, + /// Anyone can join the room without any prior action. Public, @@ -57,3 +88,68 @@ impl JoinRule { self.as_ref() } } + +/// An allow rule which defines a condition that allows joining a room. +#[cfg(feature = "unstable-pre-spec")] +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[serde(tag = "type")] +pub enum AllowRule { + /// Joining is allowed if a user is already a member of the romm with the id `room_id`. + #[serde(rename = "m.room_membership")] + RoomMembership(RoomMembership), + + #[doc(hidden)] + _Custom(CustomAllowRule), +} + +/// Allow rule which grants permission to join based on the membership of another room. +#[cfg(feature = "unstable-pre-spec")] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct RoomMembership { + /// The id of the room which being a member of grants permission to join another room. + pub room_id: RoomId, +} + +#[cfg(feature = "unstable-pre-spec")] +#[doc(hidden)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct CustomAllowRule { + #[serde(rename = "type")] + rule_type: String, + #[serde(flatten)] + extra: BTreeMap, +} + +#[cfg(feature = "unstable-pre-spec")] +impl<'de> Deserialize<'de> for AllowRule { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + fn from_raw_json_value(raw: &RawJsonValue) -> Result { + serde_json::from_str(raw.get()).map_err(E::custom) + } + + let json: Box = Box::deserialize(deserializer)?; + + // Extracts the `type` value. + #[derive(Deserialize)] + struct ExtractType { + rule_type: Option, + } + + // Get the value of `type` if present. + let rule_type = serde_json::from_str::(json.get()) + .map_err(serde::de::Error::custom)? + .rule_type; + + match rule_type.as_deref() { + Some("m.room_membership") => from_raw_json_value(&json).map(Self::RoomMembership), + Some(_) => from_raw_json_value(&json).map(Self::_Custom), + None => Err(D::Error::missing_field("type")), + } + } +} From d07cacb61d1b5ea18af95422fc939d13a1e4a5a7 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Sep 2021 21:25:27 +0200 Subject: [PATCH 2/2] Release ruma-events 0.24.6 --- crates/ruma-api/Cargo.toml | 2 +- crates/ruma-appservice-api/Cargo.toml | 2 +- crates/ruma-client-api/Cargo.toml | 2 +- crates/ruma-events-macros/Cargo.toml | 2 +- crates/ruma-events/CHANGELOG.md | 2 ++ crates/ruma-events/Cargo.toml | 4 ++-- crates/ruma-federation-api/Cargo.toml | 2 +- crates/ruma-push-gateway-api/Cargo.toml | 2 +- crates/ruma-state-res/Cargo.toml | 2 +- crates/ruma/Cargo.toml | 2 +- 10 files changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/ruma-api/Cargo.toml b/crates/ruma-api/Cargo.toml index ee7808b9..de713a33 100644 --- a/crates/ruma-api/Cargo.toml +++ b/crates/ruma-api/Cargo.toml @@ -37,5 +37,5 @@ serde_json = "1.0.61" thiserror = "1.0.23" [dev-dependencies] -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } trybuild = "1.0.38" diff --git a/crates/ruma-appservice-api/Cargo.toml b/crates/ruma-appservice-api/Cargo.toml index e3a9dec8..bf66988f 100644 --- a/crates/ruma-appservice-api/Cargo.toml +++ b/crates/ruma-appservice-api/Cargo.toml @@ -24,7 +24,7 @@ server = [] ruma-api = { version = "0.18.3", path = "../ruma-api" } ruma-client-api = { version = "0.12.2", path = "../ruma-client-api", features = ["client"], optional = true } ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma-client-api/Cargo.toml b/crates/ruma-client-api/Cargo.toml index 668e5666..8819ef90 100644 --- a/crates/ruma-client-api/Cargo.toml +++ b/crates/ruma-client-api/Cargo.toml @@ -36,7 +36,7 @@ maplit = "1.0.2" percent-encoding = "2.1.0" ruma-api = { version = "0.18.3", path = "../ruma-api" } ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma-events-macros/Cargo.toml b/crates/ruma-events-macros/Cargo.toml index ba076930..ecf85dd1 100644 --- a/crates/ruma-events-macros/Cargo.toml +++ b/crates/ruma-events-macros/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT" name = "ruma-events-macros" readme = "README.md" repository = "https://github.com/ruma/ruma" -version = "0.24.5" +version = "0.24.6" [lib] proc-macro = true diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 064822c6..f4b5a426 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -1,5 +1,7 @@ # [unreleased] +# 0.24.6 + Improvements: * Add (unstable) support for [MSC3083](https://github.com/matrix-org/matrix-doc/blob/main/proposals/3083-restricted-rooms.md) diff --git a/crates/ruma-events/Cargo.toml b/crates/ruma-events/Cargo.toml index 6b30dc8c..9b8e65ca 100644 --- a/crates/ruma-events/Cargo.toml +++ b/crates/ruma-events/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT" name = "ruma-events" readme = "README.md" repository = "https://github.com/ruma/ruma" -version = "0.24.5" +version = "0.24.6" edition = "2018" [package.metadata.docs.rs] @@ -28,7 +28,7 @@ indoc = "1.0" js_int = { version = "0.2.0", features = ["serde"] } pulldown-cmark = { version = "0.8", default-features = false, optional = true } ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events-macros = { version = "=0.24.5", path = "../ruma-events-macros" } +ruma-events-macros = { version = "=0.24.6", path = "../ruma-events-macros" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers", features = ["serde"] } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma-federation-api/Cargo.toml b/crates/ruma-federation-api/Cargo.toml index e2fbc4ae..2df4246d 100644 --- a/crates/ruma-federation-api/Cargo.toml +++ b/crates/ruma-federation-api/Cargo.toml @@ -28,7 +28,7 @@ server = [] js_int = { version = "0.2.0", features = ["serde"] } ruma-api = { version = "0.18.3", path = "../ruma-api" } ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma-push-gateway-api/Cargo.toml b/crates/ruma-push-gateway-api/Cargo.toml index 33a5c2ef..43fa0e8a 100644 --- a/crates/ruma-push-gateway-api/Cargo.toml +++ b/crates/ruma-push-gateway-api/Cargo.toml @@ -22,7 +22,7 @@ server = [] js_int = { version = "0.2.0", features = ["serde"] } ruma-api = { version = "0.18.3", path = "../ruma-api" } ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma-state-res/Cargo.toml b/crates/ruma-state-res/Cargo.toml index a356f326..2b74ba5c 100644 --- a/crates/ruma-state-res/Cargo.toml +++ b/crates/ruma-state-res/Cargo.toml @@ -23,7 +23,7 @@ itertools = "0.10.0" js_int = "0.2.0" maplit = "1.0.2" ruma-common = { version = "0.6.0", path = "../ruma-common" } -ruma-events = { version = "0.24.5", path = "../ruma-events" } +ruma-events = { version = "0.24.6", path = "../ruma-events" } ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/crates/ruma/Cargo.toml b/crates/ruma/Cargo.toml index 4ed419e6..99d0567f 100644 --- a/crates/ruma/Cargo.toml +++ b/crates/ruma/Cargo.toml @@ -124,7 +124,7 @@ ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers", features ruma-serde = { version = "0.5.0", path = "../ruma-serde" } ruma-client = { version = "0.7.0", path = "../ruma-client", optional = true } -ruma-events = { version = "0.24.5", path = "../ruma-events", optional = true } +ruma-events = { version = "0.24.6", path = "../ruma-events", optional = true } ruma-signatures = { version = "0.9.0", path = "../ruma-signatures", optional = true } ruma-state-res = { version = "0.3.0", path = "../ruma-state-res", optional = true }