Commit cc002de0d5

cc002de0d5d8d6971981bee0808fecfa66c76bae

parent: 8454dcd167

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-26 08:38 UTC
committer: <strk@kbt.io>

feat: improve thread buffer context

Layout: unified · split

src/room/buffer.rs +59 −7
@@ -145,6 +145,19 @@ impl RoomBuffer {
145 }) 145 })
146 } 146 }
147 147
148 /// Copy the root event line into a newly created thread buffer.
149 pub fn seed_thread_buffer(
150 &self,
151 thread_root: &EventId,
152 thread_buffer: &Buffer,
153 ) -> bool {
154 let event_id_tag = Cow::from(thread_root.to_tag());
155
156 self.buffer_handle().upgrade().is_ok_and(|buffer| {
157 copy_buffer_line_by_tag(&buffer, thread_buffer, &event_id_tag)
158 })
159 }
160
148 /// Replace the local echo of an event with a fully rendered one. 161 /// Replace the local echo of an event with a fully rendered one.
149 pub fn replace_local_echo( 162 pub fn replace_local_echo(
150 &self, 163 &self,
@@ -401,11 +414,7 @@ impl RoomBuffer {
401 &self, 414 &self,
402 thread_root: &EventId, 415 thread_root: &EventId,
403 ) -> String { 416 ) -> String {
404 format!( 417 format_thread_buffer_name(&self.calculate_buffer_name(), thread_root)
405 "{}.thread.{}",
406 self.calculate_buffer_name(),
407 Self::thread_buffer_suffix(thread_root)
408 )
409 } 418 }
410 419
411 pub fn thread_buffer_suffix(thread_root: &EventId) -> String { 420 pub fn thread_buffer_suffix(thread_root: &EventId) -> String {
@@ -420,6 +429,30 @@ fn buffer_contains_tag(buffer: &Buffer, tag: &Cow<str>) -> bool {
420 .is_some() 429 .is_some()
421} 430}
422 431
432fn copy_buffer_line_by_tag(
433 source: &Buffer,
434 target: &Buffer,
435 tag: &Cow<str>,
436) -> bool {
437 let Some(line) = source.lines().rfind(|line| line.tags().contains(tag))
438 else {
439 return false;
440 };
441
442 let prefix = line.prefix();
443 let message = if prefix.is_empty() {
444 line.message().to_string()
445 } else {
446 format!("{}\t{}", prefix, line.message())
447 };
448 let tags = line.tags();
449 let tags: Vec<&str> = tags.iter().map(|tag| tag.as_ref()).collect();
450
451 target.print_date_tags(line.date(), &tags, &message);
452
453 true
454}
455
423fn sanitize_thread_id(thread_root: &EventId) -> String { 456fn sanitize_thread_id(thread_root: &EventId) -> String {
424 const THREAD_ID_DISPLAY_CHARS: usize = 12; 457 const THREAD_ID_DISPLAY_CHARS: usize = 12;
425 458
@@ -471,6 +504,14 @@ fn format_buffer_name(room_name: &str, is_direct: bool) -> String {
471 room_name 504 room_name
472} 505}
473 506
507fn format_thread_buffer_name(room_name: &str, thread_root: &EventId) -> String {
508 format!(
509 "{}.{}",
510 room_name,
511 RoomBuffer::thread_buffer_suffix(thread_root)
512 )
513}
514
474impl RoomBuffer { 515impl RoomBuffer {
475 pub fn update_buffer_name(&self) { 516 pub fn update_buffer_name(&self) {
476 let buffer = self.buffer_handle(); 517 let buffer = self.buffer_handle();
@@ -554,8 +595,8 @@ mod tests {
554 use matrix_sdk::ruma::{event_id, user_id}; 595 use matrix_sdk::ruma::{event_id, user_id};
555 596
556 use super::{ 597 use super::{
557 format_buffer_name, reply_sender_id_from_tags, sanitize_thread_id, 598 format_buffer_name, format_thread_buffer_name,
558 RoomBuffer, 599 reply_sender_id_from_tags, sanitize_thread_id, RoomBuffer,
559 }; 600 };
560 601
561 #[test] 602 #[test]
@@ -616,6 +657,17 @@ mod tests {
616 "abcdef012345" 657 "abcdef012345"
617 ); 658 );
618 } 659 }
660
661 #[test]
662 fn thread_buffer_name_uses_room_prefix_and_short_suffix() {
663 assert_eq!(
664 format_thread_buffer_name(
665 "#OSGeo",
666 event_id!("$abcdef0123456789:example.org")
667 ),
668 "#OSGeo.abcdef012345"
669 );
670 }
619} 671}
620 672
621struct ParentSpaceInfo { 673struct ParentSpaceInfo {
src/room/mod.rs +1
@@ -877,6 +877,7 @@ impl MatrixRoom {
877 thread_root, room_short_name 877 thread_root, room_short_name
878 )); 878 ));
879 879
880 self.buffer.seed_thread_buffer(thread_root, &buffer);
880 self.buffer 881 self.buffer
881 .set_thread_buffer(thread_root.to_owned(), buffer_handle.clone()); 882 .set_thread_buffer(thread_root.to_owned(), buffer_handle.clone());
882 883