Commit c7ffbb7162
Unsigned
Layout: unified · split
src/commands/me.rs +36 −11
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | use std::borrow::Cow; |
| 2 | 2 | |
| 3 | use matrix_sdk::ruma::events::room::message::RoomMessageEventContent; | |
| 4 | 3 | use weechat::{ |
| 5 | 4 | buffer::Buffer, |
| 6 | 5 | hooks::{CommandRun, CommandRunCallback}, |
| @@ -13,10 +12,16 @@ pub struct MeCommand { | ||
| 13 | 12 | servers: Servers, |
| 14 | 13 | } |
| 15 | 14 | |
| 15 | const ME_COMMAND_PATTERN: &str = "/me *"; | |
| 16 | ||
| 17 | fn action_body(command: &str) -> Option<&str> { | |
| 18 | command.strip_prefix("/me ") | |
| 19 | } | |
| 20 | ||
| 16 | 21 | impl MeCommand { |
| 17 | 22 | pub fn create(servers: &Servers) -> Result<CommandRun, ()> { |
| 18 | 23 | CommandRun::new( |
| 19 | "/me", | |
| 24 | ME_COMMAND_PATTERN, | |
| 20 | 25 | MeCommand { |
| 21 | 26 | servers: servers.clone(), |
| 22 | 27 | }, |
| @@ -31,14 +36,34 @@ impl CommandRunCallback for MeCommand { | ||
| 31 | 36 | buffer: &Buffer, |
| 32 | 37 | cmd: Cow<str>, |
| 33 | 38 | ) -> ReturnCode { |
| 34 | if let Some(room) = self.servers.find_room(buffer) { | |
| 35 | self.servers.runtime().block_on(room.send_message( | |
| 36 | RoomMessageEventContent::emote_plain( | |
| 37 | cmd.strip_prefix("/me ").map(|s| s.to_string()).unwrap(), | |
| 38 | ), | |
| 39 | )); | |
| 40 | } | |
| 41 | ||
| 42 | ReturnCode::Ok | |
| 39 | let Some(room) = self.servers.find_room(buffer) else { | |
| 40 | return ReturnCode::Ok; | |
| 41 | }; | |
| 42 | ||
| 43 | let Some(body) = action_body(&cmd) else { | |
| 44 | return ReturnCode::Ok; | |
| 45 | }; | |
| 46 | ||
| 47 | self.servers | |
| 48 | .runtime() | |
| 49 | .block_on(room.send_emote(buffer, body.to_owned())); | |
| 50 | ||
| 51 | ReturnCode::OkEat | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | #[cfg(test)] | |
| 56 | mod tests { | |
| 57 | use super::{action_body, ME_COMMAND_PATTERN}; | |
| 58 | ||
| 59 | #[test] | |
| 60 | fn me_hook_matches_action_arguments_before_command_resolution() { | |
| 61 | assert_eq!(ME_COMMAND_PATTERN, "/me *"); | |
| 62 | } | |
| 63 | ||
| 64 | #[test] | |
| 65 | fn me_action_body_excludes_other_commands() { | |
| 66 | assert_eq!(action_body("/me waves"), Some("waves")); | |
| 67 | assert_eq!(action_body("/message waves"), None); | |
| 43 | 68 | } |
| 44 | 69 | } |
src/room/mod.rs +56
| @@ -291,6 +291,26 @@ fn make_text_message_content( | ||
| 291 | 291 | content |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | fn make_emote_message_content( | |
| 295 | body: String, | |
| 296 | thread_root: Option<OwnedEventId>, | |
| 297 | latest_thread_event: Option<OwnedEventId>, | |
| 298 | ) -> RoomMessageEventContent { | |
| 299 | let mut content = RoomMessageEventContent::emote_plain(body); | |
| 300 | ||
| 301 | if let Some(thread_root) = thread_root { | |
| 302 | let latest_thread_event = | |
| 303 | latest_thread_event.unwrap_or_else(|| thread_root.clone()); | |
| 304 | ||
| 305 | content.relates_to = Some(Relation::Thread(Thread::plain( | |
| 306 | thread_root, | |
| 307 | latest_thread_event, | |
| 308 | ))); | |
| 309 | } | |
| 310 | ||
| 311 | content | |
| 312 | } | |
| 313 | ||
| 294 | 314 | fn thread_root_from_buffer(buffer: &Buffer) -> Option<OwnedEventId> { |
| 295 | 315 | buffer |
| 296 | 316 | .get_localvar("thread_root") |
| @@ -540,6 +560,17 @@ impl MatrixRoom { | ||
| 540 | 560 | self.buffer.owns_buffer(buffer) |
| 541 | 561 | } |
| 542 | 562 | |
| 563 | pub async fn send_emote(&self, buffer: &Buffer<'_>, body: String) { | |
| 564 | let thread_root = thread_root_from_buffer(buffer); | |
| 565 | let latest_thread_event = thread_root.as_ref().and_then(|root| { | |
| 566 | self.latest_thread_event_ids.borrow().get(root).cloned() | |
| 567 | }); | |
| 568 | let content = | |
| 569 | make_emote_message_content(body, thread_root, latest_thread_event); | |
| 570 | ||
| 571 | self.send_message(content).await; | |
| 572 | } | |
| 573 | ||
| 543 | 574 | pub fn close_thread_buffer(&self, buffer: &Buffer) -> bool { |
| 544 | 575 | if self.buffer.remove_thread_buffer_for_buffer(buffer) { |
| 545 | 576 | buffer.close(); |
| @@ -1810,6 +1841,31 @@ mod tests { | ||
| 1810 | 1841 | assert!(thread.is_falling_back); |
| 1811 | 1842 | } |
| 1812 | 1843 | |
| 1844 | #[test] | |
| 1845 | fn thread_buffer_emote_sends_thread_relation() { | |
| 1846 | let content = make_emote_message_content( | |
| 1847 | "waves".to_owned(), | |
| 1848 | Some(EventId::parse("$thread-root:example.org").unwrap()), | |
| 1849 | Some(EventId::parse("$latest-thread-event:example.org").unwrap()), | |
| 1850 | ); | |
| 1851 | ||
| 1852 | assert!(matches!(content.msgtype, MessageType::Emote(_))); | |
| 1853 | ||
| 1854 | let Some(Relation::Thread(thread)) = content.relates_to else { | |
| 1855 | panic!("thread buffer emote must send an m.thread relation"); | |
| 1856 | }; | |
| 1857 | ||
| 1858 | assert_eq!( | |
| 1859 | thread.event_id, | |
| 1860 | EventId::parse("$thread-root:example.org").unwrap() | |
| 1861 | ); | |
| 1862 | assert_eq!( | |
| 1863 | thread.in_reply_to.expect("fallback target").event_id, | |
| 1864 | EventId::parse("$latest-thread-event:example.org").unwrap() | |
| 1865 | ); | |
| 1866 | assert!(thread.is_falling_back); | |
| 1867 | } | |
| 1868 | ||
| 1813 | 1869 | #[test] |
| 1814 | 1870 | fn plain_event_can_seed_an_already_open_thread_buffer() { |
| 1815 | 1871 | let event_id = EventId::parse("$thread-root:example.org").unwrap(); |