From 769aa72cdf865bd9d003cfa62fa2a7b4f45bf9f8 Mon Sep 17 00:00:00 2001 From: shockrah Date: Fri, 5 Feb 2021 16:39:24 -0800 Subject: [PATCH] Opting for qs_param to reduce on visual cancer --- json-api/src/messages.rs | 94 ++++++++-------------------------------- 1 file changed, 17 insertions(+), 77 deletions(-) diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 2f28ebb..eeb696a 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -1,47 +1,35 @@ use mysql_async::Pool; use hyper::{Response, Body, StatusCode}; use hyper::body::to_bytes; -use serde_json::Value; use serde_json::json; use std::collections::HashMap; -use crate::http::{self, set_json_body}; +use crate::http::set_json_body; use crate::perms; +use crate::qs_param; use db::messages::Message; -pub async fn get_by_time(pool: &Pool, response: &mut Response, params: Value) { +pub async fn get_by_time(pool: &Pool, response: &mut Response, params: HashMap<&str, &str>) { /* * Has a ton of required parameters just be warned * @channel: channel id we're looking at * @start-time: how long ago do we start searching * @end-time: how long ago do we stop searching * { - * "channel": 1, + * "channel_id": 1, * "start-time": unix_now - 24 hours * "end-time": unix_now - 23 hours * } * */ - let channel = match params.get("channel") { - Some(chan_v) => chan_v.as_u64(), - None => None - }; - 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 - }; + let channel_id = qs_param!(params, "channel_id", u64); + let start_time = qs_param!(params, "start-time", i64); + let end_time = qs_param!(params, "end-time", i64); + let limit = qs_param!(params, "limit", u64); // 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 { Ok(db_response) => { match db_response { @@ -79,7 +67,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body use crate::db::common::FromDB; // 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 { Row(user) => user.permissions, _ => 0 @@ -89,16 +77,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body return; } - let channel_id = match params.get("channel") { - Some(cval) => { - if let Ok(num) = (*cval).to_string().parse::() { - Some(num) - } else { - None - } - } - None => None - }; + let channel_id = qs_param!(params, "channel_id", u64); // Black magic 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 } else { match db::messages::Message::send(pool, &content, channel_id.unwrap(), uid).await { 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)) => { eprintln!("{}", msg); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; @@ -120,29 +99,20 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body } } -pub async fn from_id(pool: &Pool, response: &mut Response, params: Value) { +pub async fn from_id(pool: &Pool, response: &mut Response, params: HashMap<&str, &str>) { /* * @start-id: u64 * @limit: optional * @channel: u64 * { - * "channel": 1, + * "channel_id": 1, * "start": 123, * "limit": 100 * } */ - let channel = match params.get("channel") { - Some(chan_v) => chan_v.as_u64(), - None => None - }; - 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 - }; + let channel = qs_param!(params, "channel_id", u64); + let start_id = qs_param!(params, "start", u64); + let limit = qs_param!(params, "limit", u64); if let (Some(channel), Some(start_id)) = (channel, start_id) { 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, params: Value) *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()); - } - -} -