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::