Commit 777e85dc79
Unsigned
Layout: unified · split
src/commands/reply.rs +124 −30
| @@ -17,6 +17,10 @@ use crate::Servers; | |||
| 17 | 17 | ||
| 18 | const EVENT_ID_TAG_PREFIX: &str = "matrix_id_"; | 18 | const EVENT_ID_TAG_PREFIX: &str = "matrix_id_"; |
| 19 | 19 | ||
| 20 | fn message_matches_pattern(message: &str, pattern: &str) -> bool { | ||
| 21 | message.contains(pattern) | ||
| 22 | } | ||
| 23 | |||
| 20 | pub struct ReplyCommand { | 24 | pub struct ReplyCommand { |
| 21 | servers: Servers, | 25 | servers: Servers, |
| 22 | } | 26 | } |
| @@ -25,11 +29,13 @@ impl ReplyCommand { | |||
| 25 | pub fn create(servers: &Servers) -> Result<Command, ()> { | 29 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 26 | let settings = CommandSettings::new("reply") | 30 | let settings = CommandSettings::new("reply") |
| 27 | .description("Reply to a Matrix event in the current room.") | 31 | .description("Reply to a Matrix event in the current room.") |
| 28 | .add_argument("[event-id|index] <message>") | 32 | .add_argument("[event-id|index|/pattern/] <message>") |
| 29 | .arguments_description( | 33 | .arguments_description( |
| 30 | "event-id: The Matrix event ID to reply to. | 34 | "event-id: The Matrix event ID to reply to. |
| 31 | index: 1-based recent event index. 1, 0, or -1 select the latest event; \ | 35 | index: 1-based recent event index. 1, 0, or -1 select the latest event; \ |
| 32 | 2 or -2 select the event before that. | 36 | 2 or -2 select the event before that. |
| 37 | pattern: A case-sensitive substring enclosed in slashes. The most recent \ | ||
| 38 | unredacted message containing it is selected. | ||
| 33 | message: Reply message text. If no event ID or index is given, all text is \ | 39 | message: Reply message text. If no event ID or index is given, all text is \ |
| 34 | sent as a reply to the latest Matrix event in the buffer.", | 40 | sent as a reply to the latest Matrix event in the buffer.", |
| 35 | ); | 41 | ); |
| @@ -79,6 +85,43 @@ impl ReplyCommand { | |||
| 79 | } | 85 | } |
| 80 | } | 86 | } |
| 81 | 87 | ||
| 88 | fn parse_pattern(argument: &str) -> Result<Option<&str>, String> { | ||
| 89 | let Some(pattern) = argument | ||
| 90 | .strip_prefix('/') | ||
| 91 | .and_then(|argument| argument.strip_suffix('/')) | ||
| 92 | else { | ||
| 93 | return Ok(None); | ||
| 94 | }; | ||
| 95 | |||
| 96 | if pattern.is_empty() { | ||
| 97 | Err("The reply pattern cannot be empty.".to_owned()) | ||
| 98 | } else { | ||
| 99 | Ok(Some(pattern)) | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | fn event_id_matching_pattern( | ||
| 104 | buffer: &Buffer, | ||
| 105 | pattern: &str, | ||
| 106 | ) -> Option<OwnedEventId> { | ||
| 107 | buffer.lines().rev().find_map(|line| { | ||
| 108 | let tags = line.tags(); | ||
| 109 | |||
| 110 | if tags.iter().any(|tag| tag.as_ref() == "matrix_redacted") | ||
| 111 | || !message_matches_pattern( | ||
| 112 | &Weechat::remove_color(&line.message()), | ||
| 113 | pattern, | ||
| 114 | ) | ||
| 115 | { | ||
| 116 | return None; | ||
| 117 | } | ||
| 118 | |||
| 119 | tags.iter() | ||
| 120 | .find_map(|tag| tag.as_ref().strip_prefix(EVENT_ID_TAG_PREFIX)) | ||
| 121 | .and_then(|event_id| EventId::parse(event_id).ok()) | ||
| 122 | }) | ||
| 123 | } | ||
| 124 | |||
| 82 | fn parse_arguments( | 125 | fn parse_arguments( |
| 83 | buffer: &Buffer, | 126 | buffer: &Buffer, |
| 84 | arguments: Option<Vec<&str>>, | 127 | arguments: Option<Vec<&str>>, |
| @@ -86,38 +129,51 @@ impl ReplyCommand { | |||
| 86 | let Some(arguments) = | 129 | let Some(arguments) = |
| 87 | arguments.filter(|arguments| !arguments.is_empty()) | 130 | arguments.filter(|arguments| !arguments.is_empty()) |
| 88 | else { | 131 | else { |
| 89 | return Err("Usage: /reply [event-id|index] <message>".to_owned()); | 132 | return Err( |
| 133 | "Usage: /reply [event-id|index|/pattern/] <message>".to_owned() | ||
| 134 | ); | ||
| 90 | }; | 135 | }; |
| 91 | 136 | ||
| 92 | let (event_id, message) = | 137 | let (event_id, message) = if let Some((first, rest)) = |
| 93 | if let Some((first, rest)) = arguments.split_first() { | 138 | arguments.split_first() |
| 94 | if first.starts_with('$') && EventId::parse(*first).is_err() { | 139 | { |
| 95 | return Err(format!("Invalid Matrix event ID: {}", first)); | 140 | if first.starts_with('$') && EventId::parse(*first).is_err() { |
| 96 | } | 141 | return Err(format!("Invalid Matrix event ID: {}", first)); |
| 142 | } | ||
| 97 | 143 | ||
| 98 | if let Ok(event_id) = EventId::parse(*first) { | 144 | if let Ok(event_id) = EventId::parse(*first) { |
| 99 | (event_id, rest.join(" ")) | 145 | (event_id, rest.join(" ")) |
| 100 | } else if let Some(index) = Self::parse_index(first) { | 146 | } else if let Some(index) = Self::parse_index(first) { |
| 101 | let event_id = Self::event_id_at_index(buffer, index) | 147 | let event_id = Self::event_id_at_index(buffer, index) |
| 102 | .ok_or_else(|| { | 148 | .ok_or_else(|| { |
| 103 | format!( | 149 | format!( |
| 104 | "No Matrix event found at reply index {}.", | 150 | "No Matrix event found at reply index {}.", |
| 105 | first | 151 | first |
| 106 | ) | 152 | ) |
| 107 | })?; | 153 | })?; |
| 108 | 154 | ||
| 109 | (event_id, rest.join(" ")) | 155 | (event_id, rest.join(" ")) |
| 110 | } else { | 156 | } else if let Some(pattern) = Self::parse_pattern(first)? { |
| 111 | let event_id = | 157 | let event_id = Self::event_id_matching_pattern(buffer, pattern) |
| 112 | Self::latest_event_id(buffer).ok_or_else(|| { | 158 | .ok_or_else(|| { |
| 113 | "No Matrix event found to reply to.".to_owned() | 159 | format!( |
| 114 | })?; | 160 | "No unredacted Matrix event matches /{}/.", |
| 115 | 161 | pattern | |
| 116 | (event_id, arguments.join(" ")) | 162 | ) |
| 117 | } | 163 | })?; |
| 164 | |||
| 165 | (event_id, rest.join(" ")) | ||
| 118 | } else { | 166 | } else { |
| 119 | unreachable!("empty arguments were filtered above"); | 167 | let event_id = |
| 120 | }; | 168 | Self::latest_event_id(buffer).ok_or_else(|| { |
| 169 | "No Matrix event found to reply to.".to_owned() | ||
| 170 | })?; | ||
| 171 | |||
| 172 | (event_id, arguments.join(" ")) | ||
| 173 | } | ||
| 174 | } else { | ||
| 175 | unreachable!("empty arguments were filtered above"); | ||
| 176 | }; | ||
| 121 | 177 | ||
| 122 | if message.is_empty() { | 178 | if message.is_empty() { |
| 123 | Err("Reply message cannot be empty.".to_owned()) | 179 | Err("Reply message cannot be empty.".to_owned()) |
| @@ -193,7 +249,7 @@ fn reply_content( | |||
| 193 | mod tests { | 249 | mod tests { |
| 194 | use matrix_sdk::ruma::{events::room::message::Relation, owned_event_id}; | 250 | use matrix_sdk::ruma::{events::room::message::Relation, owned_event_id}; |
| 195 | 251 | ||
| 196 | use super::{reply_content, ReplyCommand}; | 252 | use super::{message_matches_pattern, reply_content, ReplyCommand}; |
| 197 | 253 | ||
| 198 | #[test] | 254 | #[test] |
| 199 | fn reply_index_accepts_recent_event_forms() { | 255 | fn reply_index_accepts_recent_event_forms() { |
| @@ -205,6 +261,33 @@ mod tests { | |||
| 205 | assert_eq!(None, ReplyCommand::parse_index("abc")); | 261 | assert_eq!(None, ReplyCommand::parse_index("abc")); |
| 206 | } | 262 | } |
| 207 | 263 | ||
| 264 | #[test] | ||
| 265 | fn reply_pattern_uses_slash_delimiters() { | ||
| 266 | assert_eq!(Ok(Some("needle")), ReplyCommand::parse_pattern("/needle/")); | ||
| 267 | assert_eq!(Ok(None), ReplyCommand::parse_pattern("needle")); | ||
| 268 | assert_eq!(Ok(None), ReplyCommand::parse_pattern("/needle")); | ||
| 269 | } | ||
| 270 | |||
| 271 | #[test] | ||
| 272 | fn reply_pattern_rejects_empty_match() { | ||
| 273 | assert_eq!( | ||
| 274 | Err("The reply pattern cannot be empty.".to_owned()), | ||
| 275 | ReplyCommand::parse_pattern("//") | ||
| 276 | ); | ||
| 277 | } | ||
| 278 | |||
| 279 | #[test] | ||
| 280 | fn reply_pattern_matches_message_substrings() { | ||
| 281 | assert!(message_matches_pattern( | ||
| 282 | "the self-hosted version", | ||
| 283 | "self-hosted" | ||
| 284 | )); | ||
| 285 | assert!(!message_matches_pattern( | ||
| 286 | "the hosted version", | ||
| 287 | "self-hosted" | ||
| 288 | )); | ||
| 289 | } | ||
| 290 | |||
| 208 | #[test] | 291 | #[test] |
| 209 | fn reply_content_sets_reply_relation() { | 292 | fn reply_content_sets_reply_relation() { |
| 210 | let event_id = owned_event_id!("$replyevent:example.org"); | 293 | let event_id = owned_event_id!("$replyevent:example.org"); |
| @@ -265,4 +348,15 @@ mod tests { | |||
| 265 | ); | 348 | ); |
| 266 | assert!(thread.is_falling_back); | 349 | assert!(thread.is_falling_back); |
| 267 | } | 350 | } |
| 351 | |||
| 352 | #[test] | ||
| 353 | fn command_parser_accepts_reply_pattern() { | ||
| 354 | let matches = ReplyCommand::parser() | ||
| 355 | .get_matches_from_safe(vec!["reply", "/needle/", "message"]) | ||
| 356 | .unwrap(); | ||
| 357 | let arguments: Vec<_> = | ||
| 358 | matches.values_of("arguments").unwrap().collect(); | ||
| 359 | |||
| 360 | assert_eq!(vec!["/needle/", "message"], arguments); | ||
| 361 | } | ||
| 268 | } | 362 | } |