Commit 6dc4a8715b

6dc4a8715b7c93425c0cb54c9c3946533d8f9cf7

parent: 586253021d

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-29 19:23 UTC
committer: <strk@kbt.io>

fix: preload restored Matrix room history

Layout: unified · split

src/connection.rs +4 −2
@@ -62,6 +62,7 @@ use crate::{
62 62
63const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); 63const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
64const SSO_CALLBACK_PORT: u16 = 29_325; 64const SSO_CALLBACK_PORT: u16 = 29_325;
65const HISTORY_BATCH_SIZE: u16 = 200;
65 66
66pub struct InteractiveAuthInfo { 67pub struct InteractiveAuthInfo {
67 pub user: String, 68 pub user: String,
@@ -262,14 +263,15 @@ impl Connection {
262 prev_batch: PrevBatch, 263 prev_batch: PrevBatch,
263 ) -> MatrixResult<Messages> { 264 ) -> MatrixResult<Messages> {
264 self.spawn(async move { 265 self.spawn(async move {
265 let request = match &prev_batch { 266 let mut request = match &prev_batch {
266 PrevBatch::Backwards(t) => { 267 PrevBatch::Backwards(t) => {
267 MessagesOptions::backward().from(Some(t.as_ref())) 268 MessagesOptions::backward().from(t.as_deref())
268 } 269 }
269 PrevBatch::Forward(t) => { 270 PrevBatch::Forward(t) => {
270 MessagesOptions::forward().from(Some(t.as_ref())) 271 MessagesOptions::forward().from(Some(t.as_ref()))
271 } 272 }
272 }; 273 };
274 request.limit = HISTORY_BATCH_SIZE.into();
273 275
274 room.messages(request).await 276 room.messages(request).await
275 }) 277 })
src/room/mod.rs +17 −11
@@ -108,11 +108,15 @@ pub(super) fn active_room(room: &SharedRoom) -> Room {
108#[derive(Debug, Clone, Eq, PartialEq)] 108#[derive(Debug, Clone, Eq, PartialEq)]
109pub enum PrevBatch { 109pub enum PrevBatch {
110 Forward(String), 110 Forward(String),
111 Backwards(String), 111 Backwards(Option<String>),
112} 112}
113 113
114fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> { 114fn 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
118fn should_render_event(already_rendered: bool) -> bool { 122fn 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]
src/server.rs +7 −1
@@ -953,7 +953,13 @@ impl InnerServer {
953 Ok(buffer) => { 953 Ok(buffer) => {
954 let room_id = buffer.room_id().to_owned(); 954 let room_id = buffer.room_id().to_owned();
955 955
956 self.rooms.borrow_mut().insert(room_id, buffer); 956 self.rooms.borrow_mut().insert(room_id, buffer.clone());
957
958 // Relay-native frontends select buffers without triggering
959 // WeeChat's buffer_switch signal. Populate restored Matrix
960 // buffers proactively instead of waiting for a TUI-only hook.
961 Weechat::spawn(async move { buffer.get_messages().await })
962 .detach();
957 } 963 }
958 Err(e) => self.print_error(&format!("Error restoring room: {}", e)), 964 Err(e) => self.print_error(&format!("Error restoring room: {}", e)),
959 } 965 }