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

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

84 lines
1.7 KiB
Rust
Raw Normal View History

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::{
utils::{IterStream, ReadyExt},
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
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(
State(services): State<crate::State>, body: Ruma<get_backfill::v1::Request>,
) -> Result<get_backfill::v1::Response> {
AccessCheck {
services: &services,
origin: body.origin(),
room_id: &body.room_id,
event_id: None,
2024-06-05 04:32:58 +00:00
}
.check()
.await?;
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");
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-11-06 22:21:51 +00:00
.pdus_rev(user_id!("@doesntmatter:conduit.rs"), &body.room_id, until)
2024-08-08 17:18:30 +00:00
.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
{
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
})
}