* 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
This commit is contained in:
parent
3f1dfbf824
commit
1c85c88f96
@ -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<ChannelCache>
|
||||
}
|
||||
|
||||
pub struct Cache {
|
||||
// Hostname -> Cache
|
||||
// url -> Cache
|
||||
servers: HashMap<String, ServerCache>,
|
||||
active_server: Option<String>
|
||||
active_server: Option<ServerMeta>
|
||||
}
|
||||
|
||||
impl Default for Cache {
|
||||
fn default() -> Cache {
|
||||
impl ServerCache {
|
||||
pub fn from(meta: ServerMeta, channels: Vec<ChannelCache>) -> 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)
|
||||
}
|
||||
}
|
||||
|
@ -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<u16>,
|
||||
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<String>,
|
||||
|
||||
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<String>,
|
||||
pub servers: Vec<ServerConfig>
|
||||
}
|
||||
|
||||
|
||||
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<String> {
|
||||
// Finds the base-url for the given hostname, assuming it exists
|
||||
let mut url: Option<String> = 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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user