make pdu batch tokens zeroith-indexed

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-06 22:21:51 +00:00
parent f36757027e
commit e507c31306
9 changed files with 67 additions and 52 deletions

View file

@ -2,7 +2,7 @@ use std::iter::once;
use axum::extract::State;
use conduit::{
err, error,
at, err, error,
utils::{future::TryExtExt, stream::ReadyExt, IterStream},
Err, Result,
};
@ -82,7 +82,7 @@ pub(crate) async fn get_context_route(
let events_before: Vec<_> = services
.rooms
.timeline
.pdus_until(sender_user, room_id, base_token)
.pdus_rev(sender_user, room_id, base_token.saturating_sub(1))
.await?
.ready_filter_map(|item| event_filter(item, filter))
.filter_map(|item| ignored_filter(&services, item, sender_user))
@ -94,7 +94,7 @@ pub(crate) async fn get_context_route(
let events_after: Vec<_> = services
.rooms
.timeline
.pdus_after(sender_user, room_id, base_token)
.pdus(sender_user, room_id, base_token.saturating_add(1))
.await?
.ready_filter_map(|item| event_filter(item, filter))
.filter_map(|item| ignored_filter(&services, item, sender_user))
@ -168,22 +168,28 @@ pub(crate) async fn get_context_route(
start: events_before
.last()
.map_or_else(|| base_token.to_string(), |(count, _)| count.to_string())
.into(),
.map(at!(0))
.map(|count| count.saturating_sub(1))
.as_ref()
.map(ToString::to_string),
end: events_after
.last()
.map_or_else(|| base_token.to_string(), |(count, _)| count.to_string())
.into(),
.map(at!(0))
.map(|count| count.saturating_add(1))
.as_ref()
.map(ToString::to_string),
events_before: events_before
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.collect(),
events_after: events_after
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.map(at!(1))
.map(|pdu| pdu.to_room_event())
.collect(),
state,

View file

@ -100,14 +100,14 @@ pub(crate) async fn get_message_events_route(
Direction::Forward => services
.rooms
.timeline
.pdus_after(sender_user, room_id, from)
.pdus(sender_user, room_id, from)
.await?
.boxed(),
Direction::Backward => services
.rooms
.timeline
.pdus_until(sender_user, room_id, from)
.pdus_rev(sender_user, room_id, from)
.await?
.boxed(),
};
@ -136,7 +136,12 @@ pub(crate) async fn get_message_events_route(
.collect()
.await;
let next_token = events.last().map(|(count, _)| count).copied();
let start_token = events.first().map(at!(0)).unwrap_or(from);
let next_token = events
.last()
.map(at!(0))
.map(|count| count.saturating_inc(body.dir));
if !cfg!(feature = "element_hacks") {
if let Some(next_token) = next_token {
@ -154,8 +159,8 @@ pub(crate) async fn get_message_events_route(
.collect();
Ok(get_message_events::v3::Response {
start: from.to_string(),
end: next_token.as_ref().map(PduCount::to_string),
start: start_token.to_string(),
end: next_token.as_ref().map(ToString::to_string),
chunk,
state,
})

View file

@ -150,10 +150,7 @@ async fn paginate_relations_with_filter(
Direction::Backward => events.first(),
}
.map(at!(0))
.map(|count| match dir {
Direction::Forward => count.saturating_add(1),
Direction::Backward => count.saturating_sub(1),
})
.map(|count| count.saturating_inc(dir))
.as_ref()
.map(ToString::to_string);

View file

@ -24,7 +24,7 @@ async fn load_timeline(
let mut non_timeline_pdus = services
.rooms
.timeline
.pdus_until(sender_user, room_id, PduCount::max())
.pdus_rev(sender_user, room_id, PduCount::max())
.await?
.ready_take_while(|(pducount, _)| *pducount > roomsincecount);

View file

@ -6,7 +6,7 @@ use std::{
use axum::extract::State;
use conduit::{
err, error, extract_variant, is_equal_to,
at, err, error, extract_variant, is_equal_to,
result::FlatOk,
utils::{math::ruma_from_u64, BoolExt, IterStream, ReadyExt, TryFutureExtExt},
PduCount,
@ -945,15 +945,10 @@ async fn load_joined_room(
let prev_batch = timeline_pdus
.first()
.map_or(Ok::<_, Error>(None), |(pdu_count, _)| {
Ok(Some(match pdu_count {
PduCount::Backfilled(_) => {
error!("timeline in backfill state?!");
"0".to_owned()
},
PduCount::Normal(c) => c.to_string(),
}))
})?;
.map(at!(0))
.map(|count| count.saturating_sub(1))
.as_ref()
.map(ToString::to_string);
let room_events: Vec<_> = timeline_pdus
.iter()

View file

@ -51,7 +51,7 @@ pub(crate) async fn get_backfill_route(
let pdus = services
.rooms
.timeline
.pdus_until(user_id!("@doesntmatter:conduit.rs"), &body.room_id, until)
.pdus_rev(user_id!("@doesntmatter:conduit.rs"), &body.room_id, until)
.await?
.take(limit)
.filter_map(|(_, pdu)| async move {