Commit a05dcfd08a
Unsigned
Layout: unified · split
src/commands/matrix.rs +31
| @@ -18,6 +18,7 @@ use super::{ | ||
| 18 | 18 | use crate::{ |
| 19 | 19 | commands::{DevicesCommand, KeysCommand, MediaCommand, VerifyCommand}, |
| 20 | 20 | config::ConfigHandle, |
| 21 | room::HistoryPageResult, | |
| 21 | 22 | MatrixServer, Servers, PLUGIN_NAME, |
| 22 | 23 | }; |
| 23 | 24 | |
| @@ -45,6 +46,7 @@ impl MatrixCommand { | ||
| 45 | 46 | .add_argument("disconnect <server-name>") |
| 46 | 47 | .add_argument("reconnect <server-name>") |
| 47 | 48 | .add_argument("sso-complete <server-name> <login-token>") |
| 49 | .add_argument("history") | |
| 48 | 50 | .add_argument("read") |
| 49 | 51 | .add_argument("version") |
| 50 | 52 | .add_argument("help <matrix-command> [<matrix-subcommand>]") |
| @@ -55,6 +57,7 @@ impl MatrixCommand { | ||
| 55 | 57 | reconnect: Reconnect to server(s). |
| 56 | 58 | join: Join a Matrix room by ID or alias. |
| 57 | 59 | sso-complete: Finish SSO login with a copied loginToken. |
| 60 | history: Load an older page of messages in the current room. | |
| 58 | 61 | read: Mark the current room as read. |
| 59 | 62 | version: Show version information about weechat-matrix. |
| 60 | 63 | devices: {} |
| @@ -302,6 +305,29 @@ Use /matrix [command] help to find out more.\n", | ||
| 302 | 305 | room.mark_as_read(); |
| 303 | 306 | } |
| 304 | 307 | } |
| 308 | ("history", _) => { | |
| 309 | let Some(room) = self.servers.find_room(buffer) else { | |
| 310 | Weechat::print(&format!( | |
| 311 | "{}{}: /matrix history needs to be run in a Matrix room buffer.", | |
| 312 | Weechat::prefix(Prefix::Error), | |
| 313 | PLUGIN_NAME, | |
| 314 | )); | |
| 315 | return; | |
| 316 | }; | |
| 317 | ||
| 318 | if !room.has_history_page() { | |
| 319 | room.print_history_page_result( | |
| 320 | HistoryPageResult::Unavailable, | |
| 321 | ); | |
| 322 | return; | |
| 323 | } | |
| 324 | ||
| 325 | Weechat::spawn(async move { | |
| 326 | let result = room.get_messages().await; | |
| 327 | room.print_history_page_result(result); | |
| 328 | }) | |
| 329 | .detach(); | |
| 330 | } | |
| 305 | 331 | ("version", _) => { |
| 306 | 332 | Weechat::print(&format!( |
| 307 | 333 | "{}: weechat-matrix version {} ({})", |
| @@ -436,6 +462,11 @@ impl CommandCallback for MatrixCommand { | ||
| 436 | 462 | SubCommand::with_name("read") |
| 437 | 463 | .about("Mark the current room as read."), |
| 438 | 464 | ) |
| 465 | .subcommand( | |
| 466 | SubCommand::with_name("history").about( | |
| 467 | "Load an older page of messages in the current room.", | |
| 468 | ), | |
| 469 | ) | |
| 439 | 470 | .subcommand( |
| 440 | 471 | SubCommand::with_name("version") |
| 441 | 472 | .about("Show version information about weechat-matrix."), |
src/room/mod.rs +97 −6
| @@ -120,6 +120,17 @@ pub enum PrevBatch { | ||
| 120 | 120 | Backwards(Option<String>), |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | #[derive(Debug, Eq, PartialEq)] | |
| 124 | pub enum HistoryPageResult { | |
| 125 | Page { added: usize, exhausted: bool }, | |
| 126 | Unavailable, | |
| 127 | Busy, | |
| 128 | Failed, | |
| 129 | } | |
| 130 | ||
| 131 | const HISTORY_PAGE_TAGS: [&str; 2] = | |
| 132 | ["matrix_history_page", "matrix_smart_filter"]; | |
| 133 | ||
| 123 | 134 | fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> { |
| 124 | 135 | // A restored SDK room does not always have a sync pagination token in its |
| 125 | 136 | // store. Matrix permits a backward /messages request without `from`, which |
| @@ -128,6 +139,30 @@ fn restored_prev_batch(prev_batch: Option<String>) -> Option<PrevBatch> { | ||
| 128 | 139 | Some(PrevBatch::Backwards(prev_batch)) |
| 129 | 140 | } |
| 130 | 141 | |
| 142 | fn has_history_page(prev_batch: &Option<PrevBatch>) -> bool { | |
| 143 | prev_batch.is_some() | |
| 144 | } | |
| 145 | ||
| 146 | fn history_page_marker(result: &HistoryPageResult) -> String { | |
| 147 | match result { | |
| 148 | HistoryPageResult::Page { added, exhausted } => format!( | |
| 149 | "matrix_history_page added={} exhausted={}", | |
| 150 | added, | |
| 151 | u8::from(*exhausted), | |
| 152 | ), | |
| 153 | HistoryPageResult::Unavailable => { | |
| 154 | "matrix_history_page added=0 exhausted=1 state=unavailable" | |
| 155 | .to_owned() | |
| 156 | } | |
| 157 | HistoryPageResult::Busy => { | |
| 158 | "matrix_history_page added=0 exhausted=0 state=busy".to_owned() | |
| 159 | } | |
| 160 | HistoryPageResult::Failed => { | |
| 161 | "matrix_history_page added=0 exhausted=0 state=failed".to_owned() | |
| 162 | } | |
| 163 | } | |
| 164 | } | |
| 165 | ||
| 131 | 166 | fn should_render_event(already_rendered: bool) -> bool { |
| 132 | 167 | !already_rendered |
| 133 | 168 | } |
| @@ -1842,6 +1877,10 @@ impl MatrixRoom { | ||
| 1842 | 1877 | self.messages_in_flight.locked() |
| 1843 | 1878 | } |
| 1844 | 1879 | |
| 1880 | pub fn has_history_page(&self) -> bool { | |
| 1881 | has_history_page(&self.prev_batch.borrow()) | |
| 1882 | } | |
| 1883 | ||
| 1845 | 1884 | pub fn reset_prev_batch(&self) { |
| 1846 | 1885 | // TODO: we'll want to be able to scroll up again after we clear the |
| 1847 | 1886 | // buffer. |
| @@ -1913,7 +1952,7 @@ impl MatrixRoom { | ||
| 1913 | 1952 | self.mark_latest_event_as_read(false); |
| 1914 | 1953 | } |
| 1915 | 1954 | |
| 1916 | pub async fn get_messages(&self) { | |
| 1955 | pub async fn get_messages(&self) -> HistoryPageResult { | |
| 1917 | 1956 | let messages_lock = self.messages_in_flight.clone(); |
| 1918 | 1957 | |
| 1919 | 1958 | let connection = self.connection.borrow().as_ref().cloned(); |
| @@ -1922,23 +1961,26 @@ impl MatrixRoom { | ||
| 1922 | 1961 | if let Some(p) = self.prev_batch.borrow().as_ref().cloned() { |
| 1923 | 1962 | p |
| 1924 | 1963 | } else { |
| 1925 | return; | |
| 1964 | return HistoryPageResult::Unavailable; | |
| 1926 | 1965 | }; |
| 1927 | 1966 | |
| 1928 | 1967 | let guard = if let Ok(l) = messages_lock.try_lock() { |
| 1929 | 1968 | l |
| 1930 | 1969 | } else { |
| 1931 | return; | |
| 1970 | return HistoryPageResult::Busy; | |
| 1932 | 1971 | }; |
| 1933 | 1972 | |
| 1934 | 1973 | Weechat::bar_item_update("buffer_modes"); |
| 1935 | 1974 | Weechat::bar_item_update("matrix_modes"); |
| 1936 | 1975 | |
| 1937 | if let Some(connection) = connection { | |
| 1976 | let result = if let Some(connection) = connection { | |
| 1938 | 1977 | let room = self.room(); |
| 1939 | 1978 | let room_id = room.room_id().to_owned(); |
| 1940 | 1979 | |
| 1941 | 1980 | if let Ok(r) = connection.room_messages(room, prev_batch).await { |
| 1981 | let added = r.chunk.len(); | |
| 1982 | let exhausted = r.chunk.is_empty() || r.end.is_none(); | |
| 1983 | ||
| 1942 | 1984 | for event in |
| 1943 | 1985 | r.chunk.iter().filter_map(|e| e.raw().deserialize().ok()) |
| 1944 | 1986 | { |
| @@ -1970,13 +2012,31 @@ impl MatrixRoom { | ||
| 1970 | 2012 | r.end.map(|token| PrevBatch::Backwards(Some(token))); |
| 1971 | 2013 | self.buffer.sort_messages(); |
| 1972 | 2014 | } |
| 2015 | ||
| 2016 | HistoryPageResult::Page { added, exhausted } | |
| 2017 | } else { | |
| 2018 | HistoryPageResult::Failed | |
| 1973 | 2019 | } |
| 1974 | } | |
| 2020 | } else { | |
| 2021 | HistoryPageResult::Failed | |
| 2022 | }; | |
| 1975 | 2023 | |
| 1976 | 2024 | drop(guard); |
| 1977 | 2025 | |
| 1978 | 2026 | Weechat::bar_item_update("buffer_modes"); |
| 1979 | 2027 | Weechat::bar_item_update("matrix_modes"); |
| 2028 | ||
| 2029 | result | |
| 2030 | } | |
| 2031 | ||
| 2032 | pub fn print_history_page_result(&self, result: HistoryPageResult) { | |
| 2033 | if let Ok(buffer) = self.buffer.buffer_handle().upgrade() { | |
| 2034 | buffer.print_date_tags( | |
| 2035 | 0, | |
| 2036 | &HISTORY_PAGE_TAGS, | |
| 2037 | &history_page_marker(&result), | |
| 2038 | ); | |
| 2039 | } | |
| 1980 | 2040 | } |
| 1981 | 2041 | |
| 1982 | 2042 | pub async fn get_messages_if_empty(&self) { |
| @@ -1986,7 +2046,7 @@ impl MatrixRoom { | ||
| 1986 | 2046 | }; |
| 1987 | 2047 | |
| 1988 | 2048 | if buffer.num_lines() == 0 { |
| 1989 | self.get_messages().await; | |
| 2049 | let _ = self.get_messages().await; | |
| 1990 | 2050 | } |
| 1991 | 2051 | } |
| 1992 | 2052 | |
| @@ -2398,6 +2458,37 @@ mod tests { | ||
| 2398 | 2458 | assert_eq!(restored_prev_batch(None), Some(PrevBatch::Backwards(None))); |
| 2399 | 2459 | } |
| 2400 | 2460 | |
| 2461 | #[test] | |
| 2462 | fn history_paging_requires_an_available_cursor() { | |
| 2463 | assert!(!has_history_page(&None)); | |
| 2464 | assert!(has_history_page(&Some(PrevBatch::Backwards(None)))); | |
| 2465 | assert!(has_history_page(&Some(PrevBatch::Backwards(Some( | |
| 2466 | "token".to_owned() | |
| 2467 | ))))); | |
| 2468 | assert!(has_history_page(&Some(PrevBatch::Forward( | |
| 2469 | "token".to_owned() | |
| 2470 | )))); | |
| 2471 | } | |
| 2472 | ||
| 2473 | #[test] | |
| 2474 | fn history_page_markers_are_machine_readable_and_hideable() { | |
| 2475 | assert_eq!( | |
| 2476 | HISTORY_PAGE_TAGS, | |
| 2477 | ["matrix_history_page", "matrix_smart_filter"] | |
| 2478 | ); | |
| 2479 | assert_eq!( | |
| 2480 | history_page_marker(&HistoryPageResult::Page { | |
| 2481 | added: 25, | |
| 2482 | exhausted: false, | |
| 2483 | }), | |
| 2484 | "matrix_history_page added=25 exhausted=0" | |
| 2485 | ); | |
| 2486 | assert_eq!( | |
| 2487 | history_page_marker(&HistoryPageResult::Unavailable), | |
| 2488 | "matrix_history_page added=0 exhausted=1 state=unavailable" | |
| 2489 | ); | |
| 2490 | } | |
| 2491 | ||
| 2401 | 2492 | #[test] |
| 2402 | 2493 | fn already_rendered_events_are_not_printed_again() { |
| 2403 | 2494 | assert!(should_render_event(false)); |