+ Send message callback now implemented

+ channel switcher works as well, although it relies on /lc being ran prior to
its invocation
This commit is contained in:
shockrah 2021-04-08 15:45:20 -07:00
parent 9ad963f097
commit 6088b0836f

View File

@ -65,12 +65,26 @@ impl Cache {
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!"))
if let None = self.active_server {
return Command::Failure("No active server set!".into())
}
// find the channel meta data
let mut msg = String::new();
for (_url, cache) in &self.servers {
if let Some(channel_cache) = cache.channels.get(&id) {
let meta = channel_cache.meta.clone();
msg = format!("Switched to channel: [{}]: {}", meta.id, meta.name);
self.active_text = Some(meta);
break;
}
}
if msg.len() == 0 {
Command::Failure(format!("Channel not found in cache try /lc to refresh cache"))
} else {
Command::Failure(format!("No server selected yet"))
Command::Text(msg)
}
}
pub async fn switch_server(&mut self, host: &str) -> Command {
// TODO: implement some better fuzzy finding for better ergonomics
// Reallt though the ux is still pretty bad here
@ -111,12 +125,20 @@ impl Cache {
}
}
pub async fn send_message(&mut self, id: &str) -> Command {
if let Some(server) = self.active_server.as_ref() {
Command::Failure(format!("Not implemented yet!"))
} else {
Command::Failure(format!("No active server+channel to send message to"))
pub async fn send_message(&self, msg: &str) -> Command {
if let Some(channel) = &self.active_text {
let config = self.active_server.as_ref().unwrap();
let chan = channel.id;
let uid = config.user.id;
let jwt = config.user.jwt.as_ref().unwrap();
let url = config.server.url.as_str();
return match net::send_text(url, uid, jwt, chan).await {
Ok(_) => Command::Message(msg.into()),
Err(_) => Command::Failure("Couldn't send text message".into())
}
}
Command::Text("yes".into())
}
pub fn list_hosts(&self) -> Command {