Opting for qs_param to reduce on visual cancer
This commit is contained in:
parent
ab9fef2ccc
commit
769aa72cdf
@ -1,47 +1,35 @@
|
|||||||
use mysql_async::Pool;
|
use mysql_async::Pool;
|
||||||
use hyper::{Response, Body, StatusCode};
|
use hyper::{Response, Body, StatusCode};
|
||||||
use hyper::body::to_bytes;
|
use hyper::body::to_bytes;
|
||||||
use serde_json::Value;
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::http::{self, set_json_body};
|
use crate::http::set_json_body;
|
||||||
use crate::perms;
|
use crate::perms;
|
||||||
|
use crate::qs_param;
|
||||||
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: HashMap<&str, &str>) {
|
||||||
/*
|
/*
|
||||||
* Has a ton of required parameters just be warned
|
* Has a ton of required parameters just be warned
|
||||||
* @channel: channel id we're looking at
|
* @channel: channel id we're looking at
|
||||||
* @start-time: how long ago do we start searching
|
* @start-time: how long ago do we start searching
|
||||||
* @end-time: how long ago do we stop searching
|
* @end-time: how long ago do we stop searching
|
||||||
* {
|
* {
|
||||||
* "channel": 1,
|
* "channel_id": 1,
|
||||||
* "start-time": unix_now - 24 hours
|
* "start-time": unix_now - 24 hours
|
||||||
* "end-time": unix_now - 23 hours
|
* "end-time": unix_now - 23 hours
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
let channel = match params.get("channel") {
|
let channel_id = qs_param!(params, "channel_id", u64);
|
||||||
Some(chan_v) => chan_v.as_u64(),
|
let start_time = qs_param!(params, "start-time", i64);
|
||||||
None => None
|
let end_time = qs_param!(params, "end-time", i64);
|
||||||
};
|
let limit = qs_param!(params, "limit", u64);
|
||||||
let start_time = match params.get("start-time") {
|
|
||||||
Some(val) => val.as_i64(),
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
let end_time = match params.get("end-time") {
|
|
||||||
Some(val) => val.as_i64(),
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
let limit = match params.get("limit") {
|
|
||||||
Some(val) => val.as_u64(),
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: flatten me mommy
|
// TODO: flatten me mommy
|
||||||
if let (Some(channel), Some(start), Some(end)) = (channel, start_time, end_time) {
|
if let (Some(channel), Some(start), Some(end)) = (channel_id, start_time, end_time) {
|
||||||
match Message::get_time_range(pool, channel, start, end, limit).await {
|
match Message::get_time_range(pool, channel, start, end, limit).await {
|
||||||
Ok(db_response) => {
|
Ok(db_response) => {
|
||||||
match db_response {
|
match db_response {
|
||||||
@ -79,7 +67,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
use crate::db::common::FromDB;
|
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 uid = http::extract_uid(¶ms);
|
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,
|
Row(user) => user.permissions,
|
||||||
_ => 0
|
_ => 0
|
||||||
@ -89,16 +77,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let channel_id = match params.get("channel") {
|
let channel_id = qs_param!(params, "channel_id", u64);
|
||||||
Some(cval) => {
|
|
||||||
if let Ok(num) = (*cval).to_string().parse::<u64>() {
|
|
||||||
Some(num)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
|
|
||||||
// Black magic
|
// Black magic
|
||||||
let body_bytes: &[u8] = &to_bytes(body).await.unwrap(); // yolo
|
let body_bytes: &[u8] = &to_bytes(body).await.unwrap(); // yolo
|
||||||
@ -110,7 +89,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
} else {
|
} else {
|
||||||
match db::messages::Message::send(pool, &content, channel_id.unwrap(), uid).await {
|
match db::messages::Message::send(pool, &content, channel_id.unwrap(), uid).await {
|
||||||
Ok(Empty) => {}, // nothing to do hyper defaults to 200
|
Ok(Empty) => {}, // nothing to do hyper defaults to 200
|
||||||
Ok(RestrictedInput(msg)) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
||||||
Ok(Other(msg)) => {
|
Ok(Other(msg)) => {
|
||||||
eprintln!("{}", msg);
|
eprintln!("{}", msg);
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
@ -120,29 +99,20 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: HashMap<&str, &str>) {
|
||||||
/*
|
/*
|
||||||
* @start-id: u64
|
* @start-id: u64
|
||||||
* @limit: optional<u64>
|
* @limit: optional<u64>
|
||||||
* @channel: u64
|
* @channel: u64
|
||||||
* {
|
* {
|
||||||
* "channel": 1,
|
* "channel_id": 1,
|
||||||
* "start": 123,
|
* "start": 123,
|
||||||
* "limit": 100
|
* "limit": 100
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
let channel = match params.get("channel") {
|
let channel = qs_param!(params, "channel_id", u64);
|
||||||
Some(chan_v) => chan_v.as_u64(),
|
let start_id = qs_param!(params, "start", u64);
|
||||||
None => None
|
let limit = qs_param!(params, "limit", u64);
|
||||||
};
|
|
||||||
let start_id = match params.get("start") {
|
|
||||||
Some(val) => val.as_u64(),
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
let limit = match params.get("limit") {
|
|
||||||
Some(val) => val.as_u64(),
|
|
||||||
None => None
|
|
||||||
};
|
|
||||||
|
|
||||||
if let (Some(channel), Some(start_id)) = (channel, start_id) {
|
if let (Some(channel), Some(start_id)) = (channel, start_id) {
|
||||||
match Message::get_from_id(pool, channel, start_id, limit).await {
|
match Message::get_from_id(pool, channel, start_id, limit).await {
|
||||||
@ -174,33 +144,3 @@ pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: Value)
|
|||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod messaging_tests {
|
|
||||||
use crate::testing::{get_pool, hyper_resp};
|
|
||||||
use serde_json::Value;
|
|
||||||
use hyper::StatusCode;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn send_message_test_missing_channel() {
|
|
||||||
/*
|
|
||||||
* Attempt to send a message i na channel that does not exist
|
|
||||||
*/
|
|
||||||
let p = get_pool();
|
|
||||||
let mut resp = hyper_resp();
|
|
||||||
|
|
||||||
let params: Value = serde_json::from_str(r#"
|
|
||||||
{
|
|
||||||
"channel": "this does not exist",
|
|
||||||
"content": "bs message",
|
|
||||||
"id": 420
|
|
||||||
}
|
|
||||||
"#).unwrap();
|
|
||||||
|
|
||||||
super::send_message(&p, &mut resp, params).await;
|
|
||||||
|
|
||||||
assert_ne!(StatusCode::OK, resp.status());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user