Commit a0e043f362
Unsigned
Layout: unified · split
src/room/buffer.rs +124 −61
| @@ -27,6 +27,26 @@ pub struct RoomBuffer { | ||
| 27 | 27 | thread_buffers: Rc<RefCell<HashMap<OwnedEventId, BufferHandle>>>, |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | struct LineCopy { | |
| 31 | date: isize, | |
| 32 | date_printed: isize, | |
| 33 | tags: Vec<String>, | |
| 34 | prefix: String, | |
| 35 | message: String, | |
| 36 | } | |
| 37 | ||
| 38 | impl<'a> From<BufferLine<'a>> for LineCopy { | |
| 39 | fn from(line: BufferLine) -> Self { | |
| 40 | Self { | |
| 41 | date: line.date(), | |
| 42 | date_printed: line.date_printed(), | |
| 43 | message: line.message().to_string(), | |
| 44 | prefix: line.prefix().to_string(), | |
| 45 | tags: line.tags().iter().map(|t| t.to_string()).collect(), | |
| 46 | } | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 30 | 50 | impl RoomBuffer { |
| 31 | 51 | pub fn new(room: SharedRoom, runtime: Handle) -> Self { |
| 32 | 52 | Self { |
| @@ -244,27 +264,33 @@ impl RoomBuffer { | ||
| 244 | 264 | let sender_tag = Cow::from(sender.to_tag()); |
| 245 | 265 | let event_id_tag = Cow::from(event_id.to_tag()); |
| 246 | 266 | |
| 247 | if self.buffer_handle().upgrade().is_ok_and(|buffer| { | |
| 267 | let _ = self.buffer_handle().upgrade().is_ok_and(|buffer| { | |
| 248 | 268 | self.replace_edit_in_buffer( |
| 249 | 269 | &buffer, |
| 250 | 270 | &event_id_tag, |
| 251 | 271 | &sender_tag, |
| 252 | 272 | &event, |
| 253 | 273 | ) |
| 254 | }) { | |
| 255 | return; | |
| 256 | } | |
| 274 | }); | |
| 257 | 275 | |
| 258 | self.thread_buffers.borrow().values().any(|handle| { | |
| 259 | handle.upgrade().is_ok_and(|buffer| { | |
| 260 | self.replace_edit_in_buffer( | |
| 276 | for handle in self.thread_buffers.borrow().values() { | |
| 277 | if let Ok(buffer) = handle.upgrade() { | |
| 278 | let replaced = self.replace_edit_in_buffer( | |
| 261 | 279 | &buffer, |
| 262 | 280 | &event_id_tag, |
| 263 | 281 | &sender_tag, |
| 264 | 282 | &event, |
| 265 | ) | |
| 266 | }) | |
| 267 | }); | |
| 283 | ); | |
| 284 | ||
| 285 | if replaced | |
| 286 | && self | |
| 287 | .thread_root_for_buffer(&buffer) | |
| 288 | .is_some_and(|thread_root| thread_root == event_id) | |
| 289 | { | |
| 290 | sort_buffer_lines(&buffer, Some(event_id_tag.as_ref())); | |
| 291 | } | |
| 292 | } | |
| 293 | } | |
| 268 | 294 | } |
| 269 | 295 | |
| 270 | 296 | pub fn redact_event_lines<F, G>( |
| @@ -363,51 +389,15 @@ impl RoomBuffer { | ||
| 363 | 389 | buffer.print_date_tags(date, &tags, &message) |
| 364 | 390 | } |
| 365 | 391 | |
| 366 | self.sort_messages() | |
| 392 | sort_buffer_lines(buffer, None) | |
| 367 | 393 | } |
| 368 | 394 | Ordering::Equal => (), |
| 369 | 395 | } |
| 370 | 396 | } |
| 371 | 397 | |
| 372 | 398 | pub fn sort_messages(&self) { |
| 373 | struct LineCopy { | |
| 374 | date: isize, | |
| 375 | date_printed: isize, | |
| 376 | tags: Vec<String>, | |
| 377 | prefix: String, | |
| 378 | message: String, | |
| 379 | } | |
| 380 | ||
| 381 | impl<'a> From<BufferLine<'a>> for LineCopy { | |
| 382 | fn from(line: BufferLine) -> Self { | |
| 383 | Self { | |
| 384 | date: line.date(), | |
| 385 | date_printed: line.date_printed(), | |
| 386 | message: line.message().to_string(), | |
| 387 | prefix: line.prefix().to_string(), | |
| 388 | tags: line.tags().iter().map(|t| t.to_string()).collect(), | |
| 389 | } | |
| 390 | } | |
| 391 | } | |
| 392 | ||
| 393 | // TODO update the highlight once Weechat starts supporting it. | |
| 394 | 399 | if let Ok(buffer) = self.buffer_handle().upgrade() { |
| 395 | let mut lines: Vec<LineCopy> = | |
| 396 | buffer.lines().map(|l| l.into()).collect(); | |
| 397 | lines.sort_by_key(|l| l.date); | |
| 398 | ||
| 399 | for (line, new) in buffer.lines().zip(lines.drain(..)) { | |
| 400 | let tags = | |
| 401 | new.tags.iter().map(|t| t.as_str()).collect::<Vec<&str>>(); | |
| 402 | let data = LineData { | |
| 403 | prefix: Some(&new.prefix), | |
| 404 | message: Some(&new.message), | |
| 405 | date: Some(new.date), | |
| 406 | date_printed: Some(new.date_printed), | |
| 407 | tags: Some(&tags), | |
| 408 | }; | |
| 409 | line.update(data) | |
| 410 | } | |
| 400 | sort_buffer_lines(&buffer, None); | |
| 411 | 401 | } |
| 412 | 402 | } |
| 413 | 403 | |
| @@ -527,25 +517,65 @@ fn copy_buffer_line_by_tag( | ||
| 527 | 517 | target: &Buffer, |
| 528 | 518 | tag: &Cow<str>, |
| 529 | 519 | ) -> bool { |
| 530 | let Some(line) = source.lines().rfind(|line| line.tags().contains(tag)) | |
| 531 | else { | |
| 520 | let lines: Vec<LineCopy> = source | |
| 521 | .lines() | |
| 522 | .filter(|line| line.tags().contains(tag)) | |
| 523 | .map(Into::into) | |
| 524 | .collect(); | |
| 525 | ||
| 526 | if lines.is_empty() { | |
| 532 | 527 | return false; |
| 533 | }; | |
| 528 | } | |
| 534 | 529 | |
| 535 | let prefix = line.prefix(); | |
| 536 | let message = if prefix.is_empty() { | |
| 537 | line.message().to_string() | |
| 538 | } else { | |
| 539 | format!("{}\t{}", prefix, line.message()) | |
| 540 | }; | |
| 541 | let tags = line.tags(); | |
| 542 | let tags: Vec<&str> = tags.iter().map(|tag| tag.as_ref()).collect(); | |
| 530 | for line in lines { | |
| 531 | let message = if line.prefix.is_empty() { | |
| 532 | line.message | |
| 533 | } else { | |
| 534 | format!("{}\t{}", line.prefix, line.message) | |
| 535 | }; | |
| 536 | let tags: Vec<&str> = line.tags.iter().map(String::as_str).collect(); | |
| 543 | 537 | |
| 544 | target.print_date_tags(line.date(), &tags, &message); | |
| 538 | target.print_date_tags(line.date, &tags, &message); | |
| 539 | } | |
| 540 | ||
| 541 | // A thread buffer may already contain the reply that caused its creation. | |
| 542 | // Keep every line of the triggering event first even when Matrix timestamps | |
| 543 | // have only second-level precision or the root arrived through a later sync. | |
| 544 | sort_buffer_lines(target, Some(tag.as_ref())); | |
| 545 | 545 | |
| 546 | 546 | true |
| 547 | 547 | } |
| 548 | 548 | |
| 549 | fn line_sort_key( | |
| 550 | date: isize, | |
| 551 | tags: &[String], | |
| 552 | priority_tag: Option<&str>, | |
| 553 | ) -> (bool, isize) { | |
| 554 | let is_priority = | |
| 555 | priority_tag.is_some_and(|tag| tags.iter().any(|item| item == tag)); | |
| 556 | ||
| 557 | (!is_priority, date) | |
| 558 | } | |
| 559 | ||
| 560 | fn sort_buffer_lines(buffer: &Buffer, priority_tag: Option<&str>) { | |
| 561 | let mut lines: Vec<LineCopy> = buffer.lines().map(Into::into).collect(); | |
| 562 | lines | |
| 563 | .sort_by_key(|line| line_sort_key(line.date, &line.tags, priority_tag)); | |
| 564 | ||
| 565 | // TODO update the highlight once Weechat starts supporting it. | |
| 566 | for (line, new) in buffer.lines().zip(lines.drain(..)) { | |
| 567 | let tags = new.tags.iter().map(String::as_str).collect::<Vec<&str>>(); | |
| 568 | let data = LineData { | |
| 569 | prefix: Some(&new.prefix), | |
| 570 | message: Some(&new.message), | |
| 571 | date: Some(new.date), | |
| 572 | date_printed: Some(new.date_printed), | |
| 573 | tags: Some(&tags), | |
| 574 | }; | |
| 575 | line.update(data) | |
| 576 | } | |
| 577 | } | |
| 578 | ||
| 549 | 579 | fn redact_event_lines_in_buffer<F, G>( |
| 550 | 580 | buffer: &Buffer, |
| 551 | 581 | event_id_tag: &Cow<str>, |
| @@ -753,7 +783,7 @@ mod tests { | ||
| 753 | 783 | use matrix_sdk::ruma::{event_id, user_id}; |
| 754 | 784 | |
| 755 | 785 | use super::{ |
| 756 | format_buffer_name, format_thread_buffer_name, | |
| 786 | format_buffer_name, format_thread_buffer_name, line_sort_key, | |
| 757 | 787 | reply_sender_id_from_tags, sanitize_thread_id, RoomBuffer, |
| 758 | 788 | }; |
| 759 | 789 | |
| @@ -774,6 +804,39 @@ mod tests { | ||
| 774 | 804 | assert_eq!(None, reply_sender_id_from_tags(&tags)); |
| 775 | 805 | } |
| 776 | 806 | |
| 807 | #[test] | |
| 808 | fn thread_root_lines_sort_before_existing_replies() { | |
| 809 | let root_tag = "matrix_id_$root:example.org"; | |
| 810 | let root_tags = vec![root_tag.to_owned(), "matrix_text".to_owned()]; | |
| 811 | let reply_tags = vec!["matrix_text".to_owned()]; | |
| 812 | ||
| 813 | assert!( | |
| 814 | line_sort_key(200, &root_tags, Some(root_tag)) | |
| 815 | < line_sort_key(100, &reply_tags, Some(root_tag)) | |
| 816 | ); | |
| 817 | } | |
| 818 | ||
| 819 | #[test] | |
| 820 | fn thread_root_wins_same_timestamp_ties() { | |
| 821 | let root_tag = "matrix_id_$root:example.org"; | |
| 822 | let root_tags = vec![root_tag.to_owned()]; | |
| 823 | let reply_tags = vec!["matrix_text".to_owned()]; | |
| 824 | ||
| 825 | assert!( | |
| 826 | line_sort_key(100, &root_tags, Some(root_tag)) | |
| 827 | < line_sort_key(100, &reply_tags, Some(root_tag)) | |
| 828 | ); | |
| 829 | } | |
| 830 | ||
| 831 | #[test] | |
| 832 | fn ordinary_lines_remain_chronological() { | |
| 833 | let tags = vec!["matrix_text".to_owned()]; | |
| 834 | ||
| 835 | assert!( | |
| 836 | line_sort_key(100, &tags, None) < line_sort_key(200, &tags, None) | |
| 837 | ); | |
| 838 | } | |
| 839 | ||
| 777 | 840 | #[test] |
| 778 | 841 | fn preserves_named_channel_prefix() { |
| 779 | 842 | assert_eq!(format_buffer_name("OSGeo", false, false), "#OSGeo"); |
src/room/mod.rs +6
| @@ -1475,6 +1475,12 @@ impl MatrixRoom { | ||
| 1475 | 1475 | |
| 1476 | 1476 | if echo { |
| 1477 | 1477 | self.buffer.replace_local_echo(transaction_id, rendered); |
| 1478 | ||
| 1479 | if let Some(thread_root) = | |
| 1480 | rendered_root_to_seed(Some(event_id), thread_root.as_deref()) | |
| 1481 | { | |
| 1482 | self.buffer.seed_open_thread_buffer(thread_root); | |
| 1483 | } | |
| 1478 | 1484 | } else { |
| 1479 | 1485 | self.print_rendered_event_for_relation( |
| 1480 | 1486 | Some(event_id), |