
* 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
101 lines
2.9 KiB
Rust
101 lines
2.9 KiB
Rust
/**
|
|
* welcum to the cache zone
|
|
* Notes about data model here
|
|
* Basically none of the ever gets written to disk so its mutability is
|
|
*
|
|
* Memory Model things
|
|
* The cache should always own its own data and nothing else
|
|
* On calls where the cache system needs to take data it should actually
|
|
* make its own copy to not disturb the render side of things as it too requires
|
|
* ownership of its own data. For this reason all parts of this are basically
|
|
* going to be really selfish about its own data
|
|
*
|
|
*/
|
|
|
|
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,
|
|
messages: Vec<Message>
|
|
}
|
|
|
|
struct ServerCache {
|
|
meta: ServerMeta,
|
|
channels: Vec<ChannelCache>
|
|
}
|
|
|
|
pub struct Cache {
|
|
// url -> Cache
|
|
servers: HashMap<String, ServerCache>,
|
|
active_server: Option<ServerMeta>
|
|
}
|
|
|
|
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,
|
|
active_server: None
|
|
}
|
|
}
|
|
}
|
|
|
|
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!"))
|
|
} else {
|
|
Command::Failure(format!("No server selected yet"))
|
|
}
|
|
}
|
|
pub async fn switch_server(&mut self, host: &str) -> Command {
|
|
if let Some(srv_cache) = self.servers.get(host) {
|
|
Command::Failure(format!("Not implemented yet!"))
|
|
} else {
|
|
Command::Failure(format!("Host {{ {} }} not found", host))
|
|
}
|
|
}
|
|
|
|
pub async fn send_message(&mut self, id: &str) -> Command {
|
|
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)
|
|
}
|
|
}
|