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>"]
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
rtc = []
|
||||
|
||||
[workspace]
|
||||
[dependencies]
|
||||
# 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"
|
||||
|
||||
|
||||
# Crypto things
|
||||
getrandom = "0.1"
|
||||
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 permissions = match Member::get(pool, uid).await {
|
||||
Row(user) => user.permissions,
|
||||
_ => 0
|
||||
Ok(Row(user)) => user.permissions,
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
0
|
||||
}
|
||||
};
|
||||
|
||||
// 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 {
|
||||
match Channel::delete(pool, id).await {
|
||||
Success => {/* nothing to do on sucess */},
|
||||
Other(data) => {
|
||||
eprintln!("\t{}", data);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
_ => { // ngmi
|
||||
eprintln!("\tBro like restart the server this branch should never execute");
|
||||
Ok(Success) => {/* nothing to do on sucess */},
|
||||
Err(e) => { // ngmi
|
||||
eprintln!("{}", e);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
|
@ -34,15 +34,21 @@ pub async fn get_self(p: &Pool, response: &mut Response<Body>, params: HashMap<S
|
||||
let uid = qs_param!(params, "id", u64).unwrap();
|
||||
|
||||
match Member::get(p, uid).await {
|
||||
Row(user) => {
|
||||
set_json_body(response, json!({
|
||||
"id" : user.id,
|
||||
"name" : user.name,
|
||||
"permissions" : user.permissions
|
||||
}));
|
||||
Ok(dbresponse) => {
|
||||
match dbresponse {
|
||||
Row(user) => {
|
||||
set_json_body(response, json!({
|
||||
"id" : user.id,
|
||||
"name" : user.name,
|
||||
"permissions" : user.permissions
|
||||
}));
|
||||
},
|
||||
Other(e) => eprintln!("{}", e),
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
Other(e) => eprintln!("{}", e),
|
||||
_ => {
|
||||
Err(e) => {
|
||||
eprintln!("[HTTP-DB-ERROR] {}", e);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
};
|
||||
@ -77,20 +83,27 @@ 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>) {
|
||||
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{
|
||||
match Member::get(p, target).await {
|
||||
Row(member) => {
|
||||
set_json_body(response, json!({"member": {
|
||||
"id": member.id,
|
||||
"name": member.name,
|
||||
"status": member.status,
|
||||
"permissions": member.permissions
|
||||
}}));
|
||||
StatusCode::OK
|
||||
Ok(dbresponse) => match dbresponse {
|
||||
Row(member) => {
|
||||
set_json_body(response, json!({"member": {
|
||||
"id": member.id,
|
||||
"name": member.name,
|
||||
"status": member.status,
|
||||
"permissions": member.permissions
|
||||
}}));
|
||||
StatusCode::OK
|
||||
},
|
||||
Empty => StatusCode::NOT_FOUND,
|
||||
Other(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR
|
||||
},
|
||||
Empty => StatusCode::NOT_FOUND,
|
||||
Other(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR
|
||||
Err(e) => {
|
||||
eprintln!("[HTTP-DB-ERROR] {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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 {
|
||||
Row(user) => user.permissions,
|
||||
_ => 0
|
||||
Ok(dbresponse) => match dbresponse {
|
||||
Row(user) => user.permissions,
|
||||
_ => 0
|
||||
},
|
||||
Err(e) => 0
|
||||
};
|
||||
if perms::has_perm(permissions, perms::SEND_MESSAGES) == false {
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
@ -108,7 +111,6 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
||||
else {
|
||||
// ctype safe unwrap
|
||||
match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await {
|
||||
#[cfg(feature = "rtc")]
|
||||
Ok(Row(msg)) => {
|
||||
use crate::rtc;
|
||||
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) {
|
||||
use hyper::{Body, Request};
|
||||
use hyper::client::Client;
|
||||
use serde_json::json;
|
||||
|
||||
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)
|
||||
}
|
||||
// just open and close for now
|
||||
let (mut ws, _) = connect_async("ws://localhost:5648").await.unwrap();
|
||||
ws.close(None);
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ async fn handle_connections(stream: TcpStream, peermap: PeerMap) {
|
||||
_ => None
|
||||
};
|
||||
|
||||
println!("{:?}", request.headers());
|
||||
println!("[DEBUG] Incoming headers {:?}", request.headers());
|
||||
let entry = request.headers()
|
||||
.iter().find(|(name, _)| name.as_str() == "jwt");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user