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

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

84 lines
1.9 KiB
Rust
Raw Normal View History

use std::{borrow::Borrow, iter::once};
2024-06-05 04:32:58 +00:00
2024-07-16 08:05:25 +00:00
use axum::extract::State;
use conduwuit::{Err, Result, at, err, info, utils::IterStream};
2024-08-08 17:18:30 +00:00
use futures::{FutureExt, StreamExt, TryStreamExt};
use ruma::{OwnedEventId, api::federation::event::get_room_state};
2024-06-05 04:32:58 +00:00
use super::AccessCheck;
2024-07-03 21:05:24 +00:00
use crate::Ruma;
2024-06-05 04:32:58 +00:00
/// # `GET /_matrix/federation/v1/state/{roomId}`
///
/// Retrieves a snapshot of a room's state at a given event.
2024-06-05 04:32:58 +00:00
pub(crate) async fn get_room_state_route(
State(services): State<crate::State>,
body: Ruma<get_room_state::v1::Request>,
2024-06-05 04:32:58 +00:00
) -> Result<get_room_state::v1::Response> {
AccessCheck {
services: &services,
origin: body.origin(),
room_id: &body.room_id,
event_id: None,
2024-06-05 04:32:58 +00:00
}
.check()
.await?;
2024-06-05 04:32:58 +00:00
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.")));
}
2024-07-16 08:05:25 +00:00
let shortstatehash = services
2024-06-05 04:32:58 +00:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.pdu_shortstatehash(&body.event_id)
.await
.map_err(|_| err!(Request(NotFound("PDU state not found."))))?;
2024-06-05 04:32:58 +00:00
2024-11-30 08:09:51 +00:00
let state_ids: Vec<OwnedEventId> = services
2024-06-05 04:32:58 +00:00
.rooms
.state_accessor
.state_full_ids(shortstatehash)
2025-01-29 01:04:02 +00:00
.map(at!(1))
.collect()
.await;
2024-11-30 08:09:51 +00:00
let pdus = state_ids
.iter()
2024-08-08 17:18:30 +00:00
.try_stream()
.and_then(|id| services.rooms.timeline.get_pdu_json(id))
.and_then(|pdu| {
2024-07-18 06:37:47 +00:00
services
.sending
2024-08-08 17:18:30 +00:00
.convert_to_outgoing_federation_event(pdu)
.map(Ok)
2024-07-18 06:37:47 +00:00
})
2024-08-08 17:18:30 +00:00
.try_collect()
.await?;
2024-06-05 04:32:58 +00:00
2024-08-08 17:18:30 +00:00
let auth_chain = services
2024-06-05 04:32:58 +00:00
.rooms
.auth_chain
.event_ids_iter(&body.room_id, once(body.event_id.borrow()))
2024-08-08 17:18:30 +00:00
.and_then(|id| async move { services.rooms.timeline.get_pdu_json(&id).await })
.and_then(|pdu| {
services
.sending
.convert_to_outgoing_federation_event(pdu)
.map(Ok)
})
.try_collect()
2024-06-05 04:32:58 +00:00
.await?;
Ok(get_room_state::v1::Response { auth_chain, pdus })
2024-06-05 04:32:58 +00:00
}