events: Make via required in space child and parent events

According to a spec clarification
This commit is contained in:
Kévin Commaille 2023-09-13 19:47:41 +02:00 committed by Kévin Commaille
parent 31ae99cf52
commit 4dbda813c9
3 changed files with 17 additions and 20 deletions

View File

@ -34,6 +34,7 @@ Breaking changes:
- In Markdown, soft line breaks are transformed into hard line breaks when compiled into HTML. - 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 - Move the HTML functions in `events::room::message::sanitize` to the ruma-html crate
- The `unstable-sanitize` cargo feature was renamed to `html` - The `unstable-sanitize` cargo feature was renamed to `html`
- Make `via` required in `Space(Child|Parent)EventContent` according to a spec clarification
Improvements: Improvements:

View File

@ -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 /// 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. /// 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)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.space.child", kind = State, state_key_type = OwnedRoomId)] #[ruma_event(type = "m.space.child", kind = State, state_key_type = OwnedRoomId)]
pub struct SpaceChildEventContent { pub struct SpaceChildEventContent {
/// List of candidate servers that can be used to join the room. /// List of candidate servers that can be used to join the room.
#[serde(skip_serializing_if = "Option::is_none")] pub via: Vec<OwnedServerName>,
pub via: Option<Vec<OwnedServerName>>,
/// Provide a default ordering of siblings in the room list. /// Provide a default ordering of siblings in the room list.
/// ///
@ -44,9 +43,9 @@ pub struct SpaceChildEventContent {
} }
impl SpaceChildEventContent { impl SpaceChildEventContent {
/// Creates a new `ChildEventContent`. /// Creates a new `SpaceChildEventContent` with the given routing servers.
pub fn new() -> Self { pub fn new(via: Vec<OwnedServerName>) -> Self {
Self::default() Self { via, order: None, suggested: false }
} }
} }
@ -79,7 +78,7 @@ mod tests {
#[test] #[test]
fn space_child_serialization() { fn space_child_serialization() {
let content = SpaceChildEventContent { 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()), order: Some("uwu".to_owned()),
suggested: false, suggested: false,
}; };
@ -94,9 +93,9 @@ mod tests {
#[test] #[test]
fn space_child_empty_serialization() { 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); 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.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1_629_413_349)));
assert_eq!(ev.sender, "@alice:example.org"); assert_eq!(ev.sender, "@alice:example.org");
assert_eq!(ev.state_key, "!a:example.org"); assert_eq!(ev.state_key, "!a:example.org");
let via = ev.content.via.unwrap(); assert_eq!(ev.content.via, ["example.org"]);
assert_eq!(via.len(), 1);
assert_eq!(via[0], "example.org");
assert_eq!(ev.content.order, None); assert_eq!(ev.content.order, None);
assert!(!ev.content.suggested); assert!(!ev.content.suggested);
} }

View File

@ -18,8 +18,7 @@ use serde::{Deserialize, Serialize};
#[ruma_event(type = "m.space.parent", kind = State, state_key_type = OwnedRoomId)] #[ruma_event(type = "m.space.parent", kind = State, state_key_type = OwnedRoomId)]
pub struct SpaceParentEventContent { pub struct SpaceParentEventContent {
/// List of candidate servers that can be used to join the room. /// List of candidate servers that can be used to join the room.
#[serde(skip_serializing_if = "Option::is_none")] pub via: Vec<OwnedServerName>,
pub via: Option<Vec<OwnedServerName>>,
/// Determines whether this is the main parent for the space. /// Determines whether this is the main parent for the space.
/// ///
@ -33,9 +32,9 @@ pub struct SpaceParentEventContent {
} }
impl SpaceParentEventContent { impl SpaceParentEventContent {
/// Creates a new `ParentEventContent` with the given canonical flag. /// Creates a new `SpaceParentEventContent` with the given routing servers and canonical flag.
pub fn new(canonical: bool) -> Self { pub fn new(via: Vec<OwnedServerName>, canonical: bool) -> Self {
Self { via: None, canonical } Self { via, canonical }
} }
} }
@ -49,7 +48,7 @@ mod tests {
#[test] #[test]
fn space_parent_serialization() { fn space_parent_serialization() {
let content = SpaceParentEventContent { let content = SpaceParentEventContent {
via: Some(vec![server_name!("example.com").to_owned()]), via: vec![server_name!("example.com").to_owned()],
canonical: true, canonical: true,
}; };
@ -63,9 +62,9 @@ mod tests {
#[test] #[test]
fn space_parent_empty_serialization() { 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); assert_eq!(to_json_value(&content).unwrap(), json);
} }