Commit 188a5acebc

188a5acebca9613b38e3c63f462e6d2611a7b9f0

parent: 1af77dee47

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-08-01 06:55 UTC
committer: <strk@kbt.io>

fix: continue Matrix history across empty pages

(cherry picked from commit d72f8cd69a0b8fdb80ca43f05cb172a678d4f0aa)

Layout: unified · split

src/room/mod.rs +70 −12
@@ -196,6 +196,26 @@ fn has_history_page(prev_batch: &Option<PrevBatch>) -> bool {
196196 prev_batch.is_some()
197197}
198198
199fn next_history_page_state(
200 current: &PrevBatch,
201 end: Option<String>,
202 _added: usize,
203) -> (Option<PrevBatch>, bool) {
204 if let PrevBatch::Forward(token) = current {
205 return (Some(PrevBatch::Backwards(Some(token.clone()))), false);
206 }
207
208 let repeated_cursor = matches!(
209 (current, end.as_deref()),
210 (PrevBatch::Backwards(Some(current)), Some(next)) if current == next
211 );
212 if end.is_none() || repeated_cursor {
213 (None, true)
214 } else {
215 (Some(PrevBatch::Backwards(end)), false)
216 }
217}
218
199219fn history_page_marker(result: &HistoryPageResult) -> String {
200220 match result {
201221 HistoryPageResult::Page { added, exhausted } => format!(
@@ -2538,12 +2558,13 @@ impl MatrixRoom {
25382558 let room = self.room();
25392559 let room_id = room.room_id().to_owned();
25402560
2541 if let Ok(r) =
2542 connection.room_messages(room, prev_batch, limit).await
2561 if let Ok(r) = connection
2562 .room_messages(room, prev_batch.clone(), limit)
2563 .await
25432564 {
25442565 let added = r.chunk.len();
2545 let exhausted = r.chunk.is_empty() || r.end.is_none();
2546
2566 let (next_prev_batch, exhausted) =
2567 next_history_page_state(&prev_batch, r.end.clone(), added);
25472568 for event in
25482569 r.chunk.iter().filter_map(|e| e.raw().deserialize().ok())
25492570 {
@@ -2564,16 +2585,14 @@ impl MatrixRoom {
25642585
25652586 let mut prev_batch = self.prev_batch.borrow_mut();
25662587
2567 if let Some(PrevBatch::Forward(t)) = prev_batch.as_ref() {
2568 *prev_batch =
2569 Some(PrevBatch::Backwards(Some(t.to_owned())));
2588 if matches!(prev_batch.as_ref(), Some(PrevBatch::Forward(_))) {
2589 *prev_batch = next_prev_batch;
25702590 self.buffer.sort_messages();
2571 } else if r.chunk.is_empty() {
2572 *prev_batch = None;
25732591 } else {
2574 *prev_batch =
2575 r.end.map(|token| PrevBatch::Backwards(Some(token)));
2576 self.buffer.sort_messages();
2592 *prev_batch = next_prev_batch;
2593 if added > 0 {
2594 self.buffer.sort_messages();
2595 }
25772596 }
25782597
25792598 HistoryPageResult::Page { added, exhausted }
@@ -3256,6 +3275,45 @@ mod tests {
32563275 ))));
32573276 }
32583277
3278 #[test]
3279 fn empty_history_page_keeps_a_new_cursor() {
3280 assert_eq!(
3281 next_history_page_state(
3282 &PrevBatch::Backwards(Some("cursor-1".to_owned())),
3283 Some("cursor-2".to_owned()),
3284 0,
3285 ),
3286 (
3287 Some(PrevBatch::Backwards(Some("cursor-2".to_owned()))),
3288 false,
3289 ),
3290 );
3291 }
3292
3293 #[test]
3294 fn empty_history_page_stops_on_a_repeated_cursor() {
3295 assert_eq!(
3296 next_history_page_state(
3297 &PrevBatch::Backwards(Some("cursor-1".to_owned())),
3298 Some("cursor-1".to_owned()),
3299 0,
3300 ),
3301 (None, true),
3302 );
3303 }
3304
3305 #[test]
3306 fn non_empty_history_page_also_stops_on_a_repeated_cursor() {
3307 assert_eq!(
3308 next_history_page_state(
3309 &PrevBatch::Backwards(Some("cursor-1".to_owned())),
3310 Some("cursor-1".to_owned()),
3311 25,
3312 ),
3313 (None, true),
3314 );
3315 }
3316
32593317 #[test]
32603318 fn interactive_history_pages_are_bounded() {
32613319 assert_eq!(INTERACTIVE_HISTORY_BATCH_SIZE, 25);