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 {
145145 })
146146 }
147147
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
148161 /// Replace the local echo of an event with a fully rendered one.
149162 pub fn replace_local_echo(
150163 &self,
@@ -401,11 +414,7 @@ impl RoomBuffer {
401414 &self,
402415 thread_root: &EventId,
403416 ) -> String {
404 format!(
405 "{}.thread.{}",
406 self.calculate_buffer_name(),
407 Self::thread_buffer_suffix(thread_root)
408 )
417 format_thread_buffer_name(&self.calculate_buffer_name(), thread_root)
409418 }
410419
411420 pub fn thread_buffer_suffix(thread_root: &EventId) -> String {
@@ -420,6 +429,30 @@ fn buffer_contains_tag(buffer: &Buffer, tag: &Cow<str>) -> bool {
420429 .is_some()
421430}
422431
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
423456fn sanitize_thread_id(thread_root: &EventId) -> String {
424457 const THREAD_ID_DISPLAY_CHARS: usize = 12;
425458
@@ -471,6 +504,14 @@ fn format_buffer_name(room_name: &str, is_direct: bool) -> String {
471504 room_name
472505}
473506
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
474515impl RoomBuffer {
475516 pub fn update_buffer_name(&self) {
476517 let buffer = self.buffer_handle();
@@ -554,8 +595,8 @@ mod tests {
554595 use matrix_sdk::ruma::{event_id, user_id};
555596
556597 use super::{
557 format_buffer_name, reply_sender_id_from_tags, sanitize_thread_id,
558 RoomBuffer,
598 format_buffer_name, format_thread_buffer_name,
599 reply_sender_id_from_tags, sanitize_thread_id, RoomBuffer,
559600 };
560601
561602 #[test]
@@ -616,6 +657,17 @@ mod tests {
616657 "abcdef012345"
617658 );
618659 }
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 }
619671}
620672
621673struct ParentSpaceInfo {
src/room/mod.rs +1
@@ -877,6 +877,7 @@ impl MatrixRoom {
877877 thread_root, room_short_name
878878 ));
879879
880 self.buffer.seed_thread_buffer(thread_root, &buffer);
880881 self.buffer
881882 .set_thread_buffer(thread_root.to_owned(), buffer_handle.clone());
882883