From 4dbda813c9ca22d29722c1feaffbca2744935ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 13 Sep 2023 19:47:41 +0200 Subject: [PATCH] events: Make via required in space child and parent events According to a spec clarification --- crates/ruma-events/CHANGELOG.md | 1 + crates/ruma-events/src/space/child.rs | 21 +++++++++------------ crates/ruma-events/src/space/parent.rs | 15 +++++++-------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 8cb91c58..bba5cebf 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -34,6 +34,7 @@ Breaking changes: - In Markdown, soft line breaks are transformed into hard line breaks when compiled into HTML. - Move the HTML functions in `events::room::message::sanitize` to the ruma-html crate - The `unstable-sanitize` cargo feature was renamed to `html` +- Make `via` required in `Space(Child|Parent)EventContent` according to a spec clarification Improvements: diff --git a/crates/ruma-events/src/space/child.rs b/crates/ruma-events/src/space/child.rs index 878fe2bb..2e8e0f0a 100644 --- a/crates/ruma-events/src/space/child.rs +++ b/crates/ruma-events/src/space/child.rs @@ -13,13 +13,12 @@ use serde::{Deserialize, Serialize}; /// /// The `state_key` is the ID of a child room or space, and the content must contain a `via` key /// which gives a list of candidate servers that can be used to join the room. -#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)] +#[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.space.child", kind = State, state_key_type = OwnedRoomId)] pub struct SpaceChildEventContent { /// List of candidate servers that can be used to join the room. - #[serde(skip_serializing_if = "Option::is_none")] - pub via: Option>, + pub via: Vec, /// Provide a default ordering of siblings in the room list. /// @@ -44,9 +43,9 @@ pub struct SpaceChildEventContent { } impl SpaceChildEventContent { - /// Creates a new `ChildEventContent`. - pub fn new() -> Self { - Self::default() + /// Creates a new `SpaceChildEventContent` with the given routing servers. + pub fn new(via: Vec) -> Self { + Self { via, order: None, suggested: false } } } @@ -79,7 +78,7 @@ mod tests { #[test] fn space_child_serialization() { let content = SpaceChildEventContent { - via: Some(vec![server_name!("example.com").to_owned()]), + via: vec![server_name!("example.com").to_owned()], order: Some("uwu".to_owned()), suggested: false, }; @@ -94,9 +93,9 @@ mod tests { #[test] fn space_child_empty_serialization() { - let content = SpaceChildEventContent { via: None, order: None, suggested: false }; + let content = SpaceChildEventContent { via: vec![], order: None, suggested: false }; - let json = json!({}); + let json = json!({ "via": [] }); assert_eq!(to_json_value(&content).unwrap(), json); } @@ -119,9 +118,7 @@ mod tests { assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1_629_413_349))); assert_eq!(ev.sender, "@alice:example.org"); assert_eq!(ev.state_key, "!a:example.org"); - let via = ev.content.via.unwrap(); - assert_eq!(via.len(), 1); - assert_eq!(via[0], "example.org"); + assert_eq!(ev.content.via, ["example.org"]); assert_eq!(ev.content.order, None); assert!(!ev.content.suggested); } diff --git a/crates/ruma-events/src/space/parent.rs b/crates/ruma-events/src/space/parent.rs index af040f30..429bae28 100644 --- a/crates/ruma-events/src/space/parent.rs +++ b/crates/ruma-events/src/space/parent.rs @@ -18,8 +18,7 @@ use serde::{Deserialize, Serialize}; #[ruma_event(type = "m.space.parent", kind = State, state_key_type = OwnedRoomId)] pub struct SpaceParentEventContent { /// List of candidate servers that can be used to join the room. - #[serde(skip_serializing_if = "Option::is_none")] - pub via: Option>, + pub via: Vec, /// Determines whether this is the main parent for the space. /// @@ -33,9 +32,9 @@ pub struct SpaceParentEventContent { } impl SpaceParentEventContent { - /// Creates a new `ParentEventContent` with the given canonical flag. - pub fn new(canonical: bool) -> Self { - Self { via: None, canonical } + /// Creates a new `SpaceParentEventContent` with the given routing servers and canonical flag. + pub fn new(via: Vec, canonical: bool) -> Self { + Self { via, canonical } } } @@ -49,7 +48,7 @@ mod tests { #[test] fn space_parent_serialization() { let content = SpaceParentEventContent { - via: Some(vec![server_name!("example.com").to_owned()]), + via: vec![server_name!("example.com").to_owned()], canonical: true, }; @@ -63,9 +62,9 @@ mod tests { #[test] fn space_parent_empty_serialization() { - let content = SpaceParentEventContent { via: None, canonical: false }; + let content = SpaceParentEventContent { via: vec![], canonical: false }; - let json = json!({}); + let json = json!({ "via": [] }); assert_eq!(to_json_value(&content).unwrap(), json); }