pub mod v1 {
use js_int::UInt;
use ruma_common::{
OwnedRoomId,
api::{auth_scheme::AccessToken, request, response},
metadata,
};
use crate::space::SpaceHierarchyRoomsChunk;
metadata! {
method: GET,
rate_limited: true,
authentication: AccessToken,
history: {
unstable => "/_matrix/client/unstable/org.matrix.msc2946/rooms/{room_id}/hierarchy",
1.2 => "/_matrix/client/v1/rooms/{room_id}/hierarchy",
}
}
#[request(error = crate::Error)]
pub struct Request {
#[ruma_api(path)]
pub room_id: OwnedRoomId,
#[ruma_api(query)]
pub from: Option<String>,
#[ruma_api(query)]
pub limit: Option<UInt>,
#[ruma_api(query)]
pub max_depth: Option<UInt>,
#[ruma_api(query)]
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub suggested_only: bool,
}
#[response(error = crate::Error)]
#[derive(Default)]
pub struct Response {
#[serde(skip_serializing_if = "Option::is_none")]
pub next_batch: Option<String>,
pub rooms: Vec<SpaceHierarchyRoomsChunk>,
}
impl Request {
pub fn new(room_id: OwnedRoomId) -> Self {
Self { room_id, from: None, limit: None, max_depth: None, suggested_only: false }
}
}
impl Response {
pub fn new() -> Self {
Default::default()
}
}
}
#[cfg(all(test, feature = "client"))]
mod tests {
use ruma_common::api::IncomingResponse;
use serde_json::{json, to_vec as to_json_vec};
use super::v1::Response;
#[test]
fn deserialize_response() {
let body = json!({
"rooms": [
{
"room_id": "!room:localhost",
"num_joined_members": 5,
"world_readable": false,
"guest_can_join": false,
"join_rule": "restricted",
"allowed_room_ids": ["!otherroom:localhost"],
"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();
let room = &response.rooms[0];
assert_eq!(room.summary.room_id, "!room:localhost");
let space_child = room.children_state[0].deserialize().unwrap();
assert_eq!(space_child.state_key, "!a:example.org");
}
}