|
@@ -1,7 +1,10 @@
|
|
|
|
|
+use std::{str::FromStr, time::Duration};
|
|
|
|
|
+
|
|
|
use cursive::{views::TextArea, Cursive};
|
|
use cursive::{views::TextArea, Cursive};
|
|
|
|
|
+use url::Url;
|
|
|
use utils::{binary::checksum, hash::Hashable as _, serialize::Serialize as _};
|
|
use utils::{binary::checksum, hash::Hashable as _, serialize::Serialize as _};
|
|
|
|
|
|
|
|
-use crate::{get_appdata, message::Message, ui, Appdata};
|
|
|
|
|
|
|
+use crate::{API_CONNECTION_TIMEOUT, API_DEFAULT_PORT, Appdata, get_appdata, message::Message, ui};
|
|
|
|
|
|
|
|
// TODO (low): Create proper user objects. Related: add a way to verify a user.
|
|
// TODO (low): Create proper user objects. Related: add a way to verify a user.
|
|
|
pub fn on_user_click(siv: &mut Cursive, user: &str) {
|
|
pub fn on_user_click(siv: &mut Cursive, user: &str) {
|
|
@@ -29,6 +32,7 @@ pub fn on_message_click(siv: &mut Cursive, message_id: &str) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// TODO: Sending in progress pop-up for slow connections. Currently just hangs the application.
|
|
|
pub fn on_input_submit(siv: &mut Cursive, input_field_id: &str) {
|
|
pub fn on_input_submit(siv: &mut Cursive, input_field_id: &str) {
|
|
|
let text = siv
|
|
let text = siv
|
|
|
.call_on_name(input_field_id, |view: &mut TextArea| {
|
|
.call_on_name(input_field_id, |view: &mut TextArea| {
|
|
@@ -93,15 +97,20 @@ pub enum NetworkError {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn fix_url(url: &str) -> Result<String, NetworkError> {
|
|
fn fix_url(url: &str) -> Result<String, NetworkError> {
|
|
|
- if url.is_empty() {
|
|
|
|
|
- return Err(NetworkError::InvalidUrl);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ let mut url: String = url.to_string();
|
|
|
|
|
|
|
|
- let mut url = url.to_string();
|
|
|
|
|
if !url.starts_with("http://") && !url.starts_with("https://") {
|
|
if !url.starts_with("http://") && !url.starts_with("https://") {
|
|
|
url = format!("http://{}", url);
|
|
url = format!("http://{}", url);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ let mut url = Url::from_str(&url).map_err(|_| NetworkError::InvalidUrl)?;
|
|
|
|
|
+
|
|
|
|
|
+ if url.port().is_none() {
|
|
|
|
|
+ url.set_port(Some(API_DEFAULT_PORT)).map_err(|_| NetworkError::InvalidUrl)?;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let mut url = url.to_string();
|
|
|
|
|
+
|
|
|
if !url.ends_with('/') {
|
|
if !url.ends_with('/') {
|
|
|
url.push('/');
|
|
url.push('/');
|
|
|
}
|
|
}
|
|
@@ -123,6 +132,7 @@ pub fn send_message(siv: &mut Cursive, message: Message) -> Result<(), NetworkEr
|
|
|
let resp = reqwest::blocking::Client::new()
|
|
let resp = reqwest::blocking::Client::new()
|
|
|
.post(format!("{}", url))
|
|
.post(format!("{}", url))
|
|
|
.body(str)
|
|
.body(str)
|
|
|
|
|
+ .timeout(Duration::from_secs(API_CONNECTION_TIMEOUT))
|
|
|
.send()
|
|
.send()
|
|
|
.map_err(|e| NetworkError::ReqwestError(e))?;
|
|
.map_err(|e| NetworkError::ReqwestError(e))?;
|
|
|
|
|
|
|
@@ -140,6 +150,7 @@ pub fn load_messages(siv: &mut Cursive) -> Result<(), NetworkError> {
|
|
|
|
|
|
|
|
let resp = reqwest::blocking::Client::new()
|
|
let resp = reqwest::blocking::Client::new()
|
|
|
.get(url)
|
|
.get(url)
|
|
|
|
|
+ .timeout(Duration::from_secs(API_CONNECTION_TIMEOUT))
|
|
|
.send()
|
|
.send()
|
|
|
.map_err(|e| NetworkError::ReqwestError(e))?;
|
|
.map_err(|e| NetworkError::ReqwestError(e))?;
|
|
|
|
|
|