Commit 39592eab8e

39592eab8e1617f52befaf0cf6ac1267cf2baea2

parent: c4ccdf76be

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-14 18:39 UTC

config: Add the redaction_style config option.

Layout: unified · split

Cargo.lock +3 −2
@@ -2125,9 +2125,9 @@ dependencies = [
2125 2125
2126[[package]] 2126[[package]]
2127name = "strum_macros" 2127name = "strum_macros"
2128version = "0.19.2" 2128version = "0.19.4"
2129source = "registry+https://github.com/rust-lang/crates.io-index" 2129source = "registry+https://github.com/rust-lang/crates.io-index"
2130checksum = "2d2ab682ecdcae7f5f45ae85cd7c1e6c8e68ea42c8a612d47fedf831c037146a" 2130checksum = "e61bb0be289045cb80bfce000512e32d09f8337e54c186725da381377ad1f8d5"
2131dependencies = [ 2131dependencies = [
2132 "heck", 2132 "heck",
2133 "proc-macro2", 2133 "proc-macro2",
@@ -2646,6 +2646,7 @@ dependencies = [
2646 "futures", 2646 "futures",
2647 "indoc", 2647 "indoc",
2648 "matrix-sdk", 2648 "matrix-sdk",
2649 "strum",
2649 "tokio", 2650 "tokio",
2650 "tracing", 2651 "tracing",
2651 "tracing-subscriber", 2652 "tracing-subscriber",
Cargo.toml +1
@@ -21,6 +21,7 @@ async-trait = "0.1.41"
21uuid = { version = "0.8.1", features = ["v4"] } 21uuid = { version = "0.8.1", features = ["v4"] }
22indoc = "1.0.2" 22indoc = "1.0.2"
23unicode-segmentation = "1.6.0" 23unicode-segmentation = "1.6.0"
24strum = "0.19.5"
24chrono = "0.4.19" 25chrono = "0.4.19"
25 26
26[dependencies.weechat] 27[dependencies.weechat]
src/config.rs +40 −7
@@ -15,7 +15,12 @@
15//! loaded so they don't need to be freed manually. The drop implementation of 15//! loaded so they don't need to be freed manually. The drop implementation of
16//! the section will do so. 16//! the section will do so.
17 17
18use crate::{MatrixServer, Servers}; 18use std::{
19 cell::{Ref, RefCell, RefMut},
20 rc::Rc,
21};
22
23use strum::EnumVariantNames;
19use weechat::{ 24use weechat::{
20 config, 25 config,
21 config::{ 26 config::{
@@ -25,10 +30,32 @@ use weechat::{
25 Weechat, 30 Weechat,
26}; 31};
27 32
28use std::{ 33use crate::{MatrixServer, Servers};
29 cell::{Ref, RefCell, RefMut}, 34
30 rc::Rc, 35#[derive(EnumVariantNames)]
31}; 36#[strum(serialize_all = "kebab_case")]
37pub enum RedactionStyle {
38 StrikeThrough,
39 Delete,
40 Notice,
41}
42
43impl Default for RedactionStyle {
44 fn default() -> Self {
45 RedactionStyle::StrikeThrough
46 }
47}
48
49impl From<i32> for RedactionStyle {
50 fn from(value: i32) -> Self {
51 match value {
52 0 => RedactionStyle::StrikeThrough,
53 1 => RedactionStyle::Delete,
54 2 => RedactionStyle::Notice,
55 _ => unreachable!(),
56 }
57 }
58}
32 59
33config!( 60config!(
34 "matrix-rust", 61 "matrix-rust",
@@ -46,7 +73,13 @@ config!(
46 the receipt of the message", 73 the receipt of the message",
47 // Default value 74 // Default value
48 true, 75 true,
49 } 76 },
77
78 redaction_style: Enum {
79 // Description
80 "The style that should be used when a message needs to be redacted",
81 RedactionStyle,
82 },
50 }, 83 },
51 Section network { 84 Section network {
52 debug_buffer: bool { 85 debug_buffer: bool {
@@ -54,7 +87,7 @@ config!(
54 "Use a separate buffer for debug logs", 87 "Use a separate buffer for debug logs",
55 // Default value. 88 // Default value.
56 false, 89 false,
57 } 90 },
58 } 91 }
59); 92);
60 93
src/room/mod.rs +21 −23
@@ -71,7 +71,7 @@ use weechat::{
71}; 71};
72 72
73use crate::{ 73use crate::{
74 config::Config, 74 config::{Config, RedactionStyle},
75 connection::{Connection, TYPING_NOTICE_TIMEOUT}, 75 connection::{Connection, TYPING_NOTICE_TIMEOUT},
76 render::{Render, RenderedEvent}, 76 render::{Render, RenderedEvent},
77 PLUGIN_NAME, 77 PLUGIN_NAME,
@@ -319,16 +319,6 @@ impl MatrixRoom {
319 319
320 let event_id_tag = 320 let event_id_tag =
321 Cow::from(format!("{}_id_{}", PLUGIN_NAME, event.redacts)); 321 Cow::from(format!("{}_id_{}", PLUGIN_NAME, event.redacts));
322 let event_type_tag = Cow::from("matrix_redacted");
323
324 let predicate = |l: &BufferLine| {
325 let tags = l.tags();
326 tags.contains(&event_id_tag) && !tags.contains(&event_type_tag)
327 };
328
329 let mut lines = buffer.lines();
330 let first_line = lines.rfind(predicate);
331
332 let tag = Cow::from("matrix_redacted"); 322 let tag = Cow::from("matrix_redacted");
333 323
334 let reason = if let Some(r) = &event.content.reason { 324 let reason = if let Some(r) = &event.content.reason {
@@ -336,7 +326,6 @@ impl MatrixRoom {
336 } else { 326 } else {
337 "".to_owned() 327 "".to_owned()
338 }; 328 };
339
340 let redaction_message = format!( 329 let redaction_message = format!(
341 "{}<{}Message redacted by: {}{}{}>{}", 330 "{}<{}Message redacted by: {}{}{}>{}",
342 Weechat::color("chat_delimiters"), 331 Weechat::color("chat_delimiters"),
@@ -347,6 +336,14 @@ impl MatrixRoom {
347 Weechat::color("reset"), 336 Weechat::color("reset"),
348 ); 337 );
349 338
339 let redaction_style = self.config.borrow().look().redaction_style();
340
341 let predicate = |l: &BufferLine| {
342 let tags = l.tags();
343 tags.contains(&event_id_tag)
344 && !tags.contains(&Cow::from("matrix_redacted"))
345 };
346
350 let strike_through = |string: Cow<str>| { 347 let strike_through = |string: Cow<str>| {
351 Weechat::remove_color(&string) 348 Weechat::remove_color(&string)
352 .graphemes(true) 349 .graphemes(true)
@@ -355,24 +352,22 @@ impl MatrixRoom {
355 .join("") 352 .join("")
356 }; 353 };
357 354
358 let redact_first_line = |message: Cow<str>| { 355 let redact_first_line = |message: Cow<str>| match redaction_style {
359 if false { 356 RedactionStyle::Delete => redaction_message.clone(),
360 redaction_message.clone() 357 RedactionStyle::Notice => {
361 } else if false {
362 format!("{} {}", message, redaction_message) 358 format!("{} {}", message, redaction_message)
363 } else { 359 }
360 RedactionStyle::StrikeThrough => {
364 format!("{} {}", strike_through(message), redaction_message) 361 format!("{} {}", strike_through(message), redaction_message)
365 } 362 }
366 }; 363 };
367 364
368 let redact_string = |message: Cow<str>| { 365 let redact_string = |message: Cow<str>| match redaction_style {
369 if false { 366 RedactionStyle::Delete => redaction_message.clone(),
370 redaction_message.clone() 367 RedactionStyle::Notice => {
371 } else if false {
372 format!("{} {}", message, redaction_message) 368 format!("{} {}", message, redaction_message)
373 } else {
374 strike_through(message)
375 } 369 }
370 RedactionStyle::StrikeThrough => strike_through(message),
376 }; 371 };
377 372
378 fn modify_line<F>(line: BufferLine, tag: Cow<str>, redaction_func: F) 373 fn modify_line<F>(line: BufferLine, tag: Cow<str>, redaction_func: F)
@@ -390,6 +385,9 @@ impl MatrixRoom {
390 line.set_tags(&tags); 385 line.set_tags(&tags);
391 }; 386 };
392 387
388 let mut lines = buffer.lines();
389 let first_line = lines.rfind(predicate);
390
393 if let Some(line) = first_line { 391 if let Some(line) = first_line {
394 modify_line(line, tag.clone(), redact_first_line); 392 modify_line(line, tag.clone(), redact_first_line);
395 } else { 393 } else {