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::perms; use db::messages::Message; 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 }; let limit = match params.get("limit") { Some(val) => val.as_u64(), None => None }; // TODO: flatten me mommy if let (Some(channel), Some(start), Some(end)) = (channel, start_time, end_time) { match Message::get_time_range(pool, channel, start, end, limit).await { Ok(db_response) => { match db_response { // this absolute lack of data streaming is prolly gonna suck like // a whore in hell week for performance but lets pretend servers don't get massive db::Response::Set(messages) => { set_json_body(response, json!({"messages": messages})); }, db::Response::RestrictedInput(_/*error message to log*/) => { *response.status_mut() = StatusCode::BAD_REQUEST; } _ => { *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } }; }, Err(e) => { eprintln!("{}", e); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } }; } else { *response.status_mut() = StatusCode::BAD_REQUEST; } } pub async fn send_message(pool: &Pool, response: &mut Response, body: Body, params: HashMap<&str, &str>) { /* * Message content is sent in the message body * @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 let uid = http::extract_uid(¶ms); 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; } let channel_id = match params.get("channel") { Some(cval) => { if let Ok(num) = (*cval).to_string().parse::