| @@ -62,7 +62,7 @@ use matrix_sdk::{ |
| 62 | 62 | IncludeRelations, RelationsOptions, Room, |
| 63 | 63 | }, |
| 64 | 64 | ruma::{ |
| 65 | | api::Direction, |
| 65 | api::{client::threads::get_threads::v1::IncludeThreads, Direction}, |
| 66 | 66 | events::{ |
| 67 | 67 | relation::{RelationType, Thread}, |
| 68 | 68 | room::{ |
| @@ -357,6 +357,8 @@ pub struct MatrixRoom { |
| 357 | 357 | Rc<RefCell<HashSet<EncryptedMessageRecovery>>>, |
| 358 | 358 | thread_history_in_flight: Rc<RefCell<HashSet<OwnedEventId>>>, |
| 359 | 359 | thread_history_loaded: Rc<RefCell<HashSet<OwnedEventId>>>, |
| 360 | thread_listing: Rc<RefCell<ThreadListing>>, |
| 361 | thread_root_renders: Rc<RefCell<HashMap<OwnedEventId, RenderedEvent>>>, |
| 360 | 362 | |
| 361 | 363 | outgoing_messages: MessageQueue, |
| 362 | 364 | |
| @@ -662,6 +664,76 @@ fn thread_history_page_is_complete( |
| 662 | 664 | event_count == 0 || next_batch_token.is_none() |
| 663 | 665 | } |
| 664 | 666 | |
| 667 | const THREAD_LIST_TAGS: &[&str] = |
| 668 | &["matrix_thread_list", "matrix_smart_filter"]; |
| 669 | const THREAD_LIST_PREVIEW_GRAPHEMES: usize = 80; |
| 670 | |
| 671 | /// Which threads a `/matrix threads list` request should cover. |
| 672 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 673 | pub enum ThreadFilter { |
| 674 | #[default] |
| 675 | All, |
| 676 | Participated, |
| 677 | } |
| 678 | |
| 679 | impl ThreadFilter { |
| 680 | pub fn from_arg(value: Option<&str>) -> Self { |
| 681 | match value { |
| 682 | Some("participated") => Self::Participated, |
| 683 | _ => Self::All, |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | fn as_include(&self) -> IncludeThreads { |
| 688 | match self { |
| 689 | Self::All => IncludeThreads::All, |
| 690 | Self::Participated => IncludeThreads::Participated, |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | fn as_str(&self) -> &'static str { |
| 695 | match self { |
| 696 | Self::All => "all", |
| 697 | Self::Participated => "participated", |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /// The result of the last thread listing of a room. |
| 703 | /// |
| 704 | /// `entries` doubles as the index backing `/matrix threads open <n>`. |
| 705 | #[derive(Default)] |
| 706 | struct ThreadListing { |
| 707 | filter: ThreadFilter, |
| 708 | entries: Vec<OwnedEventId>, |
| 709 | next_batch: Option<String>, |
| 710 | } |
| 711 | |
| 712 | fn thread_preview_body(content: &AnyMessageLikeEventContent) -> String { |
| 713 | match content { |
| 714 | AnyMessageLikeEventContent::RoomMessage(message) => { |
| 715 | message.msgtype.body().to_owned() |
| 716 | } |
| 717 | AnyMessageLikeEventContent::RoomEncrypted(_) => { |
| 718 | "<encrypted>".to_owned() |
| 719 | } |
| 720 | _ => String::new(), |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | fn thread_preview_line(body: &str) -> String { |
| 725 | let single_line = body.lines().next().unwrap_or("").trim().to_owned(); |
| 726 | let mut graphemes = single_line.graphemes(true); |
| 727 | let mut preview: String = |
| 728 | graphemes.by_ref().take(THREAD_LIST_PREVIEW_GRAPHEMES).collect(); |
| 729 | |
| 730 | if graphemes.next().is_some() { |
| 731 | preview.push('…'); |
| 732 | } |
| 733 | |
| 734 | preview |
| 735 | } |
| 736 | |
| 665 | 737 | impl RoomHandle { |
| 666 | 738 | pub fn new( |
| 667 | 739 | server_name: &str, |
| @@ -712,6 +784,8 @@ impl RoomHandle { |
| 712 | 784 | pending_encrypted_recoveries: Rc::new(RefCell::new(HashSet::new())), |
| 713 | 785 | thread_history_in_flight: Rc::new(RefCell::new(HashSet::new())), |
| 714 | 786 | thread_history_loaded: Rc::new(RefCell::new(HashSet::new())), |
| 787 | thread_listing: Rc::new(RefCell::new(ThreadListing::default())), |
| 788 | thread_root_renders: Rc::new(RefCell::new(HashMap::new())), |
| 715 | 789 | own_user_id, |
| 716 | 790 | members, |
| 717 | 791 | buffer, |
| @@ -1122,7 +1196,15 @@ impl MatrixRoom { |
| 1122 | 1196 | &self, |
| 1123 | 1197 | thread_root: &EventId, |
| 1124 | 1198 | ) -> Option<BufferHandle> { |
| 1125 | | if !self.buffer.contains_event(thread_root) { |
| 1199 | // A root that was never rendered into the room buffer can still be |
| 1200 | // opened when the thread listing told us about it; the listing caches |
| 1201 | // a rendering of the root for seeding the thread buffer. |
| 1202 | let listed = self |
| 1203 | .thread_root_renders |
| 1204 | .borrow() |
| 1205 | .contains_key(thread_root); |
| 1206 | |
| 1207 | if !listed && !self.buffer.contains_event(thread_root) { |
| 1126 | 1208 | return None; |
| 1127 | 1209 | } |
| 1128 | 1210 | |
| @@ -1133,6 +1215,18 @@ impl MatrixRoom { |
| 1133 | 1215 | buffer |
| 1134 | 1216 | } |
| 1135 | 1217 | |
| 1218 | /// Open the `index`-th (1-based) thread of the last thread listing. |
| 1219 | pub fn open_listed_thread(&self, index: usize) -> Option<BufferHandle> { |
| 1220 | let thread_root = self |
| 1221 | .thread_listing |
| 1222 | .borrow() |
| 1223 | .entries |
| 1224 | .get(index.checked_sub(1)?) |
| 1225 | .cloned()?; |
| 1226 | |
| 1227 | self.open_thread_buffer(&thread_root) |
| 1228 | } |
| 1229 | |
| 1136 | 1230 | fn resume_thread_continuation(&self, source_root: OwnedEventId) { |
| 1137 | 1231 | let source = RoomHandle { |
| 1138 | 1232 | inner: self.clone(), |
| @@ -1172,6 +1266,147 @@ impl MatrixRoom { |
| 1172 | 1266 | } |
| 1173 | 1267 | } |
| 1174 | 1268 | |
| 1269 | /// Print the first page of the threads of this room. |
| 1270 | /// |
| 1271 | /// Every listed root is cached so `/matrix threads open <n>` can open a |
| 1272 | /// thread whose root was never rendered into the room buffer. |
| 1273 | pub async fn list_threads(&self, filter: ThreadFilter) { |
| 1274 | self.fetch_thread_listing(filter, None).await; |
| 1275 | } |
| 1276 | |
| 1277 | /// Print the next page of the last thread listing. |
| 1278 | pub async fn more_threads(&self) { |
| 1279 | let (filter, from) = { |
| 1280 | let listing = self.thread_listing.borrow(); |
| 1281 | (listing.filter, listing.next_batch.clone()) |
| 1282 | }; |
| 1283 | |
| 1284 | let Some(from) = from else { |
| 1285 | let message = if self.thread_listing.borrow().entries.is_empty() { |
| 1286 | "No thread listing yet; run /matrix threads list first." |
| 1287 | } else { |
| 1288 | "No more threads to list." |
| 1289 | }; |
| 1290 | self.print_room_line(THREAD_LIST_TAGS, message); |
| 1291 | return; |
| 1292 | }; |
| 1293 | |
| 1294 | self.fetch_thread_listing(filter, Some(from)).await; |
| 1295 | } |
| 1296 | |
| 1297 | async fn fetch_thread_listing( |
| 1298 | &self, |
| 1299 | filter: ThreadFilter, |
| 1300 | from: Option<String>, |
| 1301 | ) { |
| 1302 | let append = from.is_some(); |
| 1303 | let Some(connection) = self.connection.borrow().as_ref().cloned() |
| 1304 | else { |
| 1305 | self.print_room_line( |
| 1306 | THREAD_LIST_TAGS, |
| 1307 | "Threads: not connected to a homeserver.", |
| 1308 | ); |
| 1309 | return; |
| 1310 | }; |
| 1311 | |
| 1312 | let response = match connection |
| 1313 | .room_threads(self.room_id.clone(), filter.as_include(), from) |
| 1314 | .await |
| 1315 | { |
| 1316 | Ok(response) => response, |
| 1317 | Err(error) => { |
| 1318 | self.print_room_line( |
| 1319 | THREAD_LIST_TAGS, |
| 1320 | &format!("Failed to list threads: {error}"), |
| 1321 | ); |
| 1322 | return; |
| 1323 | } |
| 1324 | }; |
| 1325 | |
| 1326 | let mut entries = if append { |
| 1327 | std::mem::take(&mut self.thread_listing.borrow_mut().entries) |
| 1328 | } else { |
| 1329 | Vec::new() |
| 1330 | }; |
| 1331 | |
| 1332 | if !append { |
| 1333 | self.print_room_line( |
| 1334 | THREAD_LIST_TAGS, |
| 1335 | &format!("Threads in {} ({})", self.room_id, filter.as_str()), |
| 1336 | ); |
| 1337 | } |
| 1338 | |
| 1339 | for raw in response.chunk { |
| 1340 | let Ok(AnyTimelineEvent::MessageLike(event)) = raw.deserialize() |
| 1341 | else { |
| 1342 | continue; |
| 1343 | }; |
| 1344 | |
| 1345 | let thread_root = event.event_id().to_owned(); |
| 1346 | let content = event.original_content(); |
| 1347 | let sender = self.members.get(event.sender()).await; |
| 1348 | let sender_nick = sender |
| 1349 | .as_ref() |
| 1350 | .map(|member| member.nick()) |
| 1351 | .unwrap_or_else(|| event.sender().to_string()); |
| 1352 | |
| 1353 | if let (Some(content), Some(sender)) = (&content, &sender) { |
| 1354 | if let Some(rendered) = self |
| 1355 | .render_message_content( |
| 1356 | &thread_root, |
| 1357 | event.origin_server_ts(), |
| 1358 | sender, |
| 1359 | content, |
| 1360 | ) |
| 1361 | .await |
| 1362 | .map(RenderedEvent::add_backlog_tags) |
| 1363 | { |
| 1364 | self.thread_root_renders |
| 1365 | .borrow_mut() |
| 1366 | .insert(thread_root.clone(), rendered); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | let preview = content |
| 1371 | .as_ref() |
| 1372 | .map(thread_preview_body) |
| 1373 | .map(|body| thread_preview_line(&body)) |
| 1374 | .filter(|preview| !preview.is_empty()) |
| 1375 | .unwrap_or_else(|| "<event>".to_owned()); |
| 1376 | |
| 1377 | entries.push(thread_root); |
| 1378 | self.print_room_line( |
| 1379 | THREAD_LIST_TAGS, |
| 1380 | &format!("[{}] {}: {}", entries.len(), sender_nick, preview), |
| 1381 | ); |
| 1382 | } |
| 1383 | |
| 1384 | let listed = entries.len(); |
| 1385 | let has_more = response.next_batch.is_some(); |
| 1386 | |
| 1387 | *self.thread_listing.borrow_mut() = ThreadListing { |
| 1388 | filter, |
| 1389 | entries, |
| 1390 | next_batch: response.next_batch, |
| 1391 | }; |
| 1392 | |
| 1393 | let hint = if has_more { |
| 1394 | "More threads: /matrix threads more. Open one: /matrix threads open <n>" |
| 1395 | } else { |
| 1396 | "End of the thread list. Open one: /matrix threads open <n>" |
| 1397 | }; |
| 1398 | self.print_room_line( |
| 1399 | THREAD_LIST_TAGS, |
| 1400 | &format!("{listed} thread(s) listed. {hint}"), |
| 1401 | ); |
| 1402 | } |
| 1403 | |
| 1404 | fn print_room_line(&self, tags: &[&str], message: &str) { |
| 1405 | if let Ok(buffer) = self.buffer.buffer_handle().upgrade() { |
| 1406 | buffer.print_date_tags(0, tags, message); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1175 | 1410 | pub fn release_sdk_state(&self) { |
| 1176 | 1411 | self.verification.release_sdk_state(); |
| 1177 | 1412 | self.room.borrow_mut().take(); |
| @@ -1595,6 +1830,17 @@ impl MatrixRoom { |
| 1595 | 1830 | self.buffer |
| 1596 | 1831 | .set_thread_buffer(thread_root.to_owned(), buffer_handle.clone()); |
| 1597 | 1832 | |
| 1833 | // The root may never have been rendered into the room buffer, for |
| 1834 | // instance when the thread was discovered by the thread listing. |
| 1835 | // Seed the thread buffer with the rendering the listing cached. |
| 1836 | if !self.buffer.thread_contains_event(thread_root, thread_root) { |
| 1837 | if let Some(rendered) = |
| 1838 | self.thread_root_renders.borrow().get(thread_root) |
| 1839 | { |
| 1840 | print_rendered_event_to_buffer(&buffer, rendered); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1598 | 1844 | self.fetch_thread_history(thread_root.to_owned()); |
| 1599 | 1845 | |
| 1600 | 1846 | Some(buffer_handle) |
| @@ -1618,7 +1864,7 @@ impl MatrixRoom { |
| 1618 | 1864 | thread_root.and_then(|root| self.get_or_create_thread_buffer(root)) |
| 1619 | 1865 | { |
| 1620 | 1866 | if let Ok(buffer) = handle.upgrade() { |
| 1621 | | print_rendered_event_to_buffer(&buffer, rendered); |
| 1867 | print_rendered_event_to_buffer(&buffer, &rendered); |
| 1622 | 1868 | } else { |
| 1623 | 1869 | self.buffer.print_rendered_event(rendered); |
| 1624 | 1870 | } |