*/message/send

Now polls content from the request body
* flattened a bunch of checks with data fetches to the db
Outright reduction of branches yey
This commit is contained in:
shockrah 2021-02-04 01:58:57 -08:00
parent 20aca8a069
commit c2e384a13a

View File

@ -1,9 +1,13 @@
use mysql_async::Pool; use mysql_async::Pool;
use hyper::{Response, Body, StatusCode}; use hyper::{Response, Body, StatusCode};
use hyper::body::to_bytes;
use serde_json::Value; use serde_json::Value;
use serde_json::json; use serde_json::json;
use std::collections::HashMap;
use crate::http::{self, set_json_body}; use crate::http::{self, set_json_body};
use crate::perms;
use db::messages::Message; use db::messages::Message;
pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Value) { pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Value) {
@ -64,47 +68,55 @@ pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Val
} }
} }
pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Value) { pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body, params: HashMap<&str, &str>) {
/* /*
* @content: expecting string type * Message content is sent in the message body
* @channel: channel id that we're going to send a message to * @channel_id: channel id that we're going to send a message to
* TODO: more features here because send_message is a large handler
*/ */
use db::Response::*;
use db::member::Member;
use crate::db::common::FromDB;
// NOTE: auth module guarantees this will be there in the correct form // NOTE: auth module guarantees this will be there in the correct form
let author = http::extract_uid(&params); let uid = http::extract_uid(&params);
let permissions = match Member::get(pool, uid).await {
Row(user) => user.permissions,
_ => 0
};
if perms::has_perm(permissions, perms::SEND_MESSAGES) == false {
*response.status_mut() = StatusCode::BAD_REQUEST;
return;
}
match (params.get("content") , params.get("channel")) { let channel_id = match params.get("channel") {
(Some(content_v), Some(channel_id_v)) => { Some(cval) => {
let (content, channel) = (content_v.as_str(), channel_id_v.as_u64()); if let Ok(num) = (*cval).to_string().parse::<u64>() {
Some(num)
} else {
None
}
}
None => None
};
if let (Some(message), Some(cid)) = (content, channel) { // Black magic
// call returns empty on sucess so we don't need to do anything let body_bytes: &[u8] = &to_bytes(body).await.unwrap(); // yolo
// TODO: loggin let content = String::from_utf8_lossy(body_bytes);
let db_result = db::messages::Message::send(pool, message, cid, author).await;
if let Err(issue) = db_result { // 400 on empty bodies or missing channel id's
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; if content.len() == 0 || channel_id.is_none() {
eprintln!("\t{}", issue); *response.status_mut() = StatusCode::BAD_REQUEST;
} } else {
else { match db::messages::Message::send(pool, &content, channel_id.unwrap(), uid).await {
match db_result.unwrap() { Ok(Empty) => {}, // nothing to do hyper defaults to 200
db::Response::RestrictedInput(msg) => { Ok(RestrictedInput(msg)) => *response.status_mut() = StatusCode::BAD_REQUEST,
// user issue Ok(Other(msg)) => {
*response.status_mut() = StatusCode::BAD_REQUEST; eprintln!("{}", msg);
set_json_body(response, json!({"msg": msg})) *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}, },
db::Response::Empty => {}, // nothing to do hyper defaults to 200 _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
db::Response::Other(msg) => { }
eprintln!("{}", msg);
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
},
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
};
}
}
else {
*response.status_mut() = StatusCode::BAD_REQUEST;
}
},
_ => *response.status_mut() = StatusCode::BAD_REQUEST
} }
} }