More db-lib ok/err wrapping
Some misc changes that don't really belong with anything else are bundled here Those changes aren't actually very important however
This commit is contained in:
parent
75dcb7b73e
commit
c45f8d0482
1075
json-api/Cargo.lock
generated
1075
json-api/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,17 +4,21 @@ version = "0.2.0"
|
|||||||
authors = ["shockrah <alejandros714@protonmail.com>"]
|
authors = ["shockrah <alejandros714@protonmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
rtc = []
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Networking things
|
# Networking things
|
||||||
tokio = { version = "^0.2", features = ["rt-core", "rt-threaded", "macros", "signal"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
hyper = { version = "0.14", features = ["full"] }
|
||||||
|
mysql_async = "0.27"
|
||||||
|
websocket = "0.26.2"
|
||||||
|
tokio-tungstenite = "0.14.0"
|
||||||
|
|
||||||
hyper = "0.13"
|
|
||||||
mysql_async = "0.23.1"
|
|
||||||
|
|
||||||
dotenv = "0.9.0"
|
dotenv = "0.9.0"
|
||||||
|
|
||||||
|
|
||||||
# Crypto things
|
# Crypto things
|
||||||
getrandom = "0.1"
|
getrandom = "0.1"
|
||||||
bcrypt = "0.8"
|
bcrypt = "0.8"
|
||||||
|
@ -85,8 +85,11 @@ pub async fn delete_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
|
|
||||||
let uid = qs_param!(params, "id", u64).unwrap();
|
let uid = qs_param!(params, "id", u64).unwrap();
|
||||||
let permissions = match Member::get(pool, uid).await {
|
let permissions = match Member::get(pool, uid).await {
|
||||||
Row(user) => user.permissions,
|
Ok(Row(user)) => user.permissions,
|
||||||
_ => 0
|
Err(e) => {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
0
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// make sure unpriveleged users don't delete channels somehow
|
// make sure unpriveleged users don't delete channels somehow
|
||||||
@ -108,13 +111,9 @@ pub async fn delete_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
|
|
||||||
if let Some(id) = channel_id {
|
if let Some(id) = channel_id {
|
||||||
match Channel::delete(pool, id).await {
|
match Channel::delete(pool, id).await {
|
||||||
Success => {/* nothing to do on sucess */},
|
Ok(Success) => {/* nothing to do on sucess */},
|
||||||
Other(data) => {
|
Err(e) => { // ngmi
|
||||||
eprintln!("\t{}", data);
|
eprintln!("{}", e);
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
|
||||||
}
|
|
||||||
_ => { // ngmi
|
|
||||||
eprintln!("\tBro like restart the server this branch should never execute");
|
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,8 @@ pub async fn get_self(p: &Pool, response: &mut Response<Body>, params: HashMap<S
|
|||||||
let uid = qs_param!(params, "id", u64).unwrap();
|
let uid = qs_param!(params, "id", u64).unwrap();
|
||||||
|
|
||||||
match Member::get(p, uid).await {
|
match Member::get(p, uid).await {
|
||||||
|
Ok(dbresponse) => {
|
||||||
|
match dbresponse {
|
||||||
Row(user) => {
|
Row(user) => {
|
||||||
set_json_body(response, json!({
|
set_json_body(response, json!({
|
||||||
"id" : user.id,
|
"id" : user.id,
|
||||||
@ -42,7 +44,11 @@ pub async fn get_self(p: &Pool, response: &mut Response<Body>, params: HashMap<S
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
Other(e) => eprintln!("{}", e),
|
Other(e) => eprintln!("{}", e),
|
||||||
_ => {
|
_ => {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("[HTTP-DB-ERROR] {}", e);
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -77,8 +83,10 @@ pub async fn post_self_nickname(p: &Pool, response: &mut Response<Body>, params:
|
|||||||
pub async fn get_member(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
pub async fn get_member(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
||||||
let target = qs_param!(params, "member_id", u64);
|
let target = qs_param!(params, "member_id", u64);
|
||||||
|
|
||||||
|
// Returning from from if let for the user parameter parsing
|
||||||
*response.status_mut() = if let Some(target) = target{
|
*response.status_mut() = if let Some(target) = target{
|
||||||
match Member::get(p, target).await {
|
match Member::get(p, target).await {
|
||||||
|
Ok(dbresponse) => match dbresponse {
|
||||||
Row(member) => {
|
Row(member) => {
|
||||||
set_json_body(response, json!({"member": {
|
set_json_body(response, json!({"member": {
|
||||||
"id": member.id,
|
"id": member.id,
|
||||||
@ -91,6 +99,11 @@ pub async fn get_member(p: &Pool, response: &mut Response<Body>, params: HashMap
|
|||||||
Empty => StatusCode::NOT_FOUND,
|
Empty => StatusCode::NOT_FOUND,
|
||||||
Other(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
|
Other(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
_ => StatusCode::INTERNAL_SERVER_ERROR
|
_ => StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("[HTTP-DB-ERROR] {}", e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
StatusCode::BAD_REQUEST
|
StatusCode::BAD_REQUEST
|
||||||
|
@ -77,8 +77,11 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
};
|
};
|
||||||
|
|
||||||
let permissions = match Member::get(pool, uid).await {
|
let permissions = match Member::get(pool, uid).await {
|
||||||
|
Ok(dbresponse) => match dbresponse {
|
||||||
Row(user) => user.permissions,
|
Row(user) => user.permissions,
|
||||||
_ => 0
|
_ => 0
|
||||||
|
},
|
||||||
|
Err(e) => 0
|
||||||
};
|
};
|
||||||
if perms::has_perm(permissions, perms::SEND_MESSAGES) == false {
|
if perms::has_perm(permissions, perms::SEND_MESSAGES) == false {
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
@ -108,7 +111,6 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
else {
|
else {
|
||||||
// ctype safe unwrap
|
// ctype safe unwrap
|
||||||
match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await {
|
match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await {
|
||||||
#[cfg(feature = "rtc")]
|
|
||||||
Ok(Row(msg)) => {
|
Ok(Row(msg)) => {
|
||||||
use crate::rtc;
|
use crate::rtc;
|
||||||
rtc::new_message(msg).await;
|
rtc::new_message(msg).await;
|
||||||
|
@ -13,29 +13,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "rtc")]
|
use websocket::ClientBuilder;
|
||||||
|
use websocket::r#async::client::{Client, ClientNew};
|
||||||
|
use websocket::r#async::TcpStream;
|
||||||
|
use websocket::futures::{Future, Stream, Sink};
|
||||||
|
use websocket::Message;
|
||||||
|
use websocket::url::Url;
|
||||||
|
|
||||||
|
use tokio_tungstenite::connect_async;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub async fn new_message(message: db::Message) {
|
pub async fn new_message(message: db::Message) {
|
||||||
use hyper::{Body, Request};
|
// just open and close for now
|
||||||
use hyper::client::Client;
|
let (mut ws, _) = connect_async("ws://localhost:5648").await.unwrap();
|
||||||
use serde_json::json;
|
ws.close(None);
|
||||||
|
|
||||||
let payload = json!({
|
|
||||||
"event": "new-message",
|
|
||||||
"message": message
|
|
||||||
});
|
|
||||||
println!("Sending event payload => {}", payload);
|
|
||||||
let request = Request::builder()
|
|
||||||
.method("POST")
|
|
||||||
.uri("http://localhost:6750/")
|
|
||||||
.body(Body::from(payload.to_string()))
|
|
||||||
.expect("[ERROR] Couldn't make event request");
|
|
||||||
|
|
||||||
println!("[INFO] Notifying RTC -> new-message");
|
|
||||||
|
|
||||||
let client = Client::new();
|
|
||||||
// capturing both ok/err for loggin purposes
|
|
||||||
match client.request(request).await {
|
|
||||||
Ok(response) => println!("[RTC-NOTIFY] All good id-{}", message.id),
|
|
||||||
Err(e) => eprintln!("{}", e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ async fn handle_connections(stream: TcpStream, peermap: PeerMap) {
|
|||||||
_ => None
|
_ => None
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{:?}", request.headers());
|
println!("[DEBUG] Incoming headers {:?}", request.headers());
|
||||||
let entry = request.headers()
|
let entry = request.headers()
|
||||||
.iter().find(|(name, _)| name.as_str() == "jwt");
|
.iter().find(|(name, _)| name.as_str() == "jwt");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user