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::{
6262
6363const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
6464const SSO_CALLBACK_PORT: u16 = 29_325;
65const HISTORY_BATCH_SIZE: u16 = 200;
6566
6667pub struct InteractiveAuthInfo {
6768 pub user: String,
@@ -262,14 +263,15 @@ impl Connection {
262263 prev_batch: PrevBatch,
263264 ) -> MatrixResult<Messages> {
264265 self.spawn(async move {
265 let request = match &prev_batch {
266 let mut request = match &prev_batch {
266267 PrevBatch::Backwards(t) => {
267 MessagesOptions::backward().from(Some(t.as_ref()))
268 MessagesOptions::backward().from(t.as_deref())
268269 }
269270 PrevBatch::Forward(t) => {
270271 MessagesOptions::forward().from(Some(t.as_ref()))
271272 }
272273 };
274 request.limit = HISTORY_BATCH_SIZE.into();
273275
274276 room.messages(request).await
275277 })
src/room/mod.rs +17 −11
@@ -108,11 +108,15 @@ pub(super) fn active_room(room: &SharedRoom) -> Room {
108108#[derive(Debug, Clone, Eq, PartialEq)]
109109pub enum PrevBatch {
110110 Forward(String),
111 Backwards(String),
111 Backwards(Option<String>),
112112}
113113
114114fn 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))
116120}
117121
118122fn should_render_event(already_rendered: bool) -> bool {
@@ -388,9 +392,9 @@ impl RoomHandle {
388392 room_id: room_id.into(),
389393 connection: connection.clone(),
390394 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 ))),
394398 latest_event_id: Rc::new(RefCell::new(None)),
395399 latest_read_event_id: Rc::new(RefCell::new(None)),
396400 latest_thread_event_ids: Rc::new(RefCell::new(HashMap::new())),
@@ -1449,12 +1453,14 @@ impl MatrixRoom {
14491453 let mut prev_batch = self.prev_batch.borrow_mut();
14501454
14511455 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())));
14531458 self.buffer.sort_messages();
14541459 } else if r.chunk.is_empty() {
14551460 *prev_batch = None;
14561461 } else {
1457 *prev_batch = r.end.map(PrevBatch::Backwards);
1462 *prev_batch =
1463 r.end.map(|token| PrevBatch::Backwards(Some(token)));
14581464 self.buffer.sort_messages();
14591465 }
14601466 }
@@ -1675,7 +1681,7 @@ impl MatrixRoom {
16751681 if let Ok(buffer) = self.buffer_handle().upgrade() {
16761682 if buffer.num_lines() == 0 {
16771683 *self.prev_batch.borrow_mut() =
1678 self.room().last_prev_batch().map(PrevBatch::Backwards);
1684 restored_prev_batch(self.room().last_prev_batch());
16791685 }
16801686 }
16811687 }
@@ -1800,13 +1806,13 @@ mod tests {
18001806 fn restored_rooms_fetch_history_backwards_from_prev_batch() {
18011807 assert_eq!(
18021808 restored_prev_batch(Some("token".to_owned())),
1803 Some(PrevBatch::Backwards("token".to_owned()))
1809 Some(PrevBatch::Backwards(Some("token".to_owned())))
18041810 );
18051811 }
18061812
18071813 #[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)));
18101816 }
18111817
18121818 #[test]
src/server.rs +7 −1
@@ -953,7 +953,13 @@ impl InnerServer {
953953 Ok(buffer) => {
954954 let room_id = buffer.room_id().to_owned();
955955
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();
957963 }
958964 Err(e) => self.print_error(&format!("Error restoring room: {}", e)),
959965 }