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

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

63 lines
1.6 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
use conduwuit::{Err, Result, err, info};
use ruma::{MilliSecondsSinceUnixEpoch, RoomId, api::federation::event::get_event};
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/event/{eventId}`
///
/// Retrieves a single event from the server.
///
/// - Only works if a user of this server is currently invited or joined the
/// room
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_event_route(
State(services): State<crate::State>,
body: Ruma<get_event::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_event::v1::Response> {
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(|_| err!(Request(NotFound("Event not found."))))?;
2024-06-05 04:32:58 +00:00
let room_id: &RoomId = event
2024-06-05 04:32:58 +00:00
.get("room_id")
.and_then(|val| val.as_str())
.ok_or_else(|| err!(Database("Invalid event in database.")))?
.try_into()
.map_err(|_| err!(Database("Invalid room_id in event in database.")))?;
2024-06-05 04:32:58 +00:00
AccessCheck {
services: &services,
origin: body.origin(),
room_id,
event_id: Some(&body.event_id),
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(), 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-06-05 04:32:58 +00:00
Ok(get_event::v1::Response {
2024-07-16 08:05:25 +00:00
origin: services.globals.server_name().to_owned(),
2024-06-05 04:32:58 +00:00
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
2024-08-08 17:18:30 +00:00
pdu: services
.sending
.convert_to_outgoing_federation_event(event)
.await,
2024-06-05 04:32:58 +00:00
})
}