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 = [
21252125
21262126[[package]]
21272127name = "strum_macros"
2128version = "0.19.2"
2128version = "0.19.4"
21292129source = "registry+https://github.com/rust-lang/crates.io-index"
2130checksum = "2d2ab682ecdcae7f5f45ae85cd7c1e6c8e68ea42c8a612d47fedf831c037146a"
2130checksum = "e61bb0be289045cb80bfce000512e32d09f8337e54c186725da381377ad1f8d5"
21312131dependencies = [
21322132 "heck",
21332133 "proc-macro2",
@@ -2646,6 +2646,7 @@ dependencies = [
26462646 "futures",
26472647 "indoc",
26482648 "matrix-sdk",
2649 "strum",
26492650 "tokio",
26502651 "tracing",
26512652 "tracing-subscriber",
Cargo.toml +1
@@ -21,6 +21,7 @@ async-trait = "0.1.41"
2121uuid = { version = "0.8.1", features = ["v4"] }
2222indoc = "1.0.2"
2323unicode-segmentation = "1.6.0"
24strum = "0.19.5"
2425chrono = "0.4.19"
2526
2627[dependencies.weechat]
src/config.rs +40 −7
@@ -15,7 +15,12 @@
1515//! loaded so they don't need to be freed manually. The drop implementation of
1616//! the section will do so.
1717
18use crate::{MatrixServer, Servers};
18use std::{
19 cell::{Ref, RefCell, RefMut},
20 rc::Rc,
21};
22
23use strum::EnumVariantNames;
1924use weechat::{
2025 config,
2126 config::{
@@ -25,10 +30,32 @@ use weechat::{
2530 Weechat,
2631};
2732
28use std::{
29 cell::{Ref, RefCell, RefMut},
30 rc::Rc,
31};
33use crate::{MatrixServer, Servers};
34
35#[derive(EnumVariantNames)]
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}
3259
3360config!(
3461 "matrix-rust",
@@ -46,7 +73,13 @@ config!(
4673 the receipt of the message",
4774 // Default value
4875 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 },
5083 },
5184 Section network {
5285 debug_buffer: bool {
@@ -54,7 +87,7 @@ config!(
5487 "Use a separate buffer for debug logs",
5588 // Default value.
5689 false,
57 }
90 },
5891 }
5992);
6093
src/room/mod.rs +21 −23
@@ -71,7 +71,7 @@ use weechat::{
7171};
7272
7373use crate::{
74 config::Config,
74 config::{Config, RedactionStyle},
7575 connection::{Connection, TYPING_NOTICE_TIMEOUT},
7676 render::{Render, RenderedEvent},
7777 PLUGIN_NAME,
@@ -319,16 +319,6 @@ impl MatrixRoom {
319319
320320 let event_id_tag =
321321 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
332322 let tag = Cow::from("matrix_redacted");
333323
334324 let reason = if let Some(r) = &event.content.reason {
@@ -336,7 +326,6 @@ impl MatrixRoom {
336326 } else {
337327 "".to_owned()
338328 };
339
340329 let redaction_message = format!(
341330 "{}<{}Message redacted by: {}{}{}>{}",
342331 Weechat::color("chat_delimiters"),
@@ -347,6 +336,14 @@ impl MatrixRoom {
347336 Weechat::color("reset"),
348337 );
349338
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
350347 let strike_through = |string: Cow<str>| {
351348 Weechat::remove_color(&string)
352349 .graphemes(true)
@@ -355,24 +352,22 @@ impl MatrixRoom {
355352 .join("")
356353 };
357354
358 let redact_first_line = |message: Cow<str>| {
359 if false {
360 redaction_message.clone()
361 } else if false {
355 let redact_first_line = |message: Cow<str>| match redaction_style {
356 RedactionStyle::Delete => redaction_message.clone(),
357 RedactionStyle::Notice => {
362358 format!("{} {}", message, redaction_message)
363 } else {
359 }
360 RedactionStyle::StrikeThrough => {
364361 format!("{} {}", strike_through(message), redaction_message)
365362 }
366363 };
367364
368 let redact_string = |message: Cow<str>| {
369 if false {
370 redaction_message.clone()
371 } else if false {
365 let redact_string = |message: Cow<str>| match redaction_style {
366 RedactionStyle::Delete => redaction_message.clone(),
367 RedactionStyle::Notice => {
372368 format!("{} {}", message, redaction_message)
373 } else {
374 strike_through(message)
375369 }
370 RedactionStyle::StrikeThrough => strike_through(message),
376371 };
377372
378373 fn modify_line<F>(line: BufferLine, tag: Cow<str>, redaction_func: F)
@@ -390,6 +385,9 @@ impl MatrixRoom {
390385 line.set_tags(&tags);
391386 };
392387
388 let mut lines = buffer.lines();
389 let first_line = lines.rfind(predicate);
390
393391 if let Some(line) = first_line {
394392 modify_line(line, tag.clone(), redact_first_line);
395393 } else {