Commit a0e043f362

a0e043f362de01d90afcfac056102644d726694f

parent: f53e2fdb70

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-29 12:41 UTC
committer: <strk@kbt.io>

fix: keep thread root copies consistent

Layout: unified · split

src/room/buffer.rs +124 −61
@@ -27,6 +27,26 @@ pub struct RoomBuffer {
2727 thread_buffers: Rc<RefCell<HashMap<OwnedEventId, BufferHandle>>>,
2828}
2929
30struct LineCopy {
31 date: isize,
32 date_printed: isize,
33 tags: Vec<String>,
34 prefix: String,
35 message: String,
36}
37
38impl<'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
3050impl RoomBuffer {
3151 pub fn new(room: SharedRoom, runtime: Handle) -> Self {
3252 Self {
@@ -244,27 +264,33 @@ impl RoomBuffer {
244264 let sender_tag = Cow::from(sender.to_tag());
245265 let event_id_tag = Cow::from(event_id.to_tag());
246266
247 if self.buffer_handle().upgrade().is_ok_and(|buffer| {
267 let _ = self.buffer_handle().upgrade().is_ok_and(|buffer| {
248268 self.replace_edit_in_buffer(
249269 &buffer,
250270 &event_id_tag,
251271 &sender_tag,
252272 &event,
253273 )
254 }) {
255 return;
256 }
274 });
257275
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(
261279 &buffer,
262280 &event_id_tag,
263281 &sender_tag,
264282 &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 }
268294 }
269295
270296 pub fn redact_event_lines<F, G>(
@@ -363,51 +389,15 @@ impl RoomBuffer {
363389 buffer.print_date_tags(date, &tags, &message)
364390 }
365391
366 self.sort_messages()
392 sort_buffer_lines(buffer, None)
367393 }
368394 Ordering::Equal => (),
369395 }
370396 }
371397
372398 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.
394399 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);
411401 }
412402 }
413403
@@ -527,25 +517,65 @@ fn copy_buffer_line_by_tag(
527517 target: &Buffer,
528518 tag: &Cow<str>,
529519) -> 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() {
532527 return false;
533 };
528 }
534529
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();
543537
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()));
545545
546546 true
547547}
548548
549fn 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
560fn 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
549579fn redact_event_lines_in_buffer<F, G>(
550580 buffer: &Buffer,
551581 event_id_tag: &Cow<str>,
@@ -753,7 +783,7 @@ mod tests {
753783 use matrix_sdk::ruma::{event_id, user_id};
754784
755785 use super::{
756 format_buffer_name, format_thread_buffer_name,
786 format_buffer_name, format_thread_buffer_name, line_sort_key,
757787 reply_sender_id_from_tags, sanitize_thread_id, RoomBuffer,
758788 };
759789
@@ -774,6 +804,39 @@ mod tests {
774804 assert_eq!(None, reply_sender_id_from_tags(&tags));
775805 }
776806
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
777840 #[test]
778841 fn preserves_named_channel_prefix() {
779842 assert_eq!(format_buffer_name("OSGeo", false, false), "#OSGeo");
src/room/mod.rs +6
@@ -1475,6 +1475,12 @@ impl MatrixRoom {
14751475
14761476 if echo {
14771477 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 }
14781484 } else {
14791485 self.print_rendered_event_for_relation(
14801486 Some(event_id),