Skip to main content

tuwunel_api/server/
state_ids.rs

1use std::{borrow::Borrow, iter::once};
2
3use axum::extract::State;
4use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, future::try_join};
5use ruma::{OwnedEventId, api::federation::event::get_room_state_ids};
6use tuwunel_core::{Result, at, err};
7
8use super::AccessCheck;
9use crate::Ruma;
10
11/// # `GET /_matrix/federation/v1/state_ids/{roomId}`
12///
13/// Retrieves a snapshot of a room's state at a given event, in the form of
14/// event IDs.
15pub(crate) async fn get_room_state_ids_route(
16	State(services): State<crate::State>,
17	body: Ruma<get_room_state_ids::v1::Request>,
18) -> Result<get_room_state_ids::v1::Response> {
19	AccessCheck {
20		services: &services,
21		origin: body.origin(),
22		room_id: &body.room_id,
23		event_id: None,
24	}
25	.check()
26	.await?;
27
28	let shortstatehash = services
29		.state
30		.pdu_shortstatehash(&body.event_id)
31		.map_err(|_| err!(Request(NotFound("Pdu state not found."))));
32
33	let room_version = services.state.get_room_version(&body.room_id);
34
35	let (shortstatehash, room_version) = try_join(shortstatehash, room_version).await?;
36
37	let auth_chain_ids = services
38		.auth_chain
39		.event_ids_iter(&body.room_id, &room_version, once(body.event_id.borrow()))
40		.try_collect();
41
42	let pdu_ids = services
43		.state_accessor
44		.state_full_ids(shortstatehash)
45		.map(at!(1))
46		.collect::<Vec<OwnedEventId>>()
47		.map(Ok);
48
49	let (auth_chain_ids, pdu_ids) = try_join(auth_chain_ids, pdu_ids).await?;
50
51	Ok(get_room_state_ids::v1::Response { auth_chain_ids, pdu_ids })
52}