Commit c45881e085
Unsigned
Layout: unified · split
Cargo.lock +3 −3
| @@ -2203,7 +2203,7 @@ dependencies = [ | ||
| 2203 | 2203 | [[package]] |
| 2204 | 2204 | name = "weechat" |
| 2205 | 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 | 2207 | dependencies = [ |
| 2208 | 2208 | "async-task", |
| 2209 | 2209 | "async-trait", |
| @@ -2217,7 +2217,7 @@ dependencies = [ | ||
| 2217 | 2217 | [[package]] |
| 2218 | 2218 | name = "weechat-macro" |
| 2219 | 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 | 2221 | dependencies = [ |
| 2222 | 2222 | "libc", |
| 2223 | 2223 | "proc-macro2", |
| @@ -2248,7 +2248,7 @@ dependencies = [ | ||
| 2248 | 2248 | [[package]] |
| 2249 | 2249 | name = "weechat-sys" |
| 2250 | 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 | 2252 | dependencies = [ |
| 2253 | 2253 | "bindgen", |
| 2254 | 2254 | "libc", |
src/lib.rs +36 −2
| @@ -16,8 +16,10 @@ use tracing_subscriber; | ||
| 16 | 16 | |
| 17 | 17 | use weechat::{ |
| 18 | 18 | buffer::{Buffer, BufferHandle}, |
| 19 | hooks::{BarItemCallback, BarItemHandle}, | |
| 20 | weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, | |
| 19 | hooks::{ | |
| 20 | BarItemCallback, BarItemHandle, SignalCallback, SignalData, SignalHook, | |
| 21 | }, | |
| 22 | weechat_plugin, ArgsWeechat, ReturnCode, Weechat, WeechatPlugin, | |
| 21 | 23 | }; |
| 22 | 24 | |
| 23 | 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 | 74 | struct Matrix { |
| 47 | 75 | servers: Servers, |
| 48 | 76 | #[used] |
| @@ -51,6 +79,8 @@ struct Matrix { | ||
| 51 | 79 | config: ConfigHandle, |
| 52 | 80 | #[used] |
| 53 | 81 | status_bar: BarItemHandle, |
| 82 | #[used] | |
| 83 | typing_notice_signal: SignalHook, | |
| 54 | 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 | 177 | let plugin = Matrix { |
| 145 | 178 | servers: servers.clone(), |
| 146 | 179 | commands, |
| 147 | 180 | config: config.clone(), |
| 148 | 181 | status_bar, |
| 149 | 182 | debug_buffer: RefCell::new(None), |
| 183 | typing_notice_signal: typing, | |
| 150 | 184 | }; |
| 151 | 185 | |
| 152 | 186 | Weechat::spawn(async move { |
src/room_buffer.rs +89 −3
| @@ -35,9 +35,11 @@ use url::Url; | ||
| 35 | 35 | |
| 36 | 36 | use async_trait::async_trait; |
| 37 | 37 | |
| 38 | use crate::server::Connection; | |
| 38 | use crate::server::{Connection, TYPING_NOTICE_TIMEOUT}; | |
| 39 | 39 | use std::cell::{Ref, RefCell, RefMut}; |
| 40 | 40 | use std::rc::Rc; |
| 41 | use std::sync::atomic::{AtomicBool, Ordering}; | |
| 42 | use std::time::Instant; | |
| 41 | 43 | use weechat::buffer::{ |
| 42 | 44 | Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync, |
| 43 | 45 | NickSettings, |
| @@ -45,6 +47,9 @@ use weechat::buffer::{ | ||
| 45 | 47 | use weechat::Weechat; |
| 46 | 48 | |
| 47 | 49 | pub struct RoomBuffer { |
| 50 | own_user_id: Rc<UserId>, | |
| 51 | room_id: Rc<RoomId>, | |
| 52 | typing_in_flight: Rc<AtomicBool>, | |
| 48 | 53 | inner: MatrixRoom, |
| 49 | 54 | buffer_handle: BufferHandle, |
| 50 | 55 | } |
| @@ -56,7 +61,7 @@ pub struct MatrixRoom { | ||
| 56 | 61 | room_id: Rc<RoomId>, |
| 57 | 62 | connection: Rc<RefCell<Option<Connection>>>, |
| 58 | 63 | prev_batch: Rc<RefCell<Option<String>>>, |
| 59 | typing_notice_time: Rc<RefCell<Option<u64>>>, | |
| 64 | typing_notice_time: Rc<RefCell<Option<Instant>>>, | |
| 60 | 65 | room: Rc<RefCell<Room>>, |
| 61 | 66 | printed_before_ack_queue: Rc<RefCell<Vec<String>>>, |
| 62 | 67 | } |
| @@ -120,6 +125,9 @@ impl RoomBuffer { | ||
| 120 | 125 | buffer.enable_nicklist(); |
| 121 | 126 | |
| 122 | 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 | 131 | inner: room, |
| 124 | 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 | 208 | let name = self.calculate_buffer_name(); |
| 201 | 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 | 290 | pub fn handle_membership_event( |
| 205 | 291 | &mut self, |
| 206 | 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 | 70 | use matrix_sdk::{ |
| 71 | 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 | 76 | events::{ |
| 74 | 77 | collections::all::{RoomEvent, StateEvent}, |
| 75 | 78 | room::message::{MessageEventContent, TextMessageEventContent}, |
| @@ -90,6 +93,7 @@ use crate::PLUGIN_NAME; | ||
| 90 | 93 | use crate::{config::Config, ConfigHandle}; |
| 91 | 94 | |
| 92 | 95 | const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); |
| 96 | pub const TYPING_NOTICE_TIMEOUT: Duration = Duration::from_secs(4); | |
| 93 | 97 | |
| 94 | 98 | pub enum ClientMessage { |
| 95 | 99 | LoginMessage(LoginResponse), |
| @@ -162,6 +166,37 @@ impl Connection { | ||
| 162 | 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 | 202 | #[derive(Clone)] |
| @@ -179,7 +214,7 @@ impl std::fmt::Debug for MatrixServer { | ||
| 179 | 214 | |
| 180 | 215 | pub(crate) struct InnerServer { |
| 181 | 216 | server_name: Rc<String>, |
| 182 | room_buffers: HashMap<RoomId, RoomBuffer>, | |
| 217 | pub room_buffers: HashMap<RoomId, RoomBuffer>, | |
| 183 | 218 | settings: ServerSettings, |
| 184 | 219 | config: ConfigHandle, |
| 185 | 220 | client: Option<Client>, |