Commit 0a3b576412

0a3b57641206aae25b64cdad44ba26341451c579

parent: 87601f4b82

Unsigned

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

feat: allow /redact to target recent events by index

Layout: unified · split

src/commands/redact.rs +74 −14
@@ -19,12 +19,14 @@ impl RedactCommand {
1919 pub fn create(servers: &Servers) -> Result<Command, ()> {
2020 let settings = CommandSettings::new("redact")
2121 .description("Redact a Matrix event in the current room.")
22 .add_argument("[event-id] [reason...]")
22 .add_argument("[event-id|index] [reason...]")
2323 .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.",
2830 );
2931
3032 Command::new(
@@ -36,17 +38,40 @@ impl RedactCommand {
3638 }
3739
3840 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 }
4143
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 }
4566
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 }
5075 }
5176
5277 fn parse_arguments(
@@ -77,6 +102,21 @@ impl RedactCommand {
77102 };
78103
79104 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 })
80120 } else {
81121 Self::latest_event_id(buffer)
82122 .map(|event_id| (event_id, Some(arguments.join(" "))))
@@ -103,6 +143,26 @@ impl RedactCommand {
103143 }
104144}
105145
146#[cfg(test)]
147mod 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
106166impl CommandCallback for RedactCommand {
107167 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
108168 let parser = Argparse::new("redact")