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 {
196 prev_batch.is_some() 196 prev_batch.is_some()
197} 197}
198 198
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
199fn history_page_marker(result: &HistoryPageResult) -> String { 219fn history_page_marker(result: &HistoryPageResult) -> String {
200 match result { 220 match result {
201 HistoryPageResult::Page { added, exhausted } => format!( 221 HistoryPageResult::Page { added, exhausted } => format!(
@@ -2538,12 +2558,13 @@ impl MatrixRoom {
2538 let room = self.room(); 2558 let room = self.room();
2539 let room_id = room.room_id().to_owned(); 2559 let room_id = room.room_id().to_owned();
2540 2560
2541 if let Ok(r) = 2561 if let Ok(r) = connection
2542 connection.room_messages(room, prev_batch, limit).await 2562 .room_messages(room, prev_batch.clone(), limit)
2563 .await
2543 { 2564 {
2544 let added = r.chunk.len(); 2565 let added = r.chunk.len();
2545 let exhausted = r.chunk.is_empty() || r.end.is_none(); 2566 let (next_prev_batch, exhausted) =
2546 2567 next_history_page_state(&prev_batch, r.end.clone(), added);
2547 for event in 2568 for event in
2548 r.chunk.iter().filter_map(|e| e.raw().deserialize().ok()) 2569 r.chunk.iter().filter_map(|e| e.raw().deserialize().ok())
2549 { 2570 {
@@ -2564,16 +2585,14 @@ impl MatrixRoom {
2564 2585
2565 let mut prev_batch = self.prev_batch.borrow_mut(); 2586 let mut prev_batch = self.prev_batch.borrow_mut();
2566 2587
2567 if let Some(PrevBatch::Forward(t)) = prev_batch.as_ref() { 2588 if matches!(prev_batch.as_ref(), Some(PrevBatch::Forward(_))) {
2568 *prev_batch = 2589 *prev_batch = next_prev_batch;
2569 Some(PrevBatch::Backwards(Some(t.to_owned())));
2570 self.buffer.sort_messages(); 2590 self.buffer.sort_messages();
2571 } else if r.chunk.is_empty() {
2572 *prev_batch = None;
2573 } else { 2591 } else {
2574 *prev_batch = 2592 *prev_batch = next_prev_batch;
2575 r.end.map(|token| PrevBatch::Backwards(Some(token))); 2593 if added > 0 {
2576 self.buffer.sort_messages(); 2594 self.buffer.sort_messages();
2595 }
2577 } 2596 }
2578 2597
2579 HistoryPageResult::Page { added, exhausted } 2598 HistoryPageResult::Page { added, exhausted }
@@ -3256,6 +3275,45 @@ mod tests {
3256 )))); 3275 ))));
3257 } 3276 }
3258 3277
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
3259 #[test] 3317 #[test]
3260 fn interactive_history_pages_are_bounded() { 3318 fn interactive_history_pages_are_bounded() {
3261 assert_eq!(INTERACTIVE_HISTORY_BATCH_SIZE, 25); 3319 assert_eq!(INTERACTIVE_HISTORY_BATCH_SIZE, 25);