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

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

71 lines
1.8 KiB
Rust
Raw Normal View History

2024-06-05 04:32:58 +00:00
use std::sync::Arc;
2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-07-03 21:05:24 +00:00
use conduit::{Error, Result};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
2024-06-05 04:32:58 +00:00
use ruma::{
api::{client::error::ErrorKind, federation::authorization::get_event_authorization},
RoomId,
};
2024-07-03 21:05:24 +00:00
use crate::Ruma;
2024-06-05 04:32:58 +00:00
/// # `GET /_matrix/federation/v1/event_auth/{roomId}/{eventId}`
///
/// Retrieves the auth chain for a given event.
///
/// - This does not include the event itself
pub(crate) async fn get_event_authorization_route(
2024-07-16 08:05:25 +00:00
State(services): State<crate::State>, body: Ruma<get_event_authorization::v1::Request>,
2024-06-05 04:32:58 +00:00
) -> Result<get_event_authorization::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
2024-07-16 08:05:25 +00:00
services
.rooms
.event_handler
2024-08-08 17:18:30 +00:00
.acl_check(origin, &body.room_id)
.await?;
2024-07-16 08:05:25 +00:00
if !services
2024-06-05 04:32:58 +00:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.is_world_readable(&body.room_id)
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.await
2024-06-05 04:32:58 +00:00
{
return Err(Error::BadRequest(ErrorKind::forbidden(), "Server is not in room."));
}
2024-07-16 08:05:25 +00:00
let event = services
2024-06-05 04:32:58 +00:00
.rooms
.timeline
2024-08-08 17:18:30 +00:00
.get_pdu_json(&body.event_id)
.await
.map_err(|_| Error::BadRequest(ErrorKind::NotFound, "Event not found."))?;
2024-06-05 04:32:58 +00:00
let room_id_str = event
.get("room_id")
.and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database."))?;
let room_id =
<&RoomId>::try_from(room_id_str).map_err(|_| Error::bad_database("Invalid room_id in event in database."))?;
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(room_id, vec![Arc::from(&*body.event_id)])
2024-08-08 17:18:30 +00:00
.await?
.filter_map(|id| async move { services.rooms.timeline.get_pdu_json(&id).await.ok() })
.then(|pdu| services.sending.convert_to_outgoing_federation_event(pdu))
.collect()
.await;
2024-06-05 04:32:58 +00:00
Ok(get_event_authorization::v1::Response {
2024-08-08 17:18:30 +00:00
auth_chain,
2024-06-05 04:32:58 +00:00
})
}