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.

73 lines
1.6 KiB
Rust
Raw Normal View History

2024-09-25 03:52:28 +00:00
use std::borrow::Borrow;
2024-06-05 04:32:58 +00:00
2024-07-16 08:05:25 +00:00
use axum::extract::State;
use conduit::{err, result::LogErr, utils::IterStream, Result};
2024-08-08 17:18:30 +00:00
use futures::{FutureExt, StreamExt, TryStreamExt};
use ruma::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(
2024-07-16 08:05:25 +00:00
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
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-07-16 08:05:25 +00:00
let pdus = services
2024-06-05 04:32:58 +00:00
.rooms
.state_accessor
.state_full_ids(shortstatehash)
2024-08-08 17:18:30 +00:00
.await
.log_err()
.map_err(|_| err!(Request(NotFound("PDU state IDs not found."))))?
.values()
.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
2024-09-25 03:52:28 +00:00
.event_ids_iter(&body.room_id, &[body.event_id.borrow()])
2024-08-08 17:18:30 +00:00
.await?
.map(Ok)
.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 {
2024-08-08 17:18:30 +00:00
auth_chain,
2024-06-05 04:32:58 +00:00
pdus,
})
}