Commit e24322a553
e24322a55367a01668700b8fc04e0cfcec7b7183
parent: 71960d5ded
Unsigned
Darafei Praliaskouski <me@komzpa.net> · 2026-09-03 07:48 UTC
committer: <strk@kbt.io>
fix: keep /say in Matrix rooms
Layout: unified · split
src/commands/mod.rs
+4
| @@ -28,6 +28,7 @@ mod page_up; |
| 28 | mod part; |
28 | mod part; |
| 29 | mod redact; |
29 | mod redact; |
| 30 | mod reply; |
30 | mod reply; |
| |
31 | mod say; |
| 31 | mod topic; |
32 | mod topic; |
| 32 | mod upload; |
33 | mod upload; |
| 33 | mod verification; |
34 | mod verification; |
| @@ -56,6 +57,7 @@ use page_up::PageUpCommand; |
| 56 | use part::PartCommand; |
57 | use part::PartCommand; |
| 57 | use redact::RedactCommand; |
58 | use redact::RedactCommand; |
| 58 | use reply::ReplyCommand; |
59 | use reply::ReplyCommand; |
| |
60 | use say::SayCommand; |
| 59 | use topic::TopicCommand; |
61 | use topic::TopicCommand; |
| 60 | use upload::UploadCommand; |
62 | use upload::UploadCommand; |
| 61 | use verify::VerifyCommand; |
63 | use verify::VerifyCommand; |
| @@ -74,6 +76,7 @@ pub struct Commands { |
| 74 | _page_up: CommandRun, |
76 | _page_up: CommandRun, |
| 75 | _redact: Command, |
77 | _redact: Command, |
| 76 | _reply: Command, |
78 | _reply: Command, |
| |
79 | _say: CommandRun, |
| 77 | _topic: Command, |
80 | _topic: Command, |
| 78 | _verification: Command, |
81 | _verification: Command, |
| 79 | _buffer_clear: CommandRun, |
82 | _buffer_clear: CommandRun, |
| @@ -108,6 +111,7 @@ impl Commands { |
| 108 | _page_up: PageUpCommand::create(servers)?, |
111 | _page_up: PageUpCommand::create(servers)?, |
| 109 | _redact: RedactCommand::create(servers)?, |
112 | _redact: RedactCommand::create(servers)?, |
| 110 | _reply: ReplyCommand::create(servers)?, |
113 | _reply: ReplyCommand::create(servers)?, |
| |
114 | _say: SayCommand::create(servers)?, |
| 111 | _topic: TopicCommand::create(servers)?, |
115 | _topic: TopicCommand::create(servers)?, |
| 112 | _verification: VerificationCommand::create(servers)?, |
116 | _verification: VerificationCommand::create(servers)?, |
| 113 | _buffer_clear: BufferClearCommand::create(servers)?, |
117 | _buffer_clear: BufferClearCommand::create(servers)?, |
src/commands/say.rs
added
+68
| @@ -0,0 +1,68 @@ |
| |
1 | use std::borrow::Cow; |
| |
2 | |
| |
3 | use weechat::{ |
| |
4 | buffer::Buffer, |
| |
5 | hooks::{CommandRun, CommandRunCallback}, |
| |
6 | ReturnCode, Weechat, |
| |
7 | }; |
| |
8 | |
| |
9 | use crate::Servers; |
| |
10 | |
| |
11 | pub struct SayCommand { |
| |
12 | servers: Servers, |
| |
13 | } |
| |
14 | |
| |
15 | fn say_body(command: &str) -> Option<&str> { |
| |
16 | command.strip_prefix("/say ").map(str::trim_start) |
| |
17 | } |
| |
18 | |
| |
19 | impl SayCommand { |
| |
20 | pub fn create(servers: &Servers) -> Result<CommandRun, ()> { |
| |
21 | CommandRun::new( |
| |
22 | "/say *", |
| |
23 | SayCommand { |
| |
24 | servers: servers.clone(), |
| |
25 | }, |
| |
26 | ) |
| |
27 | } |
| |
28 | } |
| |
29 | |
| |
30 | impl CommandRunCallback for SayCommand { |
| |
31 | fn callback( |
| |
32 | &mut self, |
| |
33 | _: &Weechat, |
| |
34 | buffer: &Buffer, |
| |
35 | command: Cow<str>, |
| |
36 | ) -> ReturnCode { |
| |
37 | let Some(room) = self.servers.find_room(buffer) else { |
| |
38 | return ReturnCode::Ok; |
| |
39 | }; |
| |
40 | |
| |
41 | let Some(body) = say_body(&command) else { |
| |
42 | return ReturnCode::Ok; |
| |
43 | }; |
| |
44 | |
| |
45 | let content = room.text_message_content(buffer, body.to_owned()); |
| |
46 | Weechat::spawn(async move { room.send_message(content).await }) |
| |
47 | .detach(); |
| |
48 | |
| |
49 | ReturnCode::OkEat |
| |
50 | } |
| |
51 | } |
| |
52 | |
| |
53 | #[cfg(test)] |
| |
54 | mod tests { |
| |
55 | use super::say_body; |
| |
56 | |
| |
57 | #[test] |
| |
58 | fn say_body_preserves_message_text() { |
| |
59 | assert_eq!(say_body("/say hello Matrix"), Some("hello Matrix")); |
| |
60 | assert_eq!(say_body("/say leading spaces"), Some("leading spaces")); |
| |
61 | } |
| |
62 | |
| |
63 | #[test] |
| |
64 | fn say_body_excludes_other_commands() { |
| |
65 | assert_eq!(say_body("/msg hello"), None); |
| |
66 | assert_eq!(say_body("hello"), None); |
| |
67 | } |
| |
68 | } |
src/room/mod.rs
+18 −20
| @@ -808,42 +808,40 @@ impl BufferInputCallbackAsync for MatrixRoom { |
| 808 | return; |
808 | return; |
| 809 | } |
809 | } |
| 810 | |
810 | |
| 811 | let thread_root = buffer |
811 | if let Ok(buffer) = buffer.upgrade() { |
| 812 | .upgrade() |
812 | let content = self.text_message_content(&buffer, input); |
| 813 | .ok() |
813 | self.send_message(content).await; |
| 814 | .and_then(|buffer| thread_root_from_buffer(&buffer)); |
814 | } |
| |
815 | } |
| |
816 | } |
| |
817 | |
| |
818 | impl MatrixRoom { |
| |
819 | pub(crate) fn text_message_content( |
| |
820 | &self, |
| |
821 | buffer: &Buffer, |
| |
822 | input: String, |
| |
823 | ) -> RoomMessageEventContent { |
| |
824 | let thread_root = thread_root_from_buffer(buffer); |
| 815 | let latest_thread_event = thread_root.as_ref().and_then(|root| { |
825 | let latest_thread_event = thread_root.as_ref().and_then(|root| { |
| 816 | self.latest_thread_event_ids.borrow().get(root).cloned() |
826 | self.latest_thread_event_ids.borrow().get(root).cloned() |
| 817 | }); |
827 | }); |
| 818 | let content = make_text_message_content( |
828 | |
| |
829 | make_text_message_content( |
| 819 | input, |
830 | input, |
| 820 | self.config.borrow().input().markdown_input(), |
831 | self.config.borrow().input().markdown_input(), |
| 821 | thread_root, |
832 | thread_root, |
| 822 | latest_thread_event, |
833 | latest_thread_event, |
| 823 | ); |
834 | ) |
| 824 | |
| |
| 825 | self.send_message(content).await; |
| |
| 826 | } |
835 | } |
| 827 | } |
| |
| 828 | |
836 | |
| 829 | impl MatrixRoom { |
| |
| 830 | pub(crate) fn mention_message_content( |
837 | pub(crate) fn mention_message_content( |
| 831 | &self, |
838 | &self, |
| 832 | buffer: &Buffer, |
839 | buffer: &Buffer, |
| 833 | input: String, |
840 | input: String, |
| 834 | mentioned_user_ids: Vec<OwnedUserId>, |
841 | mentioned_user_ids: Vec<OwnedUserId>, |
| 835 | ) -> RoomMessageEventContent { |
842 | ) -> RoomMessageEventContent { |
| 836 | let thread_root = thread_root_from_buffer(buffer); |
| |
| 837 | let latest_thread_event = thread_root.as_ref().and_then(|root| { |
| |
| 838 | self.latest_thread_event_ids.borrow().get(root).cloned() |
| |
| 839 | }); |
| |
| 840 | with_mentions( |
843 | with_mentions( |
| 841 | make_text_message_content( |
844 | self.text_message_content(buffer, input), |
| 842 | input, |
| |
| 843 | self.config.borrow().input().markdown_input(), |
| |
| 844 | thread_root, |
| |
| 845 | latest_thread_event, |
| |
| 846 | ), |
| |
| 847 | mentioned_user_ids, |
845 | mentioned_user_ids, |
| 848 | ) |
846 | ) |
| 849 | } |
847 | } |