use mysql_async::Pool; use hyper::{Response, Body, StatusCode}; use serde_json::Value; use hyper::header::HeaderValue; use serde_json::json; pub async fn get_by_time(pool: &Pool, response: &mut Response, params: Value) { /* * 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, * "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 }; use db::messages::Message; match (channel, start_time, end_time) { (Some(channel), Some(start), Some(end)) => { match Message::get_time_range(pool, channel, start, end).await { Ok(db_response) => { match db_response { db::Response::Set(messages) => { response.headers_mut().insert( "Content-Type", HeaderValue::from_static("application/json")); let payload = json!({"messages": messages}); *response.body_mut() = Body::from(payload.to_string()); }, db::Response::RestrictedInput(_/*error message to log*/) => *response.status_mut() = StatusCode::BAD_REQUEST, _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR }; // this absolute lack of data streaming is prolly gonna suck like // a whoe in hell week for performance but lets pretend servers don't get massive }, Err(e) => { eprintln!("{}", e); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } }; }, _ => *response.status_mut() = StatusCode::BAD_REQUEST }; } pub async fn send_message(pool: &Pool, response: &mut Response, params: Value) { /* * @content: expecting string type * @channel: channel id that we're going to send a message to */ // NOTE: auth module guarantees this will be there in the correct form let author = params.get("id") .unwrap().as_u64().unwrap(); match (params.get("content") , params.get("channel")) { (Some(content_v), Some(channel_id_v)) => { let (content, channel) = (content_v.as_str(), channel_id_v.as_u64()); if let (Some(message), Some(cid)) = (content, channel) { // call returns empty on sucess so we don't need to do anything // TODO: loggin let db_result = db::messages::Message::send(pool, message, cid, author).await; if let Err(issue) = db_result { *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; eprintln!("\t{}", issue); } else { match db_result.unwrap() { db::Response::RestrictedInput(msg) => { // user issue *response.status_mut() = StatusCode::BAD_REQUEST; response.headers_mut().insert("Content-Type", HeaderValue::from_static("application/json")); let payload = json!({"msg": msg}); *response.body_mut() = Body::from(payload.to_string()); }, db::Response::Empty => {}, // nothing to do hyper defaults to 200 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 } } #[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()); } }