Files
continuwuity/src/api/server/hierarchy.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.4 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
2025-02-07 07:09:45 +00:00
use conduwuit::{
Err, Result, info,
utils::stream::{BroadbandExt, IterStream},
2025-02-06 20:08:00 +00:00
};
use conduwuit_service::rooms::spaces::{
Identifier, SummaryAccessibility, get_parent_children_via,
};
2025-02-07 07:09:45 +00:00
use futures::{FutureExt, StreamExt};
use ruma::api::federation::space::get_hierarchy;
2024-06-05 04:32:58 +00:00
2025-02-06 20:08:00 +00:00
use crate::Ruma;
2024-06-05 04:32:58 +00:00
/// # `GET /_matrix/federation/v1/hierarchy/{roomId}`
///
/// Gets the space tree in a depth-first manner to locate child rooms of a given
/// space.
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_hierarchy_route(
State(services): State<crate::State>,
body: Ruma<get_hierarchy::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_hierarchy::v1::Response> {
2025-02-06 20:08:00 +00:00
if !services.rooms.metadata.exists(&body.room_id).await {
return Err!(Request(NotFound("Room does not exist.")));
}
if !services
.rooms
.state_cache
.server_in_room(services.globals.server_name(), &body.room_id)
.await
{
info!(
origin = body.origin().as_str(),
"Refusing to serve state for room we aren't participating in"
);
return Err!(Request(NotFound("This server is not participating in that room.")));
}
2025-02-07 07:09:45 +00:00
let room_id = &body.room_id;
let suggested_only = body.suggested_only;
let ref identifier = Identifier::ServerName(body.origin());
2025-02-06 20:08:00 +00:00
match services
.rooms
.spaces
2025-02-07 07:09:45 +00:00
.get_summary_and_children_local(room_id, identifier)
2025-02-06 20:08:00 +00:00
.await?
{
2025-02-07 07:09:45 +00:00
| None => Err!(Request(NotFound("The requested room was not found"))),
2025-03-02 23:15:05 -05:00
| Some(SummaryAccessibility::Inaccessible) => {
Err!(Request(NotFound("The requested room is inaccessible")))
},
2025-02-07 07:09:45 +00:00
2025-02-06 20:08:00 +00:00
| Some(SummaryAccessibility::Accessible(room)) => {
2025-02-07 07:09:45 +00:00
let (children, inaccessible_children) =
get_parent_children_via(&room, suggested_only)
.stream()
.broad_filter_map(|(child, _via)| async move {
match services
.rooms
.spaces
.get_summary_and_children_local(&child, identifier)
.await
.ok()?
{
| None => None,
| Some(SummaryAccessibility::Inaccessible) =>
Some((None, Some(child))),
2025-02-06 20:08:00 +00:00
2025-02-07 07:09:45 +00:00
| Some(SummaryAccessibility::Accessible(summary)) =>
Some((Some(summary), None)),
}
})
.unzip()
.map(|(children, inaccessible_children): (Vec<_>, Vec<_>)| {
(
children.into_iter().flatten().map(Into::into).collect(),
inaccessible_children.into_iter().flatten().collect(),
)
})
.await;
2025-02-06 20:08:00 +00:00
2025-02-07 07:09:45 +00:00
Ok(get_hierarchy::v1::Response { room, children, inaccessible_children })
2025-02-06 20:08:00 +00:00
},
2024-06-05 04:32:58 +00:00
}
}