Files
continuwuity/src/api/client/room/initial_sync.rs
T

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

98 lines
2.1 KiB
Rust
Raw Normal View History

2024-11-12 03:46:31 +00:00
use axum::extract::State;
use conduwuit::{
2025-06-04 00:11:09 +01:00
Err, Event, Result, at, debug_warn,
2025-04-29 06:39:30 +00:00
utils::{BoolExt, stream::TryTools},
};
use futures::{FutureExt, TryStreamExt, future::try_join4};
2024-11-12 03:46:31 +00:00
use ruma::api::client::room::initial_sync::v3::{PaginationChunk, Request, Response};
use crate::Ruma;
const LIMIT_MAX: usize = 100;
pub(crate) async fn room_initial_sync_route(
State(services): State<crate::State>,
body: Ruma<Request>,
2024-11-12 03:46:31 +00:00
) -> Result<Response> {
let room_id = &body.room_id;
if !services
.rooms
.state_accessor
.user_can_see_state_events(body.sender_user(), room_id)
.await
{
return Err!(Request(Forbidden("No room preview available.")));
}
let membership = services
2024-11-12 03:46:31 +00:00
.rooms
.state_cache
.user_membership(body.sender_user(), room_id)
.map(Ok);
let visibility = services.rooms.directory.visibility(room_id).map(Ok);
2024-11-12 03:46:31 +00:00
let state = services
2024-11-12 03:46:31 +00:00
.rooms
.state_accessor
.room_state_full_pdus(room_id)
2025-04-26 08:24:47 +00:00
.map_ok(Event::into_format)
.try_collect::<Vec<_>>();
// Events are returned in body
let limit = LIMIT_MAX;
let events = services
.rooms
.timeline
.pdus_rev(room_id, None)
.try_take(limit)
.and_then(async |mut pdu| {
pdu.1.set_unsigned(body.sender_user.as_deref());
2025-06-04 00:11:09 +01:00
if let Some(sender_user) = body.sender_user.as_deref() {
if let Err(e) = services
.rooms
.pdu_metadata
.add_bundled_aggregations_to_pdu(sender_user, &mut pdu.1)
.await
{
debug_warn!("Failed to add bundled aggregations: {e}");
}
}
Ok(pdu)
})
.try_collect::<Vec<_>>();
let (membership, visibility, state, events) =
try_join4(membership, visibility, state, events)
.boxed()
.await?;
2024-11-12 03:46:31 +00:00
let messages = PaginationChunk {
start: events.last().map(at!(0)).as_ref().map(ToString::to_string),
end: events
.first()
.map(at!(0))
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
chunk: events
.into_iter()
.map(at!(1))
2025-04-26 08:24:47 +00:00
.map(Event::into_format)
2024-11-12 03:46:31 +00:00
.collect(),
};
Ok(Response {
room_id: room_id.to_owned(),
account_data: None,
state: state.into(),
messages: messages.chunk.is_empty().or_some(messages),
visibility: visibility.into(),
membership,
2024-11-12 03:46:31 +00:00
})
}