Commit c45881e085
Unsigned
Layout: unified · split
Cargo.lock +3 −3
| @@ -2203,7 +2203,7 @@ dependencies = [ | |||
| 2203 | [[package]] | 2203 | [[package]] |
| 2204 | name = "weechat" | 2204 | name = "weechat" |
| 2205 | version = "0.1.0" | 2205 | version = "0.1.0" |
| 2206 | source = "git+https://github.com/poljar/rust-weechat#3d154ed91a70c587011fdeb31c619037dd82e501" | 2206 | source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" |
| 2207 | dependencies = [ | 2207 | dependencies = [ |
| 2208 | "async-task", | 2208 | "async-task", |
| 2209 | "async-trait", | 2209 | "async-trait", |
| @@ -2217,7 +2217,7 @@ dependencies = [ | |||
| 2217 | [[package]] | 2217 | [[package]] |
| 2218 | name = "weechat-macro" | 2218 | name = "weechat-macro" |
| 2219 | version = "0.1.0" | 2219 | version = "0.1.0" |
| 2220 | source = "git+https://github.com/poljar/rust-weechat#3d154ed91a70c587011fdeb31c619037dd82e501" | 2220 | source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" |
| 2221 | dependencies = [ | 2221 | dependencies = [ |
| 2222 | "libc", | 2222 | "libc", |
| 2223 | "proc-macro2", | 2223 | "proc-macro2", |
| @@ -2248,7 +2248,7 @@ dependencies = [ | |||
| 2248 | [[package]] | 2248 | [[package]] |
| 2249 | name = "weechat-sys" | 2249 | name = "weechat-sys" |
| 2250 | version = "0.1.0" | 2250 | version = "0.1.0" |
| 2251 | source = "git+https://github.com/poljar/rust-weechat#3d154ed91a70c587011fdeb31c619037dd82e501" | 2251 | source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" |
| 2252 | dependencies = [ | 2252 | dependencies = [ |
| 2253 | "bindgen", | 2253 | "bindgen", |
| 2254 | "libc", | 2254 | "libc", |
src/lib.rs +36 −2
| @@ -16,8 +16,10 @@ use tracing_subscriber; | |||
| 16 | 16 | ||
| 17 | use weechat::{ | 17 | use weechat::{ |
| 18 | buffer::{Buffer, BufferHandle}, | 18 | buffer::{Buffer, BufferHandle}, |
| 19 | hooks::{BarItemCallback, BarItemHandle}, | 19 | hooks::{ |
| 20 | weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, | 20 | BarItemCallback, BarItemHandle, SignalCallback, SignalData, SignalHook, |
| 21 | }, | ||
| 22 | weechat_plugin, ArgsWeechat, ReturnCode, Weechat, WeechatPlugin, | ||
| 21 | }; | 23 | }; |
| 22 | 24 | ||
| 23 | use crate::commands::Commands; | 25 | use crate::commands::Commands; |
| @@ -43,6 +45,32 @@ impl Servers { | |||
| 43 | } | 45 | } |
| 44 | } | 46 | } |
| 45 | 47 | ||
| 48 | impl SignalCallback for Servers { | ||
| 49 | fn callback( | ||
| 50 | &mut self, | ||
| 51 | _: &Weechat, | ||
| 52 | _signal_name: &str, | ||
| 53 | data: Option<SignalData>, | ||
| 54 | ) -> ReturnCode { | ||
| 55 | if let Some(data) = data { | ||
| 56 | if let SignalData::Buffer(buffer) = data { | ||
| 57 | let servers = self.borrow(); | ||
| 58 | |||
| 59 | for server in servers.values() { | ||
| 60 | for room_buffer in server.inner().room_buffers.values() { | ||
| 61 | if buffer == room_buffer.weechat_buffer() { | ||
| 62 | room_buffer.update_typing_notice(); | ||
| 63 | |||
| 64 | return ReturnCode::Ok; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | ReturnCode::Ok | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 46 | struct Matrix { | 74 | struct Matrix { |
| 47 | servers: Servers, | 75 | servers: Servers, |
| 48 | #[used] | 76 | #[used] |
| @@ -51,6 +79,8 @@ struct Matrix { | |||
| 51 | config: ConfigHandle, | 79 | config: ConfigHandle, |
| 52 | #[used] | 80 | #[used] |
| 53 | status_bar: BarItemHandle, | 81 | status_bar: BarItemHandle, |
| 82 | #[used] | ||
| 83 | typing_notice_signal: SignalHook, | ||
| 54 | debug_buffer: RefCell<Option<BufferHandle>>, | 84 | debug_buffer: RefCell<Option<BufferHandle>>, |
| 55 | } | 85 | } |
| 56 | 86 | ||
| @@ -141,12 +171,16 @@ impl WeechatPlugin for Matrix { | |||
| 141 | } | 171 | } |
| 142 | } | 172 | } |
| 143 | 173 | ||
| 174 | let typing = SignalHook::new("input_text_changed", servers.clone()) | ||
| 175 | .expect("Can't create signal hook for the typing notice cb"); | ||
| 176 | |||
| 144 | let plugin = Matrix { | 177 | let plugin = Matrix { |
| 145 | servers: servers.clone(), | 178 | servers: servers.clone(), |
| 146 | commands, | 179 | commands, |
| 147 | config: config.clone(), | 180 | config: config.clone(), |
| 148 | status_bar, | 181 | status_bar, |
| 149 | debug_buffer: RefCell::new(None), | 182 | debug_buffer: RefCell::new(None), |
| 183 | typing_notice_signal: typing, | ||
| 150 | }; | 184 | }; |
| 151 | 185 | ||
| 152 | Weechat::spawn(async move { | 186 | Weechat::spawn(async move { |
src/room_buffer.rs +89 −3
| @@ -35,9 +35,11 @@ use url::Url; | |||
| 35 | 35 | ||
| 36 | use async_trait::async_trait; | 36 | use async_trait::async_trait; |
| 37 | 37 | ||
| 38 | use crate::server::Connection; | 38 | use crate::server::{Connection, TYPING_NOTICE_TIMEOUT}; |
| 39 | use std::cell::{Ref, RefCell, RefMut}; | 39 | use std::cell::{Ref, RefCell, RefMut}; |
| 40 | use std::rc::Rc; | 40 | use std::rc::Rc; |
| 41 | use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 42 | use std::time::Instant; | ||
| 41 | use weechat::buffer::{ | 43 | use weechat::buffer::{ |
| 42 | Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync, | 44 | Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync, |
| 43 | NickSettings, | 45 | NickSettings, |
| @@ -45,6 +47,9 @@ use weechat::buffer::{ | |||
| 45 | use weechat::Weechat; | 47 | use weechat::Weechat; |
| 46 | 48 | ||
| 47 | pub struct RoomBuffer { | 49 | pub struct RoomBuffer { |
| 50 | own_user_id: Rc<UserId>, | ||
| 51 | room_id: Rc<RoomId>, | ||
| 52 | typing_in_flight: Rc<AtomicBool>, | ||
| 48 | inner: MatrixRoom, | 53 | inner: MatrixRoom, |
| 49 | buffer_handle: BufferHandle, | 54 | buffer_handle: BufferHandle, |
| 50 | } | 55 | } |
| @@ -56,7 +61,7 @@ pub struct MatrixRoom { | |||
| 56 | room_id: Rc<RoomId>, | 61 | room_id: Rc<RoomId>, |
| 57 | connection: Rc<RefCell<Option<Connection>>>, | 62 | connection: Rc<RefCell<Option<Connection>>>, |
| 58 | prev_batch: Rc<RefCell<Option<String>>>, | 63 | prev_batch: Rc<RefCell<Option<String>>>, |
| 59 | typing_notice_time: Rc<RefCell<Option<u64>>>, | 64 | typing_notice_time: Rc<RefCell<Option<Instant>>>, |
| 60 | room: Rc<RefCell<Room>>, | 65 | room: Rc<RefCell<Room>>, |
| 61 | printed_before_ack_queue: Rc<RefCell<Vec<String>>>, | 66 | printed_before_ack_queue: Rc<RefCell<Vec<String>>>, |
| 62 | } | 67 | } |
| @@ -120,6 +125,9 @@ impl RoomBuffer { | |||
| 120 | buffer.enable_nicklist(); | 125 | buffer.enable_nicklist(); |
| 121 | 126 | ||
| 122 | RoomBuffer { | 127 | RoomBuffer { |
| 128 | room_id: Rc::new(room_id), | ||
| 129 | own_user_id: Rc::new(own_user_id.to_owned()), | ||
| 130 | typing_in_flight: Rc::new(AtomicBool::from(false)), | ||
| 123 | inner: room, | 131 | inner: room, |
| 124 | buffer_handle, | 132 | buffer_handle, |
| 125 | } | 133 | } |
| @@ -196,11 +204,89 @@ impl RoomBuffer { | |||
| 196 | } | 204 | } |
| 197 | } | 205 | } |
| 198 | 206 | ||
| 199 | pub fn update_buffer_name(&mut self) { | 207 | pub fn update_buffer_name(&self) { |
| 200 | let name = self.calculate_buffer_name(); | 208 | let name = self.calculate_buffer_name(); |
| 201 | self.weechat_buffer().set_name(&name) | 209 | self.weechat_buffer().set_name(&name) |
| 202 | } | 210 | } |
| 203 | 211 | ||
| 212 | /// Send out a typing notice. | ||
| 213 | /// | ||
| 214 | /// This will send out a typing notice or reset one if needed. It will make | ||
| 215 | /// sure that only one typing notice request is in flight at a time. | ||
| 216 | /// | ||
| 217 | /// Typing notices are sent out only if we have more than 4 letters in the | ||
| 218 | /// input and the input isn't a command. | ||
| 219 | /// | ||
| 220 | /// If the input is empty the typing notice is disabled. | ||
| 221 | pub fn update_typing_notice(&self) { | ||
| 222 | // We're in the process of sending out a typing notice, so don't make | ||
| 223 | // the same request twice. | ||
| 224 | if self.typing_in_flight.load(Ordering::Relaxed) { | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | let buffer = self.weechat_buffer(); | ||
| 229 | let input = buffer.input(); | ||
| 230 | |||
| 231 | if input.starts_with("/") && !input.starts_with("//") { | ||
| 232 | // Don't send typing notices for commands. | ||
| 233 | return; | ||
| 234 | } | ||
| 235 | |||
| 236 | let connection = self.inner.connection.clone(); | ||
| 237 | let room_id = self.room_id.clone(); | ||
| 238 | let user_id = self.own_user_id.clone(); | ||
| 239 | let typing_notice_time = self.inner.typing_notice_time.clone(); | ||
| 240 | let typing_in_flight = self.typing_in_flight.clone(); | ||
| 241 | |||
| 242 | let send = async move |typing: bool| { | ||
| 243 | let typing_time = typing_notice_time; | ||
| 244 | |||
| 245 | if let Some(connection) = &*connection.borrow() { | ||
| 246 | let response = connection | ||
| 247 | .send_typing_notice(&*room_id, &*user_id, typing) | ||
| 248 | .await; | ||
| 249 | |||
| 250 | // If we got a successfull response mark when we sent out the | ||
| 251 | // notice so we don't send out another one while this one is | ||
| 252 | // active, or if we disabled the notice mark that no notice is | ||
| 253 | // active. | ||
| 254 | if response.is_ok() { | ||
| 255 | if typing { | ||
| 256 | *typing_time.borrow_mut() = Some(Instant::now()); | ||
| 257 | } else { | ||
| 258 | *typing_time.borrow_mut() = None; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | }; | ||
| 262 | |||
| 263 | typing_in_flight.store(false, Ordering::Relaxed); | ||
| 264 | }; | ||
| 265 | |||
| 266 | let typing_time = self.inner.typing_notice_time.borrow_mut(); | ||
| 267 | |||
| 268 | if input.len() < 4 && typing_time.is_some() { | ||
| 269 | // If we have an active typing notice and our input is short, e.g. | ||
| 270 | // we removed the input set the typing notice to false. | ||
| 271 | self.typing_in_flight.store(true, Ordering::Release); | ||
| 272 | Weechat::spawn(send(false)); | ||
| 273 | } else if input.len() >= 4 { | ||
| 274 | if let Some(typing_time) = &*typing_time { | ||
| 275 | // If we have some valid input, check if the typing notice | ||
| 276 | // expired and send one out if it indeed expired. | ||
| 277 | if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT { | ||
| 278 | self.typing_in_flight.store(true, Ordering::Release); | ||
| 279 | Weechat::spawn(send(true)); | ||
| 280 | } | ||
| 281 | } else { | ||
| 282 | // If we have some valid input and no active typing notice send | ||
| 283 | // one out. | ||
| 284 | self.typing_in_flight.store(true, Ordering::Release); | ||
| 285 | Weechat::spawn(send(true)); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 204 | pub fn handle_membership_event( | 290 | pub fn handle_membership_event( |
| 205 | &mut self, | 291 | &mut self, |
| 206 | event: &MemberEvent, | 292 | event: &MemberEvent, |
src/server.rs +37 −2
| @@ -69,7 +69,10 @@ use matrix_sdk::api::r0::session::login::Response as LoginResponse; | |||
| 69 | 69 | ||
| 70 | use matrix_sdk::{ | 70 | use matrix_sdk::{ |
| 71 | self, | 71 | self, |
| 72 | api::r0::message::create_message_event::Response as RoomSendResponse, | 72 | api::r0::{ |
| 73 | message::create_message_event::Response as RoomSendResponse, | ||
| 74 | typing::create_typing_event::Response as TypingResponse, | ||
| 75 | }, | ||
| 73 | events::{ | 76 | events::{ |
| 74 | collections::all::{RoomEvent, StateEvent}, | 77 | collections::all::{RoomEvent, StateEvent}, |
| 75 | room::message::{MessageEventContent, TextMessageEventContent}, | 78 | room::message::{MessageEventContent, TextMessageEventContent}, |
| @@ -90,6 +93,7 @@ use crate::PLUGIN_NAME; | |||
| 90 | use crate::{config::Config, ConfigHandle}; | 93 | use crate::{config::Config, ConfigHandle}; |
| 91 | 94 | ||
| 92 | const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); | 95 | const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); |
| 96 | pub const TYPING_NOTICE_TIMEOUT: Duration = Duration::from_secs(4); | ||
| 93 | 97 | ||
| 94 | pub enum ClientMessage { | 98 | pub enum ClientMessage { |
| 95 | LoginMessage(LoginResponse), | 99 | LoginMessage(LoginResponse), |
| @@ -162,6 +166,37 @@ impl Connection { | |||
| 162 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | 166 | Err(e) => panic!("Tokio error while sending a message {:?}", e), |
| 163 | } | 167 | } |
| 164 | } | 168 | } |
| 169 | pub async fn send_typing_notice( | ||
| 170 | &self, | ||
| 171 | room_id: &RoomId, | ||
| 172 | user_id: &UserId, | ||
| 173 | typing: bool, | ||
| 174 | ) -> MatrixResult<TypingResponse> { | ||
| 175 | let room_id = room_id.to_owned(); | ||
| 176 | let user_id = user_id.to_owned(); | ||
| 177 | let client = self.client.clone(); | ||
| 178 | |||
| 179 | let handle = self | ||
| 180 | .runtime | ||
| 181 | .spawn(async move { | ||
| 182 | let timeout = if typing { | ||
| 183 | Some(TYPING_NOTICE_TIMEOUT) | ||
| 184 | } else { | ||
| 185 | None | ||
| 186 | }; | ||
| 187 | |||
| 188 | let ret = client | ||
| 189 | .typing_notice(&room_id, &user_id, typing, timeout) | ||
| 190 | .await; | ||
| 191 | ret | ||
| 192 | }) | ||
| 193 | .await; | ||
| 194 | |||
| 195 | match handle { | ||
| 196 | Ok(response) => response, | ||
| 197 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | ||
| 198 | } | ||
| 199 | } | ||
| 165 | } | 200 | } |
| 166 | 201 | ||
| 167 | #[derive(Clone)] | 202 | #[derive(Clone)] |
| @@ -179,7 +214,7 @@ impl std::fmt::Debug for MatrixServer { | |||
| 179 | 214 | ||
| 180 | pub(crate) struct InnerServer { | 215 | pub(crate) struct InnerServer { |
| 181 | server_name: Rc<String>, | 216 | server_name: Rc<String>, |
| 182 | room_buffers: HashMap<RoomId, RoomBuffer>, | 217 | pub room_buffers: HashMap<RoomId, RoomBuffer>, |
| 183 | settings: ServerSettings, | 218 | settings: ServerSettings, |
| 184 | config: ConfigHandle, | 219 | config: ConfigHandle, |
| 185 | client: Option<Client>, | 220 | client: Option<Client>, |