Commit 0a3b576412
Unsigned
Layout: unified · split
src/commands/redact.rs +74 −14
| @@ -19,12 +19,14 @@ impl RedactCommand { | ||
| 19 | 19 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 20 | 20 | let settings = CommandSettings::new("redact") |
| 21 | 21 | .description("Redact a Matrix event in the current room.") |
| 22 | .add_argument("[event-id] [reason...]") | |
| 22 | .add_argument("[event-id|index] [reason...]") | |
| 23 | 23 | .arguments_description( |
| 24 | "event-id: The Matrix event ID to redact. If omitted, the latest \ | |
| 25 | unredacted event in the current buffer is used. | |
| 26 | reason: Optional reason for the redaction. If no event ID is given, all text \ | |
| 27 | is used as the reason for redacting the latest unredacted event.", | |
| 24 | "event-id: The Matrix event ID to redact. | |
| 25 | index: 1-based recent unredacted event index. 1, 0, or -1 select the latest \ | |
| 26 | event; 2 or -2 select the event before that. | |
| 27 | 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 \ | |
| 29 | event.", | |
| 28 | 30 | ); |
| 29 | 31 | |
| 30 | 32 | Command::new( |
| @@ -36,17 +38,40 @@ impl RedactCommand { | ||
| 36 | 38 | } |
| 37 | 39 | |
| 38 | 40 | fn latest_event_id(buffer: &Buffer) -> Option<OwnedEventId> { |
| 39 | buffer.lines().rev().find_map(|line| { | |
| 40 | let tags = line.tags(); | |
| 41 | Self::event_id_at_index(buffer, 1) | |
| 42 | } | |
| 41 | 43 | |
| 42 | if tags.iter().any(|tag| tag.as_ref() == "matrix_redacted") { | |
| 43 | return None; | |
| 44 | } | |
| 44 | fn event_id_at_index( | |
| 45 | buffer: &Buffer, | |
| 46 | index: usize, | |
| 47 | ) -> Option<OwnedEventId> { | |
| 48 | buffer | |
| 49 | .lines() | |
| 50 | .rev() | |
| 51 | .filter_map(|line| { | |
| 52 | let tags = line.tags(); | |
| 53 | ||
| 54 | if tags.iter().any(|tag| tag.as_ref() == "matrix_redacted") { | |
| 55 | return None; | |
| 56 | } | |
| 57 | ||
| 58 | tags.iter() | |
| 59 | .find_map(|tag| { | |
| 60 | tag.as_ref().strip_prefix(EVENT_ID_TAG_PREFIX) | |
| 61 | }) | |
| 62 | .and_then(|event_id| EventId::parse(event_id).ok()) | |
| 63 | }) | |
| 64 | .nth(index.saturating_sub(1)) | |
| 65 | } | |
| 45 | 66 | |
| 46 | tags.iter() | |
| 47 | .find_map(|tag| tag.as_ref().strip_prefix(EVENT_ID_TAG_PREFIX)) | |
| 48 | .and_then(|event_id| EventId::parse(event_id).ok()) | |
| 49 | }) | |
| 67 | fn parse_index(argument: &str) -> Option<usize> { | |
| 68 | let index = argument.parse::<isize>().ok()?; | |
| 69 | ||
| 70 | match index { | |
| 71 | 0 | -1 => Some(1), | |
| 72 | n if n > 0 => Some(n as usize), | |
| 73 | n => n.checked_abs().map(|n| n as usize), | |
| 74 | } | |
| 50 | 75 | } |
| 51 | 76 | |
| 52 | 77 | fn parse_arguments( |
| @@ -77,6 +102,21 @@ impl RedactCommand { | ||
| 77 | 102 | }; |
| 78 | 103 | |
| 79 | 104 | Ok((event_id, reason)) |
| 105 | } else if let Some(index) = Self::parse_index(first) { | |
| 106 | let reason = if rest.is_empty() { | |
| 107 | None | |
| 108 | } else { | |
| 109 | Some(rest.join(" ")) | |
| 110 | }; | |
| 111 | ||
| 112 | Self::event_id_at_index(buffer, index) | |
| 113 | .map(|event_id| (event_id, reason)) | |
| 114 | .ok_or_else(|| { | |
| 115 | format!( | |
| 116 | "No Matrix event found at redaction index {}.", | |
| 117 | first | |
| 118 | ) | |
| 119 | }) | |
| 80 | 120 | } else { |
| 81 | 121 | Self::latest_event_id(buffer) |
| 82 | 122 | .map(|event_id| (event_id, Some(arguments.join(" ")))) |
| @@ -103,6 +143,26 @@ impl RedactCommand { | ||
| 103 | 143 | } |
| 104 | 144 | } |
| 105 | 145 | |
| 146 | #[cfg(test)] | |
| 147 | mod tests { | |
| 148 | use super::RedactCommand; | |
| 149 | ||
| 150 | #[test] | |
| 151 | fn redaction_index_accepts_recent_event_forms() { | |
| 152 | assert_eq!(Some(1), RedactCommand::parse_index("1")); | |
| 153 | assert_eq!(Some(1), RedactCommand::parse_index("0")); | |
| 154 | assert_eq!(Some(1), RedactCommand::parse_index("-1")); | |
| 155 | assert_eq!(Some(2), RedactCommand::parse_index("2")); | |
| 156 | assert_eq!(Some(2), RedactCommand::parse_index("-2")); | |
| 157 | } | |
| 158 | ||
| 159 | #[test] | |
| 160 | fn redaction_index_rejects_non_indices() { | |
| 161 | assert_eq!(None, RedactCommand::parse_index("reason")); | |
| 162 | assert_eq!(None, RedactCommand::parse_index("$event:example.org")); | |
| 163 | } | |
| 164 | } | |
| 165 | ||
| 106 | 166 | impl CommandCallback for RedactCommand { |
| 107 | 167 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { |
| 108 | 168 | let parser = Argparse::new("redact") |