| @@ -108,11 +108,15 @@ pub(super) fn active_room(room: &SharedRoom) -> Room { |
| 108 | #[derive(Debug, Clone, Eq, PartialEq)] |
108 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 109 | pub enum PrevBatch { |
109 | pub enum PrevBatch { |
| 110 | Forward(String), |
110 | Forward(String), |
| 111 | Backwards(String), |
111 | Backwards(Option<String>), |
| 112 | } |
112 | } |
| 113 | |
113 | |
| 114 | fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> { |
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 | fn should_render_event(already_rendered: bool) -> bool { |
122 | fn should_render_event(already_rendered: bool) -> bool { |
| @@ -388,9 +392,9 @@ impl RoomHandle { |
| 388 | room_id: room_id.into(), |
392 | room_id: room_id.into(), |
| 389 | connection: connection.clone(), |
393 | connection: connection.clone(), |
| 390 | config, |
394 | config, |
| 391 | prev_batch: Rc::new(RefCell::new( |
395 | prev_batch: Rc::new(RefCell::new(restored_prev_batch( |
| 392 | sdk_room.last_prev_batch().map(PrevBatch::Backwards), |
396 | sdk_room.last_prev_batch(), |
| 393 | )), |
397 | ))), |
| 394 | latest_event_id: Rc::new(RefCell::new(None)), |
398 | latest_event_id: Rc::new(RefCell::new(None)), |
| 395 | latest_read_event_id: Rc::new(RefCell::new(None)), |
399 | latest_read_event_id: Rc::new(RefCell::new(None)), |
| 396 | latest_thread_event_ids: Rc::new(RefCell::new(HashMap::new())), |
400 | latest_thread_event_ids: Rc::new(RefCell::new(HashMap::new())), |
| @@ -1449,12 +1453,14 @@ impl MatrixRoom { |
| 1449 | let mut prev_batch = self.prev_batch.borrow_mut(); |
1453 | let mut prev_batch = self.prev_batch.borrow_mut(); |
| 1450 | |
1454 | |
| 1451 | if let Some(PrevBatch::Forward(t)) = prev_batch.as_ref() { |
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 | self.buffer.sort_messages(); |
1458 | self.buffer.sort_messages(); |
| 1454 | } else if r.chunk.is_empty() { |
1459 | } else if r.chunk.is_empty() { |
| 1455 | *prev_batch = None; |
1460 | *prev_batch = None; |
| 1456 | } else { |
1461 | } else { |
| 1457 | *prev_batch = r.end.map(PrevBatch::Backwards); |
1462 | *prev_batch = |
| |
1463 | r.end.map(|token| PrevBatch::Backwards(Some(token))); |
| 1458 | self.buffer.sort_messages(); |
1464 | self.buffer.sort_messages(); |
| 1459 | } |
1465 | } |
| 1460 | } |
1466 | } |
| @@ -1675,7 +1681,7 @@ impl MatrixRoom { |
| 1675 | if let Ok(buffer) = self.buffer_handle().upgrade() { |
1681 | if let Ok(buffer) = self.buffer_handle().upgrade() { |
| 1676 | if buffer.num_lines() == 0 { |
1682 | if buffer.num_lines() == 0 { |
| 1677 | *self.prev_batch.borrow_mut() = |
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 | fn restored_rooms_fetch_history_backwards_from_prev_batch() { |
1806 | fn restored_rooms_fetch_history_backwards_from_prev_batch() { |
| 1801 | assert_eq!( |
1807 | assert_eq!( |
| 1802 | restored_prev_batch(Some("token".to_owned())), |
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 | #[test] |
1813 | #[test] |
| 1808 | fn restored_rooms_without_prev_batch_have_no_history_request() { |
1814 | fn restored_rooms_without_prev_batch_fetch_history_from_end() { |
| 1809 | assert_eq!(restored_prev_batch(None), None); |
1815 | assert_eq!(restored_prev_batch(None), Some(PrevBatch::Backwards(None))); |
| 1810 | } |
1816 | } |
| 1811 | |
1817 | |
| 1812 | #[test] |
1818 | #[test] |