Commit a7999247c5
Unsigned
Layout: unified · split
src/commands/reply.rs +38 −5
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg}; |
| 2 | 2 | use matrix_sdk::ruma::{ |
| 3 | 3 | events::{ |
| 4 | relation::InReplyTo, | |
| 4 | relation::{InReplyTo, Thread}, | |
| 5 | 5 | room::message::{Relation, RoomMessageEventContent}, |
| 6 | 6 | }, |
| 7 | 7 | EventId, OwnedEventId, |
| @@ -138,8 +138,12 @@ impl ReplyCommand { | ||
| 138 | 138 | |
| 139 | 139 | fn reply(&self, buffer: &Buffer, event_id: OwnedEventId, message: String) { |
| 140 | 140 | if let Some(room) = self.servers.find_room(buffer) { |
| 141 | let thread_root = buffer | |
| 142 | .get_localvar("thread_root") | |
| 143 | .and_then(|root| EventId::parse(root.as_ref()).ok()); | |
| 141 | 144 | Weechat::spawn(async move { |
| 142 | room.send_message(reply_content(event_id, message)).await | |
| 145 | room.send_message(reply_content(event_id, message, thread_root)) | |
| 146 | .await | |
| 143 | 147 | }) |
| 144 | 148 | .detach(); |
| 145 | 149 | } else { |
| @@ -173,10 +177,16 @@ impl CommandCallback for ReplyCommand { | ||
| 173 | 177 | fn reply_content( |
| 174 | 178 | event_id: OwnedEventId, |
| 175 | 179 | message: String, |
| 180 | thread_root: Option<OwnedEventId>, | |
| 176 | 181 | ) -> RoomMessageEventContent { |
| 177 | 182 | let mut content = RoomMessageEventContent::text_plain(message); |
| 178 | content.relates_to = Some(Relation::Reply { | |
| 179 | in_reply_to: InReplyTo::new(event_id), | |
| 183 | content.relates_to = Some(match thread_root { | |
| 184 | Some(thread_root) => { | |
| 185 | Relation::Thread(Thread::plain(thread_root, event_id)) | |
| 186 | } | |
| 187 | None => Relation::Reply { | |
| 188 | in_reply_to: InReplyTo::new(event_id), | |
| 189 | }, | |
| 180 | 190 | }); |
| 181 | 191 | content |
| 182 | 192 | } |
| @@ -200,7 +210,8 @@ mod tests { | ||
| 200 | 210 | #[test] |
| 201 | 211 | fn reply_content_sets_reply_relation() { |
| 202 | 212 | let event_id = owned_event_id!("$replyevent:example.org"); |
| 203 | let content = reply_content(event_id.clone(), "Thanks".to_owned()); | |
| 213 | let content = | |
| 214 | reply_content(event_id.clone(), "Thanks".to_owned(), None); | |
| 204 | 215 | |
| 205 | 216 | let Some(Relation::Reply { in_reply_to }) = content.relates_to else { |
| 206 | 217 | panic!("reply command must create a Matrix reply relation"); |
| @@ -234,4 +245,26 @@ mod tests { | ||
| 234 | 245 | |
| 235 | 246 | assert_eq!(vec!["-2", "message"], arguments); |
| 236 | 247 | } |
| 248 | ||
| 249 | #[test] | |
| 250 | fn reply_content_keeps_thread_relation_in_thread_buffer() { | |
| 251 | let event_id = owned_event_id!("$replyevent:example.org"); | |
| 252 | let thread_root = owned_event_id!("$threadroot:example.org"); | |
| 253 | let content = reply_content( | |
| 254 | event_id.clone(), | |
| 255 | "Thanks".to_owned(), | |
| 256 | Some(thread_root.clone()), | |
| 257 | ); | |
| 258 | ||
| 259 | let Some(Relation::Thread(thread)) = content.relates_to else { | |
| 260 | panic!("reply from a thread buffer must stay in that thread"); | |
| 261 | }; | |
| 262 | ||
| 263 | assert_eq!(thread_root, thread.event_id); | |
| 264 | assert_eq!( | |
| 265 | event_id, | |
| 266 | thread.in_reply_to.expect("fallback reply target").event_id | |
| 267 | ); | |
| 268 | assert!(thread.is_falling_back); | |
| 269 | } | |
| 237 | 270 | } |