2024-08-08 17:18:30 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{
|
|
|
|
is_equal_to,
|
|
|
|
utils::{IterStream, ReadyExt},
|
|
|
|
Err, PduCount, Result,
|
2024-06-05 04:32:58 +00:00
|
|
|
};
|
2024-08-08 17:18:30 +00:00
|
|
|
use futures::{FutureExt, StreamExt};
|
|
|
|
use ruma::{api::federation::backfill::get_backfill, uint, user_id, MilliSecondsSinceUnixEpoch};
|
2024-06-05 04:32:58 +00:00
|
|
|
|
2024-07-03 21:05:24 +00:00
|
|
|
use crate::Ruma;
|
2024-06-05 04:32:58 +00:00
|
|
|
|
|
|
|
/// # `GET /_matrix/federation/v1/backfill/<room_id>`
|
|
|
|
///
|
|
|
|
/// Retrieves events from before the sender joined the room, if the room's
|
|
|
|
/// history visibility allows.
|
2024-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn get_backfill_route(
|
|
|
|
State(services): State<crate::State>, body: Ruma<get_backfill::v1::Request>,
|
|
|
|
) -> Result<get_backfill::v1::Response> {
|
|
|
|
services
|
2024-06-07 01:45:00 -04:00
|
|
|
.rooms
|
|
|
|
.event_handler
|
2024-10-24 12:03:56 +00:00
|
|
|
.acl_check(body.origin(), &body.room_id)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await?;
|
2024-06-07 01:45:00 -04:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
if !services
|
2024-06-05 04:32:58 +00:00
|
|
|
.rooms
|
2024-06-07 01:45:00 -04:00
|
|
|
.state_accessor
|
2024-08-08 17:18:30 +00:00
|
|
|
.is_world_readable(&body.room_id)
|
|
|
|
.await && !services
|
|
|
|
.rooms
|
|
|
|
.state_cache
|
2024-10-24 12:03:56 +00:00
|
|
|
.server_in_room(body.origin(), &body.room_id)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await
|
2024-06-05 04:32:58 +00:00
|
|
|
{
|
2024-08-08 17:18:30 +00:00
|
|
|
return Err!(Request(Forbidden("Server is not in room.")));
|
2024-06-05 04:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let until = body
|
|
|
|
.v
|
|
|
|
.iter()
|
2024-08-08 17:18:30 +00:00
|
|
|
.stream()
|
|
|
|
.filter_map(|event_id| {
|
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.get_pdu_count(event_id)
|
|
|
|
.map(Result::ok)
|
|
|
|
})
|
|
|
|
.ready_fold(PduCount::Backfilled(0), cmp::max)
|
|
|
|
.await;
|
2024-06-05 04:32:58 +00:00
|
|
|
|
|
|
|
let limit = body
|
|
|
|
.limit
|
|
|
|
.min(uint!(100))
|
|
|
|
.try_into()
|
|
|
|
.expect("UInt could not be converted to usize");
|
|
|
|
|
2024-10-24 12:03:56 +00:00
|
|
|
let origin = body.origin();
|
2024-08-08 17:18:30 +00:00
|
|
|
let pdus = services
|
2024-06-05 04:32:58 +00:00
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-08-08 17:18:30 +00:00
|
|
|
.pdus_until(user_id!("@doesntmatter:conduit.rs"), &body.room_id, until)
|
|
|
|
.await?
|
|
|
|
.take(limit)
|
|
|
|
.filter_map(|(_, pdu)| async move {
|
|
|
|
if !services
|
|
|
|
.rooms
|
|
|
|
.state_accessor
|
|
|
|
.server_can_see_event(origin, &pdu.room_id, &pdu.event_id)
|
|
|
|
.await
|
|
|
|
.is_ok_and(is_equal_to!(true))
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
2024-06-05 04:32:58 +00:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.get_pdu_json(&pdu.event_id)
|
|
|
|
.await
|
|
|
|
.ok()
|
2024-06-05 04:32:58 +00:00
|
|
|
})
|
2024-08-08 17:18:30 +00:00
|
|
|
.then(|pdu| services.sending.convert_to_outgoing_federation_event(pdu))
|
|
|
|
.collect()
|
|
|
|
.await;
|
2024-06-05 04:32:58 +00:00
|
|
|
|
|
|
|
Ok(get_backfill::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
|
|
|
pdus,
|
2024-06-05 04:32:58 +00:00
|
|
|
})
|
|
|
|
}
|