Commit ef39e6b642

ef39e6b6423aba12a239587140a53abe520e720a

parent: 5e2dde414b

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-22 10:51 UTC
committer: <strk@kbt.io>

feat: select redactions by message pattern

Layout: unified · split

src/commands/redact.rs +87 −2
@@ -11,6 +11,10 @@ use crate::Servers;
1111
1212const EVENT_ID_TAG_PREFIX: &str = "matrix_id_";
1313
14fn message_matches_pattern(message: &str, pattern: &str) -> bool {
15 message.contains(pattern)
16}
17
1418pub struct RedactCommand {
1519 servers: Servers,
1620}
@@ -19,11 +23,13 @@ impl RedactCommand {
1923 pub fn create(servers: &Servers) -> Result<Command, ()> {
2024 let settings = CommandSettings::new("redact")
2125 .description("Redact a Matrix event in the current room.")
22 .add_argument("[event-id|index] [reason...]")
26 .add_argument("[event-id|index|/pattern/] [reason...]")
2327 .arguments_description(
2428 "event-id: The Matrix event ID to redact.
2529 index: 1-based recent unredacted event index. 1, 0, or -1 select the latest \
2630 event; 2 or -2 select the event before that.
31 pattern: A case-sensitive substring enclosed in slashes. The most recent \
32 unredacted message containing it is selected.
2733 reason: Optional reason for the redaction. If no event ID or index is given, \
2834 all text is used as the reason for redacting the latest unredacted \
2935 event.",
@@ -74,6 +80,43 @@ impl RedactCommand {
7480 }
7581 }
7682
83 fn parse_pattern(argument: &str) -> Result<Option<&str>, String> {
84 let Some(pattern) = argument
85 .strip_prefix('/')
86 .and_then(|argument| argument.strip_suffix('/'))
87 else {
88 return Ok(None);
89 };
90
91 if pattern.is_empty() {
92 Err("The redaction pattern cannot be empty.".to_owned())
93 } else {
94 Ok(Some(pattern))
95 }
96 }
97
98 fn event_id_matching_pattern(
99 buffer: &Buffer,
100 pattern: &str,
101 ) -> Option<OwnedEventId> {
102 buffer.lines().rev().find_map(|line| {
103 let tags = line.tags();
104
105 if tags.iter().any(|tag| tag.as_ref() == "matrix_redacted")
106 || !message_matches_pattern(
107 &Weechat::remove_color(&line.message()),
108 pattern,
109 )
110 {
111 return None;
112 }
113
114 tags.iter()
115 .find_map(|tag| tag.as_ref().strip_prefix(EVENT_ID_TAG_PREFIX))
116 .and_then(|event_id| EventId::parse(event_id).ok())
117 })
118 }
119
77120 fn parse_arguments(
78121 buffer: &Buffer,
79122 arguments: Option<Vec<&str>>,
@@ -117,6 +160,18 @@ impl RedactCommand {
117160 first
118161 )
119162 })
163 } else if let Some(pattern) = Self::parse_pattern(first)? {
164 let reason = if rest.is_empty() {
165 None
166 } else {
167 Some(rest.join(" "))
168 };
169
170 Self::event_id_matching_pattern(buffer, pattern)
171 .map(|event_id| (event_id, reason))
172 .ok_or_else(|| {
173 format!("No unredacted Matrix event matches /{}/.", pattern)
174 })
120175 } else {
121176 Self::latest_event_id(buffer)
122177 .map(|event_id| (event_id, Some(arguments.join(" "))))
@@ -145,7 +200,7 @@ impl RedactCommand {
145200
146201#[cfg(test)]
147202mod tests {
148 use super::RedactCommand;
203 use super::{message_matches_pattern, RedactCommand};
149204
150205 #[test]
151206 fn redaction_index_accepts_recent_event_forms() {
@@ -161,6 +216,36 @@ mod tests {
161216 assert_eq!(None, RedactCommand::parse_index("reason"));
162217 assert_eq!(None, RedactCommand::parse_index("$event:example.org"));
163218 }
219
220 #[test]
221 fn redaction_pattern_uses_slash_delimiters() {
222 assert_eq!(
223 Ok(Some("needle")),
224 RedactCommand::parse_pattern("/needle/")
225 );
226 assert_eq!(Ok(None), RedactCommand::parse_pattern("needle"));
227 assert_eq!(Ok(None), RedactCommand::parse_pattern("/needle"));
228 }
229
230 #[test]
231 fn redaction_pattern_rejects_empty_match() {
232 assert_eq!(
233 Err("The redaction pattern cannot be empty.".to_owned()),
234 RedactCommand::parse_pattern("//")
235 );
236 }
237
238 #[test]
239 fn redaction_pattern_matches_message_substrings() {
240 assert!(message_matches_pattern(
241 "the self-hosted version",
242 "self-hosted"
243 ));
244 assert!(!message_matches_pattern(
245 "the hosted version",
246 "self-hosted"
247 ));
248 }
164249}
165250
166251impl CommandCallback for RedactCommand {