diff --git a/src/api/client/sync/mod.rs b/src/api/client/sync/mod.rs index 0cfc7b8b..ed22010c 100644 --- a/src/api/client/sync/mod.rs +++ b/src/api/client/sync/mod.rs @@ -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::>() - .await - .into_iter() - .rev() - .collect::>(); + // Take the last events for the timeline + let timeline_pdus: Vec<_> = non_timeline_pdus + .by_ref() + .take(usize_from_u64_truncated(limit)) + .collect::>() + .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)) }