From 894a5bae34a9e6b990eea00ab400022f8cfd714c Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 14 Apr 2021 22:44:57 -0700 Subject: [PATCH] - Removing more behavior from the cache for complexity reduciton + Cache is no longer contained in app structure This should let us later throw this into an Arc> for better async support. ! Also more cache access safety is being done on the front end because the cahe doesn't guarantee much on access Perhaps some convenience wrappers would make this look nicer(with inline) !!! Main needs a lot of flattening It's technically not much of a problem since most of the code behind 1/2 match's which really just bloat the hell out of indentation making it look bad when its not _that_ bad. However it still bugs me so I should probably do something about that(also (inline)) --- tui/src/api_types.rs | 2 +- tui/src/cache.rs | 42 +++++++++++++++++++----------------------- tui/src/main.rs | 21 ++++++++++++++++----- tui/src/net.rs | 1 + 4 files changed, 37 insertions(+), 29 deletions(-) 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());