diff --git a/tui/src/api_types.rs b/tui/src/api_types.rs index 3d5c5cd..9747b42 100644 --- a/tui/src/api_types.rs +++ b/tui/src/api_types.rs @@ -31,7 +31,7 @@ impl Message { } -#[derive(Deserialize, Clone)] +#[derive(Debug, Deserialize, Clone)] pub struct Channel { pub name: String, pub id: u64, diff --git a/tui/src/cache.rs b/tui/src/cache.rs index 9df99ae..a13fff6 100644 --- a/tui/src/cache.rs +++ b/tui/src/cache.rs @@ -132,30 +132,26 @@ impl Cache { Command::Text(hosts) } - pub async fn list_channels(&mut self) -> Command { - if let None = self.active_server { - return Command::Failure("No active server".into()) + pub fn add_channels(&mut self, url: &str, channels: &[api::Channel]) { + // if the url isn't there we just silently fail? + let serv_ref = self.servers.get_mut(url).unwrap(); + for chan in channels.iter() { + let cache = ChannelCache { + meta: chan.clone(), + messages: Vec::new() + }; + serv_ref.channels.insert(chan.id, cache); } - // we have to own our own reference here to avoid mutating after immutable borrows - let config = self.active_server.clone().unwrap(); - let id = config.user.id; - let jwt = config.user.jwt.unwrap(); - let url = config.server.url.as_str(); - let mut buf = String::new(); - match net::list_channels(url, id, jwt.as_str()).await { - Ok(mut channels) => { - for chan in channels.iter_mut() { - let id = chan.id; - let chan_cache = ChannelCache { meta: chan.clone(), messages: Vec::new() }; - self.servers.get_mut(url).unwrap().channels.insert(id, chan_cache); - let chan = format!("{}: {},", id, chan.name); - buf.push_str(chan.as_str()); - } - Command::Text(buf) - }, - Err(e) => { - Command::Failure(format!("Couldn't fetch channels: {:?}", e.status())) - } + } + + pub fn session(&self) -> Option<(String, u64, String)>{ + if let Some(serv) = &self.active_server { + let id = serv.user.id; + let url = serv.server.url.clone(); + let jwt = serv.user.jwt.clone().unwrap_or("".to_string()); + Some((url, id, jwt)) + } else { + None } } } diff --git a/tui/src/main.rs b/tui/src/main.rs index 44c9ac6..6920c19 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -10,7 +10,6 @@ mod net; use crate::util::event::{Event, Events}; use crate::command::Command; use crate::cache::Cache; -use crate::config::ConfigFile; use std::{env, fs, error::Error, io}; use std::sync::Mutex; use clap::{App as Clap, Arg, ArgMatches}; @@ -41,7 +40,7 @@ struct App { } impl App { - fn new(config: &ConfigFile) -> App { + fn new() -> App { let init_commands = vec![ Command::Text("! /chan | /channel - Switches text context to that channel".into()), Command::Text("! /serv | /server - Must include port if its not default to 80/443".into()), @@ -49,7 +48,6 @@ impl App { Command::Text("! /help - Shows the help menu".into()), Command::Text("! Commands: available".into()), ]; - let cache = Cache::from(&config); App { input: String::new(), input_mode: InputMode::Normal, @@ -110,7 +108,7 @@ async fn main() -> Result<(), Box> { let mut events = Events::new(); // Create default app state - let mut app = App::new(&config); + let mut app = App::new(); let cache: Mutex = Mutex::new(Cache::from(&config)); loop { @@ -243,7 +241,20 @@ async fn main() -> Result<(), Box> { }, Command::Message(msg) => cache.lock().unwrap().send_message(&msg).await, Command::ListHost => cache.lock().unwrap().list_hosts(), - Command::ListChannel => cache.lock().unwrap().list_channels().await, + Command::ListChannel => { + let session_params = cache.lock().unwrap().session(); + if let Some((url, id, jwt)) = session_params { + match net::list_channels(&url , id, &jwt).await { + Ok(channels) => { + cache.lock().unwrap().add_channels(&url, channels.as_slice()); + Command::Text(format!("{:?}", channels)) + }, + Err(_) => Command::Text("Coulnd't fetch channels".into()) + } + } else { + Command::Text("adf".into()) + } + }, _ => cmd }); } diff --git a/tui/src/net.rs b/tui/src/net.rs index 5b31608..053da68 100644 --- a/tui/src/net.rs +++ b/tui/src/net.rs @@ -53,6 +53,7 @@ pub async fn send_text<'m>(url: &str, id: u64, jwt: &str, chan: u64, msg: &str) Ok(client.post(url).body(body).headers(headers).send().await?) } +#[allow(dead_code)] pub fn ws_url(base: &str, jwt: Option) -> String { let base = format!("{}/text", base); let jwt = jwt.unwrap_or("hehexd".into());