| @@ -108,11 +108,15 @@ pub(super) fn active_room(room: &SharedRoom) -> Room { |
| 108 | 108 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 109 | 109 | pub enum PrevBatch { |
| 110 | 110 | Forward(String), |
| 111 | | Backwards(String), |
| 111 | Backwards(Option<String>), |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> { |
| 115 | | prev_batch.map(PrevBatch::Backwards) |
| 115 | // A restored SDK room does not always have a sync pagination token in its |
| 116 | // store. Matrix permits a backward /messages request without `from`, which |
| 117 | // starts at the newest visible event. Keep that request representable so a |
| 118 | // freshly restored room can still populate its WeeChat buffer. |
| 119 | Some(PrevBatch::Backwards(prev_batch)) |
| 116 | 120 | } |
| 117 | 121 | |
| 118 | 122 | fn should_render_event(already_rendered: bool) -> bool { |
| @@ -388,9 +392,9 @@ impl RoomHandle { |
| 388 | 392 | room_id: room_id.into(), |
| 389 | 393 | connection: connection.clone(), |
| 390 | 394 | config, |
| 391 | | prev_batch: Rc::new(RefCell::new( |
| 392 | | sdk_room.last_prev_batch().map(PrevBatch::Backwards), |
| 393 | | )), |
| 395 | prev_batch: Rc::new(RefCell::new(restored_prev_batch( |
| 396 | sdk_room.last_prev_batch(), |
| 397 | ))), |
| 394 | 398 | latest_event_id: Rc::new(RefCell::new(None)), |
| 395 | 399 | latest_read_event_id: Rc::new(RefCell::new(None)), |
| 396 | 400 | latest_thread_event_ids: Rc::new(RefCell::new(HashMap::new())), |
| @@ -1449,12 +1453,14 @@ impl MatrixRoom { |
| 1449 | 1453 | let mut prev_batch = self.prev_batch.borrow_mut(); |
| 1450 | 1454 | |
| 1451 | 1455 | if let Some(PrevBatch::Forward(t)) = prev_batch.as_ref() { |
| 1452 | | *prev_batch = Some(PrevBatch::Backwards(t.to_owned())); |
| 1456 | *prev_batch = |
| 1457 | Some(PrevBatch::Backwards(Some(t.to_owned()))); |
| 1453 | 1458 | self.buffer.sort_messages(); |
| 1454 | 1459 | } else if r.chunk.is_empty() { |
| 1455 | 1460 | *prev_batch = None; |
| 1456 | 1461 | } else { |
| 1457 | | *prev_batch = r.end.map(PrevBatch::Backwards); |
| 1462 | *prev_batch = |
| 1463 | r.end.map(|token| PrevBatch::Backwards(Some(token))); |
| 1458 | 1464 | self.buffer.sort_messages(); |
| 1459 | 1465 | } |
| 1460 | 1466 | } |
| @@ -1675,7 +1681,7 @@ impl MatrixRoom { |
| 1675 | 1681 | if let Ok(buffer) = self.buffer_handle().upgrade() { |
| 1676 | 1682 | if buffer.num_lines() == 0 { |
| 1677 | 1683 | *self.prev_batch.borrow_mut() = |
| 1678 | | self.room().last_prev_batch().map(PrevBatch::Backwards); |
| 1684 | restored_prev_batch(self.room().last_prev_batch()); |
| 1679 | 1685 | } |
| 1680 | 1686 | } |
| 1681 | 1687 | } |
| @@ -1800,13 +1806,13 @@ mod tests { |
| 1800 | 1806 | fn restored_rooms_fetch_history_backwards_from_prev_batch() { |
| 1801 | 1807 | assert_eq!( |
| 1802 | 1808 | restored_prev_batch(Some("token".to_owned())), |
| 1803 | | Some(PrevBatch::Backwards("token".to_owned())) |
| 1809 | Some(PrevBatch::Backwards(Some("token".to_owned()))) |
| 1804 | 1810 | ); |
| 1805 | 1811 | } |
| 1806 | 1812 | |
| 1807 | 1813 | #[test] |
| 1808 | | fn restored_rooms_without_prev_batch_have_no_history_request() { |
| 1809 | | assert_eq!(restored_prev_batch(None), None); |
| 1814 | fn restored_rooms_without_prev_batch_fetch_history_from_end() { |
| 1815 | assert_eq!(restored_prev_batch(None), Some(PrevBatch::Backwards(None))); |
| 1810 | 1816 | } |
| 1811 | 1817 | |
| 1812 | 1818 | #[test] |