additional sync cleanup

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-16 06:58:37 +00:00 committed by strawberry
parent 828cb96ba9
commit 1fdcab0319

View file

@ -14,38 +14,37 @@ use crate::{service::Services, Error, PduEvent, Result};
async fn load_timeline(
services: &Services, sender_user: &UserId, room_id: &RoomId, roomsincecount: PduCount, limit: u64,
) -> Result<(Vec<(PduCount, PduEvent)>, bool), Error> {
let timeline_pdus;
let limited = if services
let last_timeline_count = services
.rooms
.timeline
.last_timeline_count(sender_user, room_id)
.await?;
if last_timeline_count <= roomsincecount {
return Ok((Vec::new(), false));
}
let mut non_timeline_pdus = services
.rooms
.timeline
.pdus_until(sender_user, room_id, PduCount::max())
.await?
> roomsincecount
{
let mut non_timeline_pdus = services
.rooms
.timeline
.pdus_until(sender_user, room_id, PduCount::max())
.await?
.ready_take_while(|(pducount, _)| pducount > &roomsincecount);
.ready_take_while(|(pducount, _)| pducount > &roomsincecount);
// Take the last events for the timeline
timeline_pdus = non_timeline_pdus
.by_ref()
.take(usize_from_u64_truncated(limit))
.collect::<Vec<_>>()
.await
.into_iter()
.rev()
.collect::<Vec<_>>();
// Take the last events for the timeline
let timeline_pdus: Vec<_> = non_timeline_pdus
.by_ref()
.take(usize_from_u64_truncated(limit))
.collect::<Vec<_>>()
.await
.into_iter()
.rev()
.collect();
// They /sync response doesn't always return all messages, so we say the output
// is limited unless there are events in non_timeline_pdus
let limited = non_timeline_pdus.next().await.is_some();
// They /sync response doesn't always return all messages, so we say the output
// is limited unless there are events in non_timeline_pdus
non_timeline_pdus.next().await.is_some()
} else {
timeline_pdus = Vec::new();
false
};
Ok((timeline_pdus, limited))
}