Commit 9f54607754
Unsigned
Layout: unified · split
src/room/buffer.rs +83
| @@ -211,6 +211,42 @@ impl RoomBuffer { | |||
| 211 | }); | 211 | }); |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | pub fn redact_event_lines<F, G>( | ||
| 215 | &self, | ||
| 216 | event_id_tag: &Cow<str>, | ||
| 217 | redacted_tag: &Cow<str>, | ||
| 218 | redact_first_line: F, | ||
| 219 | redact_rest_line: G, | ||
| 220 | ) -> bool | ||
| 221 | where | ||
| 222 | F: Fn(Cow<str>) -> String, | ||
| 223 | G: Fn(Cow<str>) -> String, | ||
| 224 | { | ||
| 225 | let mut redacted = self.buffer_handle().upgrade().is_ok_and(|buffer| { | ||
| 226 | redact_event_lines_in_buffer( | ||
| 227 | &buffer, | ||
| 228 | event_id_tag, | ||
| 229 | redacted_tag, | ||
| 230 | &redact_first_line, | ||
| 231 | &redact_rest_line, | ||
| 232 | ) | ||
| 233 | }); | ||
| 234 | |||
| 235 | for handle in self.thread_buffers.borrow().values() { | ||
| 236 | redacted |= handle.upgrade().is_ok_and(|buffer| { | ||
| 237 | redact_event_lines_in_buffer( | ||
| 238 | &buffer, | ||
| 239 | event_id_tag, | ||
| 240 | redacted_tag, | ||
| 241 | &redact_first_line, | ||
| 242 | &redact_rest_line, | ||
| 243 | ) | ||
| 244 | }); | ||
| 245 | } | ||
| 246 | |||
| 247 | redacted | ||
| 248 | } | ||
| 249 | |||
| 214 | fn replace_edit_in_buffer( | 250 | fn replace_edit_in_buffer( |
| 215 | &self, | 251 | &self, |
| 216 | buffer: &Buffer, | 252 | buffer: &Buffer, |
| @@ -454,6 +490,53 @@ fn copy_buffer_line_by_tag( | |||
| 454 | true | 490 | true |
| 455 | } | 491 | } |
| 456 | 492 | ||
| 493 | fn redact_event_lines_in_buffer<F, G>( | ||
| 494 | buffer: &Buffer, | ||
| 495 | event_id_tag: &Cow<str>, | ||
| 496 | redacted_tag: &Cow<str>, | ||
| 497 | redact_first_line: &F, | ||
| 498 | redact_rest_line: &G, | ||
| 499 | ) -> bool | ||
| 500 | where | ||
| 501 | F: Fn(Cow<str>) -> String, | ||
| 502 | G: Fn(Cow<str>) -> String, | ||
| 503 | { | ||
| 504 | let predicate = |line: &BufferLine| { | ||
| 505 | let tags = line.tags(); | ||
| 506 | tags.contains(event_id_tag) && !tags.contains(redacted_tag) | ||
| 507 | }; | ||
| 508 | |||
| 509 | let mut lines = buffer.lines(); | ||
| 510 | let first_line = lines.rfind(predicate); | ||
| 511 | |||
| 512 | if let Some(line) = first_line { | ||
| 513 | modify_line(line, redacted_tag.clone(), redact_first_line); | ||
| 514 | } else { | ||
| 515 | return false; | ||
| 516 | } | ||
| 517 | |||
| 518 | while let Some(line) = lines.next_back().filter(predicate) { | ||
| 519 | modify_line(line, redacted_tag.clone(), redact_rest_line); | ||
| 520 | } | ||
| 521 | |||
| 522 | true | ||
| 523 | } | ||
| 524 | |||
| 525 | fn modify_line<F>(line: BufferLine, tag: Cow<str>, redaction_func: F) | ||
| 526 | where | ||
| 527 | F: Fn(Cow<str>) -> String, | ||
| 528 | { | ||
| 529 | let message = line.message(); | ||
| 530 | let new_message = redaction_func(message); | ||
| 531 | |||
| 532 | let mut tags = line.tags(); | ||
| 533 | tags.push(tag); | ||
| 534 | let tags: Vec<&str> = tags.iter().map(|tag| tag.as_ref()).collect(); | ||
| 535 | |||
| 536 | line.set_message(&new_message); | ||
| 537 | line.set_tags(&tags); | ||
| 538 | } | ||
| 539 | |||
| 457 | fn sanitize_thread_id(thread_root: &EventId) -> String { | 540 | fn sanitize_thread_id(thread_root: &EventId) -> String { |
| 458 | const THREAD_ID_DISPLAY_CHARS: usize = 12; | 541 | const THREAD_ID_DISPLAY_CHARS: usize = 12; |
| 459 | 542 | ||
src/room/mod.rs +6 −42
| @@ -78,7 +78,6 @@ use matrix_sdk::{ | |||
| 78 | use weechat::{ | 78 | use weechat::{ |
| 79 | buffer::{ | 79 | buffer::{ |
| 80 | Buffer, BufferBuilderAsync, BufferHandle, BufferInputCallbackAsync, | 80 | Buffer, BufferBuilderAsync, BufferHandle, BufferInputCallbackAsync, |
| 81 | BufferLine, | ||
| 82 | }, | 81 | }, |
| 83 | Prefix, Weechat, | 82 | Prefix, Weechat, |
| 84 | }; | 83 | }; |
| @@ -628,14 +627,6 @@ impl MatrixRoom { | |||
| 628 | return; | 627 | return; |
| 629 | }; | 628 | }; |
| 630 | 629 | ||
| 631 | let buffer_handle = self.buffer_handle(); | ||
| 632 | |||
| 633 | let buffer = if let Ok(b) = buffer_handle.upgrade() { | ||
| 634 | b | ||
| 635 | } else { | ||
| 636 | return; | ||
| 637 | }; | ||
| 638 | |||
| 639 | // TODO: remove this unwrap. | 630 | // TODO: remove this unwrap. |
| 640 | let redacter = self.members.get(&event.sender).await.unwrap(); | 631 | let redacter = self.members.get(&event.sender).await.unwrap(); |
| 641 | 632 | ||
| @@ -664,12 +655,6 @@ impl MatrixRoom { | |||
| 664 | 655 | ||
| 665 | let redaction_style = self.config.borrow().look().redaction_style(); | 656 | let redaction_style = self.config.borrow().look().redaction_style(); |
| 666 | 657 | ||
| 667 | let predicate = |l: &BufferLine| { | ||
| 668 | let tags = l.tags(); | ||
| 669 | tags.contains(&event_id_tag) | ||
| 670 | && !tags.contains(&Cow::from("matrix_redacted")) | ||
| 671 | }; | ||
| 672 | |||
| 673 | let strike_through = |string: Cow<str>| { | 658 | let strike_through = |string: Cow<str>| { |
| 674 | Weechat::remove_color(&string) | 659 | Weechat::remove_color(&string) |
| 675 | .graphemes(true) | 660 | .graphemes(true) |
| @@ -696,33 +681,12 @@ impl MatrixRoom { | |||
| 696 | RedactionStyle::StrikeThrough => strike_through(message), | 681 | RedactionStyle::StrikeThrough => strike_through(message), |
| 697 | }; | 682 | }; |
| 698 | 683 | ||
| 699 | fn modify_line<F>(line: BufferLine, tag: Cow<str>, redaction_func: F) | 684 | self.buffer.redact_event_lines( |
| 700 | where | 685 | &event_id_tag, |
| 701 | F: Fn(Cow<str>) -> String, | 686 | &tag, |
| 702 | { | 687 | redact_first_line, |
| 703 | let message = line.message(); | 688 | redact_string, |
| 704 | let new_message = redaction_func(message); | 689 | ); |
| 705 | |||
| 706 | let mut tags = line.tags(); | ||
| 707 | tags.push(tag); | ||
| 708 | let tags: Vec<&str> = tags.iter().map(|t| t.as_ref()).collect(); | ||
| 709 | |||
| 710 | line.set_message(&new_message); | ||
| 711 | line.set_tags(&tags); | ||
| 712 | } | ||
| 713 | |||
| 714 | let mut lines = buffer.lines(); | ||
| 715 | let first_line = lines.rfind(predicate); | ||
| 716 | |||
| 717 | if let Some(line) = first_line { | ||
| 718 | modify_line(line, tag.clone(), redact_first_line); | ||
| 719 | } else { | ||
| 720 | return; | ||
| 721 | } | ||
| 722 | |||
| 723 | while let Some(line) = lines.next_back().filter(predicate) { | ||
| 724 | modify_line(line, tag.clone(), redact_string); | ||
| 725 | } | ||
| 726 | } | 690 | } |
| 727 | 691 | ||
| 728 | async fn render_message_content( | 692 | async fn render_message_content( |