use ruma_common::{
OwnedRoomId,
api::{request, response},
metadata,
room::RoomSummary,
};
use crate::{authentication::ServerSignatures, space::SpaceHierarchyParentSummary};
metadata! {
method: GET,
rate_limited: false,
authentication: ServerSignatures,
path: "/_matrix/federation/v1/hierarchy/{room_id}",
}
#[request]
pub struct Request {
#[ruma_api(path)]
pub room_id: OwnedRoomId,
#[ruma_api(query)]
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub suggested_only: bool,
}
#[response]
pub struct Response {
pub children: Vec<RoomSummary>,
pub inaccessible_children: Vec<OwnedRoomId>,
pub room: SpaceHierarchyParentSummary,
}
impl Request {
pub fn new(room_id: OwnedRoomId) -> Self {
Self { room_id, suggested_only: false }
}
}
impl Response {
pub fn new(room_summary: SpaceHierarchyParentSummary) -> Self {
Self { children: Vec::new(), inaccessible_children: Vec::new(), room: room_summary }
}
}
#[cfg(all(test, feature = "client"))]
mod tests {
use ruma_common::{OwnedRoomId, api::IncomingResponse};
use serde_json::{json, to_vec as to_json_vec};
use super::Response;
#[test]
fn deserialize_response() {
let body = json!({
"children": [
{
"room_id": "!a:localhost",
"num_joined_members": 6,
"world_readable": true,
"guest_can_join": false,
"join_rule": "public",
},
],
"inaccessible_children": [],
"room": {
"room_id": "!room:localhost",
"num_joined_members": 5,
"world_readable": false,
"guest_can_join": false,
"join_rule": "restricted",
"allowed_room_ids": ["!otherroom:localhost"],
"type": "space",
"children_state": [
{
"content": {
"via": [
"example.org"
]
},
"origin_server_ts": 1_629_413_349,
"sender": "@alice:example.org",
"state_key": "!a:example.org",
"type": "m.space.child"
}
],
},
});
let response = http::Response::new(to_json_vec(&body).unwrap());
let response = Response::try_from_http_response(response).unwrap();
assert_eq!(response.room.summary.room_id, "!room:localhost");
let space_child = response.room.children_state[0].deserialize().unwrap();
assert_eq!(space_child.state_key, "!a:example.org");
assert_eq!(response.inaccessible_children, &[] as &[OwnedRoomId]);
assert_eq!(response.children[0].room_id, "!a:localhost");
}
}