* 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
This commit is contained in:
shockrah 2021-04-08 21:21:56 -07:00
parent 6088b0836f
commit 7eb81f38f2
3 changed files with 21 additions and 12 deletions

View File

@ -64,6 +64,7 @@ impl Cache {
} }
impl Cache { impl Cache {
pub async fn switch_channel(&mut self, id: u64) -> Command { pub async fn switch_channel(&mut self, id: u64) -> Command {
if let None = self.active_server { if let None = self.active_server {
return Command::Failure("No active server set!".into()) return Command::Failure("No active server set!".into())
@ -97,10 +98,10 @@ impl Cache {
let secret = &meta.meta.user.secret; let secret = &meta.meta.user.secret;
found = match net::login(url, id, secret).await { found = match net::login(url, id, secret).await {
Ok(jwt) => { Ok(jwt) => {
let twt = jwt.clone();
meta.meta.user.jwt = Some(jwt); meta.meta.user.jwt = Some(jwt);
self.active_server = Some(meta.meta.clone()); self.active_server = Some(meta.meta.clone());
// TODO: Setup websocket to server or something idk // TODO: Setup websocket to server or something idk
net::setup_websocket("adsf").await;
self.active_text = None; self.active_text = None;
true true
}, },
@ -133,7 +134,7 @@ impl Cache {
let jwt = config.user.jwt.as_ref().unwrap(); let jwt = config.user.jwt.as_ref().unwrap();
let url = config.server.url.as_str(); 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()), Ok(_) => Command::Message(msg.into()),
Err(_) => Command::Failure("Couldn't send text message".into()) Err(_) => Command::Failure("Couldn't send text message".into())
} }

View File

@ -11,8 +11,9 @@ use crate::command::Command;
use crate::cache::Cache; use crate::cache::Cache;
use crate::config::ConfigFile; use crate::config::ConfigFile;
use std::{env, fs, error::Error, io}; use std::{env, fs, error::Error, io};
use std::sync::Mutex;
use clap::{App as Clap, Arg, ArgMatches}; 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::{ use tui::{
backend::TermionBackend, backend::TermionBackend,
layout::{Constraint, Direction, Layout}, layout::{Constraint, Direction, Layout},
@ -35,7 +36,7 @@ struct App {
/// Current input mode /// Current input mode
input_mode: InputMode, input_mode: InputMode,
/// History of recorded messages /// History of recorded messages
messages: Vec<Command>, messages: Mutex<Vec<Command>>,
cache: Cache cache: Cache
} }
@ -52,7 +53,7 @@ impl App {
App { App {
input: String::new(), input: String::new(),
input_mode: InputMode::Normal, input_mode: InputMode::Normal,
messages: init_commands, messages: Mutex::new(init_commands),
cache cache
} }
} }
@ -102,7 +103,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Terminal initialization // Terminal initialization
let stdout = io::stdout().into_raw_mode()?; let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout); let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout); let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
@ -156,7 +156,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
f.render_widget(input, chunks[1]); f.render_widget(input, chunks[1]);
let mut lines: Vec<Spans> = Vec::new(); let mut lines: Vec<Spans> = 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()); &lines.push(cmd.styled());
} }
@ -220,7 +221,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// ensure that we don't bother with empty input // ensure that we don't bother with empty input
if trimmed.len() != 0 { if trimmed.len() != 0 {
let cmd = Command::from(trimmed); 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 // only for networked commands do we need to touch cache
Command::Channel(id) => app.cache.switch_channel(id).await, Command::Channel(id) => app.cache.switch_channel(id).await,
Command::Server(host) => app.cache.switch_server(&host).await, Command::Server(host) => app.cache.switch_server(&host).await,

View File

@ -1,8 +1,8 @@
use serde::Deserialize; use serde::Deserialize;
use crate::api_types::{Jwt, Channel, TEXT_CHANNEL}; 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::Result as HttpResult;
use reqwest::Response;
// TODO: Url generation is kinda gross looking but i'm not 100% // 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 // 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<Vec<Cha
Ok(response.channels) Ok(response.channels)
} }
pub async fn send_text(url: &str, id: u64, jwt: &str, chan: u64) -> HttpResult<Response> { pub async fn send_text<'m>(url: &str, id: u64, jwt: &str, chan: u64, msg: &str) -> HttpResult<Response> {
let client = Client::new(); 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( let url = Url::parse_with_params(
&format!("{}/message/send", url), &format!("{}/message/send", url),
&[ &[
@ -48,5 +50,10 @@ pub async fn send_text(url: &str, id: u64, jwt: &str, chan: u64) -> HttpResult<R
("channel_id", &format!("{}", chan)) ("channel_id", &format!("{}", chan))
] ]
).unwrap(); ).unwrap();
Ok(client.post(url).send().await?)
let body = Body::from(msg.to_string());
Ok(client.post(url).body(body).headers(headers).send().await?)
}
pub async fn setup_websocket(url: &str) {
} }