From 7eb81f38f2f6fbed207224e8a1bd9f7e28dd0ec2 Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 8 Apr 2021 21:21:56 -0700 Subject: [PATCH] * Fixing bad /message/send route callback Issue was that we weren't sending the correct headers/body data(ya I know) - Removing mouse control from app since its completel irrelevant !+ Putting app messagses behind a mutex for now since they are written to by sockets and renderer process in the next few patches --- tui/src/cache.rs | 5 +++-- tui/src/main.rs | 13 +++++++------ tui/src/net.rs | 15 +++++++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tui/src/cache.rs b/tui/src/cache.rs index 1f90271..879cea9 100644 --- a/tui/src/cache.rs +++ b/tui/src/cache.rs @@ -64,6 +64,7 @@ impl Cache { } impl Cache { + pub async fn switch_channel(&mut self, id: u64) -> Command { if let None = self.active_server { return Command::Failure("No active server set!".into()) @@ -97,10 +98,10 @@ impl Cache { let secret = &meta.meta.user.secret; found = match net::login(url, id, secret).await { Ok(jwt) => { - let twt = jwt.clone(); meta.meta.user.jwt = Some(jwt); self.active_server = Some(meta.meta.clone()); // TODO: Setup websocket to server or something idk + net::setup_websocket("adsf").await; self.active_text = None; true }, @@ -133,7 +134,7 @@ impl Cache { 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 { + return match net::send_text(url, uid, jwt, chan, msg).await { Ok(_) => Command::Message(msg.into()), Err(_) => Command::Failure("Couldn't send text message".into()) } diff --git a/tui/src/main.rs b/tui/src/main.rs index a542e42..f33e0ed 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -11,8 +11,9 @@ 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}; -use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen}; +use termion::{event::Key, raw::IntoRawMode, screen::AlternateScreen}; use tui::{ backend::TermionBackend, layout::{Constraint, Direction, Layout}, @@ -35,7 +36,7 @@ struct App { /// Current input mode input_mode: InputMode, /// History of recorded messages - messages: Vec, + messages: Mutex>, cache: Cache } @@ -52,7 +53,7 @@ impl App { App { input: String::new(), input_mode: InputMode::Normal, - messages: init_commands, + messages: Mutex::new(init_commands), cache } } @@ -102,7 +103,6 @@ async fn main() -> Result<(), Box> { // Terminal initialization let stdout = io::stdout().into_raw_mode()?; - let stdout = MouseTerminal::from(stdout); let stdout = AlternateScreen::from(stdout); let backend = TermionBackend::new(stdout); let mut terminal = Terminal::new(backend)?; @@ -156,7 +156,8 @@ async fn main() -> Result<(), Box> { f.render_widget(input, chunks[1]); let mut lines: Vec = Vec::new(); - for cmd in app.messages.iter().rev() { + let msgs = app.messages.lock().unwrap(); + for cmd in msgs.iter().rev() { &lines.push(cmd.styled()); } @@ -220,7 +221,7 @@ async fn main() -> Result<(), Box> { // ensure that we don't bother with empty input if trimmed.len() != 0 { let cmd = Command::from(trimmed); - app.messages.push(match cmd { + app.messages.lock().unwrap().push(match cmd { // only for networked commands do we need to touch cache Command::Channel(id) => app.cache.switch_channel(id).await, Command::Server(host) => app.cache.switch_server(&host).await, diff --git a/tui/src/net.rs b/tui/src/net.rs index 6fe8e95..d3a73e0 100644 --- a/tui/src/net.rs +++ b/tui/src/net.rs @@ -1,8 +1,8 @@ use serde::Deserialize; use crate::api_types::{Jwt, Channel, TEXT_CHANNEL}; -use reqwest::{Client, Url}; +use reqwest::{Client, Url, Body, Response}; +use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE}; use reqwest::Result as HttpResult; -use reqwest::Response; // TODO: Url generation is kinda gross looking but i'm not 100% // convinced it needs to be pretty if its just going to add overhead @@ -38,8 +38,10 @@ pub async fn list_channels(url: &str, id: u64, jwt: &str) -> HttpResult HttpResult { +pub async fn send_text<'m>(url: &str, id: u64, jwt: &str, chan: u64, msg: &str) -> HttpResult { let client = Client::new(); + let mut headers = HeaderMap::new(); + headers.insert(CONTENT_TYPE, HeaderValue::from_str("text/plain").unwrap()); let url = Url::parse_with_params( &format!("{}/message/send", url), &[ @@ -48,5 +50,10 @@ pub async fn send_text(url: &str, id: u64, jwt: &str, chan: u64) -> HttpResult