diff --git a/src/requests/indicator.rs b/src/requests/indicator.rs index 770a239..82ff40a 100644 --- a/src/requests/indicator.rs +++ b/src/requests/indicator.rs @@ -15,16 +15,40 @@ pub struct IndicatorContext { pub expires: u16, } +impl Server { + pub fn spawn_indicator_thread(self: &Arc) { + let server = self.clone(); + std::thread::spawn(move || { + loop { + let mut remove = Vec::new(); + for (i, indicator) in server.indicators.lock().unwrap().iter_mut().enumerate() { + indicator.expires -= 1; + if indicator.expires == 0 { + remove.push(i); + } + } + for r in remove { + server.indicators.lock().unwrap().remove(r); + } + } + }); + } +} + crate::logger!(LOGGER "Typing Indicator"); pub fn start_typing(server: &Arc, client: &Client, channel_id: &str) -> crate::Result<()> { - server.broadcast(ServerMessage::Indicator(IndicatorContext { + let indicator = IndicatorContext { indicator: Indicator::Typing { user_id: client.get_uuid().context("Failed to get uuid")?, channel_id: channel_id.to_string(), }, - expires: 5, // 5 secs - })); + expires: 2, // 2 secs + }; + + server.broadcast(ServerMessage::Indicator(indicator.clone())); + + server.indicators.lock().unwrap().push(indicator); Ok(()) } diff --git a/src/server.rs b/src/server.rs index 0f35140..6764fa8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -99,6 +99,10 @@ impl Server { Self::LOGGER.info("Authenticating"); auth::test(self)?; + // Initialize indicators + Self::LOGGER.info("Initializing indicators"); + self.spawn_indicator_thread(); + // Initialize CLI Self::LOGGER.info("Initializing CLI"); cli::start_cli(self.clone(), plugin_loader);