Commit ef39e6b642
Unsigned
Layout: unified · split
src/commands/redact.rs +87 −2
| @@ -11,6 +11,10 @@ use crate::Servers; | |||
| 11 | 11 | ||
| 12 | const EVENT_ID_TAG_PREFIX: &str = "matrix_id_"; | 12 | const EVENT_ID_TAG_PREFIX: &str = "matrix_id_"; |
| 13 | 13 | ||
| 14 | fn message_matches_pattern(message: &str, pattern: &str) -> bool { | ||
| 15 | message.contains(pattern) | ||
| 16 | } | ||
| 17 | |||
| 14 | pub struct RedactCommand { | 18 | pub struct RedactCommand { |
| 15 | servers: Servers, | 19 | servers: Servers, |
| 16 | } | 20 | } |
| @@ -19,11 +23,13 @@ impl RedactCommand { | |||
| 19 | pub fn create(servers: &Servers) -> Result<Command, ()> { | 23 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 20 | let settings = CommandSettings::new("redact") | 24 | let settings = CommandSettings::new("redact") |
| 21 | .description("Redact a Matrix event in the current room.") | 25 | .description("Redact a Matrix event in the current room.") |
| 22 | .add_argument("[event-id|index] [reason...]") | 26 | .add_argument("[event-id|index|/pattern/] [reason...]") |
| 23 | .arguments_description( | 27 | .arguments_description( |
| 24 | "event-id: The Matrix event ID to redact. | 28 | "event-id: The Matrix event ID to redact. |
| 25 | index: 1-based recent unredacted event index. 1, 0, or -1 select the latest \ | 29 | index: 1-based recent unredacted event index. 1, 0, or -1 select the latest \ |
| 26 | event; 2 or -2 select the event before that. | 30 | 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. | ||
| 27 | reason: Optional reason for the redaction. If no event ID or index is given, \ | 33 | reason: Optional reason for the redaction. If no event ID or index is given, \ |
| 28 | all text is used as the reason for redacting the latest unredacted \ | 34 | all text is used as the reason for redacting the latest unredacted \ |
| 29 | event.", | 35 | event.", |
| @@ -74,6 +80,43 @@ impl RedactCommand { | |||
| 74 | } | 80 | } |
| 75 | } | 81 | } |
| 76 | 82 | ||
| 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 | |||
| 77 | fn parse_arguments( | 120 | fn parse_arguments( |
| 78 | buffer: &Buffer, | 121 | buffer: &Buffer, |
| 79 | arguments: Option<Vec<&str>>, | 122 | arguments: Option<Vec<&str>>, |
| @@ -117,6 +160,18 @@ impl RedactCommand { | |||
| 117 | first | 160 | first |
| 118 | ) | 161 | ) |
| 119 | }) | 162 | }) |
| 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 | }) | ||
| 120 | } else { | 175 | } else { |
| 121 | Self::latest_event_id(buffer) | 176 | Self::latest_event_id(buffer) |
| 122 | .map(|event_id| (event_id, Some(arguments.join(" ")))) | 177 | .map(|event_id| (event_id, Some(arguments.join(" ")))) |
| @@ -145,7 +200,7 @@ impl RedactCommand { | |||
| 145 | 200 | ||
| 146 | #[cfg(test)] | 201 | #[cfg(test)] |
| 147 | mod tests { | 202 | mod tests { |
| 148 | use super::RedactCommand; | 203 | use super::{message_matches_pattern, RedactCommand}; |
| 149 | 204 | ||
| 150 | #[test] | 205 | #[test] |
| 151 | fn redaction_index_accepts_recent_event_forms() { | 206 | fn redaction_index_accepts_recent_event_forms() { |
| @@ -161,6 +216,36 @@ mod tests { | |||
| 161 | assert_eq!(None, RedactCommand::parse_index("reason")); | 216 | assert_eq!(None, RedactCommand::parse_index("reason")); |
| 162 | assert_eq!(None, RedactCommand::parse_index("$event:example.org")); | 217 | assert_eq!(None, RedactCommand::parse_index("$event:example.org")); |
| 163 | } | 218 | } |
| 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 | } | ||
| 164 | } | 249 | } |
| 165 | 250 | ||
| 166 | impl CommandCallback for RedactCommand { | 251 | impl CommandCallback for RedactCommand { |