From 1c85c88f966a8effcc1afe593022723eb5d489a7 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 4 Apr 2021 14:13:37 -0700 Subject: [PATCH] * Normalized cache data structures a bit * Also changed up the naming scheme to be more clear * Config -> ConfigFile - Removed ConfigFile::server_url method ! The ConfigFile struct is now pretty much entirely used for disk operations * update_jwt method also take in correct parameters --- tui/src/cache.rs | 46 ++++++++++++++++++++++++++++++++++++++-------- tui/src/config.rs | 43 ++++++++++++++----------------------------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/tui/src/cache.rs b/tui/src/cache.rs index 5d63c7c..c5c7a03 100644 --- a/tui/src/cache.rs +++ b/tui/src/cache.rs @@ -12,11 +12,13 @@ * */ -use crate::config::{ ServerMeta, UserConfig }; +use crate::config::ServerMeta; use crate::api_types::{Channel, Message}; use crate::command::Command; +use crate::config::ConfigFile; use std::collections::HashMap; +use hyper::Request; struct ChannelCache { meta: Channel, @@ -25,20 +27,32 @@ struct ChannelCache { struct ServerCache { meta: ServerMeta, - user: UserConfig, channels: Vec } pub struct Cache { - // Hostname -> Cache + // url -> Cache servers: HashMap, - active_server: Option + active_server: Option } -impl Default for Cache { - fn default() -> Cache { +impl ServerCache { + pub fn from(meta: ServerMeta, channels: Vec) -> Self { + ServerCache { meta, channels } + } +} + +impl Cache { + pub fn from(config: &ConfigFile) -> Cache { + let mut servers = HashMap::new(); + for cfg in &config.servers { + let i_server_cache = ServerCache::from(cfg.server.clone(), Vec::new()); + let url = i_server_cache.meta.url.clone(); + servers.insert(url, i_server_cache); + } + Cache { - servers: HashMap::new(), + servers, active_server: None } } @@ -61,10 +75,26 @@ impl Cache { } pub async fn send_message(&mut self, id: &str) -> Command { - if let Some(host) = self.active_server.as_ref() { + use hyper::body::HttpBody as _; + use hyper::Client; + + if let Some(server) = self.active_server.as_ref() { + let url = format!("{}/message/send", server.url); + let request = Request::builder() + .method("POST") + .uri(url).body(()).unwrap(); + Command::Failure(format!("Not implemented yet!")) } else { Command::Failure(format!("No active server+channel to send message to")) } } + + pub fn list_hosts(&self) -> Command { + let mut hosts = String::from("Hosts: "); + for (host, _) in self.servers.iter() { + hosts.push_str(format!("{}, ", host).as_str()); + } + Command::Text(hosts) + } } diff --git a/tui/src/config.rs b/tui/src/config.rs index f87b864..213c0cf 100644 --- a/tui/src/config.rs +++ b/tui/src/config.rs @@ -2,63 +2,46 @@ use serde::{Serialize, Deserialize}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ServerMeta { - pub protocol: String, - pub hostname: String, - pub port: Option, + pub url: String, // Should never end with a '/' pub description: String, pub name: String, } -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct ServerConfig { - pub user: UserConfig, - pub server: ServerMeta -} #[derive(Clone, Debug, Deserialize, Serialize)] -pub struct UserConfig { +pub struct UserMeta { pub id: u64, pub secret: String, pub jwt: Option, pub permissions: u64, - pub joindate: i64, pub status: i32, } +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct ServerConfig { + pub user: UserMeta, + pub server: ServerMeta +} #[derive(Clone, Debug, Deserialize, Serialize)] -pub struct Config { +pub struct ConfigFile { + // Global option pub username: Option, pub servers: Vec } -impl Config { - pub fn update_jwt(&mut self, hostname: String, jwt: String) { +impl ConfigFile { + pub fn update_jwt(&mut self, url: String, jwt: String) { for servermeta in self.servers.iter_mut() { - if servermeta.server.hostname == hostname { + if servermeta.server.url == url { servermeta.user.jwt = Some(jwt); break } } } - pub fn server_url(&self, hostname: &str) -> Option { - // Finds the base-url for the given hostname, assuming it exists - let mut url: Option = None; - for meta in self.servers.iter() { - if meta.server.hostname == hostname { - url = match meta.server.port { - Some(p) => Some(format!("{}://{}:{}", meta.server.protocol, hostname, p)), - None => Some(format!("{}://{}", meta.server.protocol, hostname)) - }; - break - } - } - return url; - } - pub fn save(&self, path: String) -> std::io::Result<()>{ use std::io::prelude::Write; use std::fs::File; @@ -70,3 +53,5 @@ impl Config { Ok(()) } } + +