mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor: Consolidate hierarchy and summary logic in a new service
This commit is contained in:
+10
-57
@@ -1,13 +1,7 @@
|
||||
use axum::extract::State;
|
||||
use conduwuit::{
|
||||
Err, Result, info,
|
||||
utils::stream::{BroadbandExt, IterStream},
|
||||
};
|
||||
use conduwuit_service::rooms::spaces::{
|
||||
Identifier, SummaryAccessibility, get_parent_children_via,
|
||||
};
|
||||
use futures::{FutureExt, StreamExt};
|
||||
use conduwuit::{Err, Result, info};
|
||||
use ruma::api::federation::space::get_hierarchy;
|
||||
use service::rooms::summary::Accessibility;
|
||||
|
||||
use crate::Ruma;
|
||||
|
||||
@@ -19,10 +13,6 @@ pub(crate) async fn get_hierarchy_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<get_hierarchy::v1::Request>,
|
||||
) -> Result<get_hierarchy::v1::Response> {
|
||||
if !services.rooms.metadata.exists(&body.room_id).await {
|
||||
return Err!(Request(NotFound("Room does not exist.")));
|
||||
}
|
||||
|
||||
if !services
|
||||
.rooms
|
||||
.state_cache
|
||||
@@ -36,52 +26,15 @@ pub(crate) async fn get_hierarchy_route(
|
||||
return Err!(Request(NotFound("This server is not participating in that room.")));
|
||||
}
|
||||
|
||||
let room_id = &body.room_id;
|
||||
let suggested_only = body.suggested_only;
|
||||
let ref identifier = Identifier::ServerName(body.origin());
|
||||
match services
|
||||
let response = services
|
||||
.rooms
|
||||
.spaces
|
||||
.get_summary_and_children_local(room_id, identifier)
|
||||
.await?
|
||||
{
|
||||
| None => Err!(Request(NotFound("The requested room was not found"))),
|
||||
.summary
|
||||
.get_local_room_summary_for_server(body.origin(), &body.room_id, body.suggested_only)
|
||||
.await;
|
||||
|
||||
| Some(SummaryAccessibility::Inaccessible) => {
|
||||
Err!(Request(NotFound("The requested room is inaccessible")))
|
||||
},
|
||||
|
||||
| Some(SummaryAccessibility::Accessible(room)) => {
|
||||
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))),
|
||||
|
||||
| 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;
|
||||
|
||||
Ok(get_hierarchy::v1::Response { room, children, inaccessible_children })
|
||||
},
|
||||
if let Accessibility::Accessible(response) = response {
|
||||
Ok(response)
|
||||
} else {
|
||||
Err!(Request(NotFound("This room is not accessible.")))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user