2024-08-08 17:18:30 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{
|
2024-08-08 17:18:30 +00:00
|
|
|
utils::{IterStream, ReadyExt},
|
2024-10-30 07:01:50 +00:00
|
|
|
PduCount, Result,
|
2024-06-05 04:32:58 +00:00
|
|
|
};
|
2024-08-08 17:18:30 +00:00
|
|
|
use futures::{FutureExt, StreamExt};
|
2024-11-07 04:49:01 +00:00
|
|
|
use ruma::{api::federation::backfill::get_backfill, uint, MilliSecondsSinceUnixEpoch};
|
2024-06-05 04:32:58 +00:00
|
|
|
|
2024-10-30 07:01:50 +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/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(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
ref body: Ruma<get_backfill::v1::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<get_backfill::v1::Response> {
|
2024-10-30 07:01:50 +00:00
|
|
|
AccessCheck {
|
|
|
|
services: &services,
|
|
|
|
origin: body.origin(),
|
|
|
|
room_id: &body.room_id,
|
|
|
|
event_id: None,
|
2024-06-05 04:32:58 +00:00
|
|
|
}
|
2024-10-30 07:01:50 +00:00
|
|
|
.check()
|
|
|
|
.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-11-08 05:49:28 +00:00
|
|
|
let from = body
|
|
|
|
.v
|
|
|
|
.iter()
|
|
|
|
.stream()
|
|
|
|
.filter_map(|event_id| {
|
2024-08-08 17:18:30 +00:00
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-11-08 05:49:28 +00:00
|
|
|
.get_pdu_count(event_id)
|
|
|
|
.map(Result::ok)
|
2024-06-05 04:32:58 +00:00
|
|
|
})
|
2024-11-08 05:49:28 +00:00
|
|
|
.ready_fold(PduCount::min(), cmp::max)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await;
|
2024-06-05 04:32:58 +00:00
|
|
|
|
|
|
|
Ok(get_backfill::v1::Response {
|
|
|
|
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
|
2024-11-08 05:49:28 +00:00
|
|
|
|
|
|
|
origin: services.globals.server_name().to_owned(),
|
|
|
|
|
|
|
|
pdus: services
|
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-11-11 05:00:29 +00:00
|
|
|
.pdus_rev(None, &body.room_id, Some(from.saturating_add(1)))
|
2024-11-08 05:49:28 +00:00
|
|
|
.await?
|
|
|
|
.take(limit)
|
|
|
|
.filter_map(|(_, pdu)| async move {
|
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.state_accessor
|
|
|
|
.server_can_see_event(body.origin(), &pdu.room_id, &pdu.event_id)
|
|
|
|
.await
|
|
|
|
.then_some(pdu)
|
|
|
|
})
|
|
|
|
.filter_map(|pdu| async move {
|
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.get_pdu_json(&pdu.event_id)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.then(|pdu| services.sending.convert_to_outgoing_federation_event(pdu))
|
|
|
|
.collect()
|
|
|
|
.await,
|
2024-06-05 04:32:58 +00:00
|
|
|
})
|
|
|
|
}
|