From 6088b0836f08878c00e008a39a2a21a4a15c9e5f Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 8 Apr 2021 15:45:20 -0700 Subject: [PATCH] + Send message callback now implemented + channel switcher works as well, although it relies on /lc being ran prior to its invocation --- tui/src/cache.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/tui/src/cache.rs b/tui/src/cache.rs index 2632eee..1f90271 100644 --- a/tui/src/cache.rs +++ b/tui/src/cache.rs @@ -65,12 +65,26 @@ impl Cache { impl Cache { pub async fn switch_channel(&mut self, id: u64) -> Command { - if let Some(host) = self.active_server.as_ref() { - Command::Failure(format!("Not implemented yet!")) + if let None = self.active_server { + return Command::Failure("No active server set!".into()) + } + // find the channel meta data + let mut msg = String::new(); + for (_url, cache) in &self.servers { + if let Some(channel_cache) = cache.channels.get(&id) { + let meta = channel_cache.meta.clone(); + msg = format!("Switched to channel: [{}]: {}", meta.id, meta.name); + self.active_text = Some(meta); + break; + } + } + if msg.len() == 0 { + Command::Failure(format!("Channel not found in cache try /lc to refresh cache")) } else { - Command::Failure(format!("No server selected yet")) + Command::Text(msg) } } + pub async fn switch_server(&mut self, host: &str) -> Command { // TODO: implement some better fuzzy finding for better ergonomics // Reallt though the ux is still pretty bad here @@ -111,12 +125,20 @@ impl Cache { } } - pub async fn send_message(&mut self, id: &str) -> Command { - if let Some(server) = self.active_server.as_ref() { - Command::Failure(format!("Not implemented yet!")) - } else { - Command::Failure(format!("No active server+channel to send message to")) + pub async fn send_message(&self, msg: &str) -> Command { + if let Some(channel) = &self.active_text { + let config = self.active_server.as_ref().unwrap(); + let chan = channel.id; + let uid = config.user.id; + let jwt = config.user.jwt.as_ref().unwrap(); + let url = config.server.url.as_str(); + + return match net::send_text(url, uid, jwt, chan).await { + Ok(_) => Command::Message(msg.into()), + Err(_) => Command::Failure("Couldn't send text message".into()) + } } + Command::Text("yes".into()) } pub fn list_hosts(&self) -> Command {