Commit 3217ba2157

3217ba21572dd66e39da55ca1a642f5e5f9ac30a

parent: e289083eeb

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-31 09:57 UTC
committer: <strk@kbt.io>

fix: preload useful Matrix history windows

Layout: unified · split

src/room/mod.rs +50
@@ -141,6 +141,8 @@ const HISTORY_PAGE_TAGS: [&str; 2] =
141 ["matrix_history_page", "matrix_smart_filter"]; 141 ["matrix_history_page", "matrix_smart_filter"];
142const RESTORED_HISTORY_BATCH_SIZE: u16 = 25; 142const RESTORED_HISTORY_BATCH_SIZE: u16 = 25;
143const INTERACTIVE_HISTORY_BATCH_SIZE: u16 = 25; 143const INTERACTIVE_HISTORY_BATCH_SIZE: u16 = 25;
144const RESTORED_HISTORY_TARGET_LINES: i32 = 100;
145const RESTORED_HISTORY_MAX_PAGES: usize = 10;
144 146
145fn restored_prev_batch(_prev_batch: Option<String>) -> Option<PrevBatch> { 147fn restored_prev_batch(_prev_batch: Option<String>) -> Option<PrevBatch> {
146 // The SDK does not replay the stored sync timeline when a room is restored. 148 // The SDK does not replay the stored sync timeline when a room is restored.
@@ -174,6 +176,16 @@ fn history_page_marker(result: &HistoryPageResult) -> String {
174 } 176 }
175} 177}
176 178
179fn should_continue_restored_history(
180 lines_before: i32,
181 lines_after: i32,
182 has_older_page: bool,
183) -> bool {
184 has_older_page
185 && lines_after > lines_before
186 && lines_after < RESTORED_HISTORY_TARGET_LINES
187}
188
177fn should_render_event(already_rendered: bool) -> bool { 189fn should_render_event(already_rendered: bool) -> bool {
178 !already_rendered 190 !already_rendered
179} 191}
@@ -2304,6 +2316,35 @@ impl MatrixRoom {
2304 } 2316 }
2305 } 2317 }
2306 2318
2319 pub async fn preload_restored_messages(&self) {
2320 for _ in 0..RESTORED_HISTORY_MAX_PAGES {
2321 let buffer_handle = self.buffer_handle();
2322 let Ok(buffer) = buffer_handle.upgrade() else {
2323 return;
2324 };
2325 let lines_before = buffer.num_lines();
2326 drop(buffer);
2327
2328 self.get_messages().await;
2329
2330 let buffer_handle = self.buffer_handle();
2331 let Ok(buffer) = buffer_handle.upgrade() else {
2332 return;
2333 };
2334 let lines_after = buffer.num_lines();
2335 drop(buffer);
2336
2337 let has_older_page = self.prev_batch.borrow().is_some();
2338 if !should_continue_restored_history(
2339 lines_before,
2340 lines_after,
2341 has_older_page,
2342 ) {
2343 break;
2344 }
2345 }
2346 }
2347
2307 pub async fn get_thread_messages(&self, thread_root: OwnedEventId) { 2348 pub async fn get_thread_messages(&self, thread_root: OwnedEventId) {
2308 if self.thread_history_loaded.borrow().contains(&thread_root) 2349 if self.thread_history_loaded.borrow().contains(&thread_root)
2309 || !self 2350 || !self
@@ -2939,6 +2980,15 @@ mod tests {
2939 ); 2980 );
2940 } 2981 }
2941 2982
2983 #[test]
2984 fn restored_history_keeps_paging_until_it_is_useful() {
2985 assert!(should_continue_restored_history(0, 13, true));
2986 assert!(should_continue_restored_history(87, 99, true));
2987 assert!(!should_continue_restored_history(99, 112, true));
2988 assert!(!should_continue_restored_history(13, 13, true));
2989 assert!(!should_continue_restored_history(0, 13, false));
2990 }
2991
2942 #[test] 2992 #[test]
2943 fn already_rendered_events_are_not_printed_again() { 2993 fn already_rendered_events_are_not_printed_again() {
2944 assert!(should_render_event(false)); 2994 assert!(should_render_event(false));
src/server.rs +1 −1
@@ -973,7 +973,7 @@ impl InnerServer {
973 // WeeChat's buffer_switch signal. Populate restored Matrix 973 // WeeChat's buffer_switch signal. Populate restored Matrix
974 // buffers proactively instead of waiting for a TUI-only hook. 974 // buffers proactively instead of waiting for a TUI-only hook.
975 Weechat::spawn(async move { 975 Weechat::spawn(async move {
976 buffer.get_messages().await; 976 buffer.preload_restored_messages().await;
977 buffer.recover_logged_encrypted_events().await; 977 buffer.recover_logged_encrypted_events().await;
978 }) 978 })
979 .detach(); 979 .detach();