diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 6c8d278..2f28ebb 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -1,9 +1,13 @@ 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) { @@ -64,47 +68,55 @@ pub async fn get_by_time(pool: &Pool, response: &mut Response, params: Val } } -pub async fn send_message(pool: &Pool, response: &mut Response, params: Value) { +pub async fn send_message(pool: &Pool, response: &mut Response, body: Body, params: HashMap<&str, &str>) { /* - * @content: expecting string type - * @channel: channel id that we're going to send a message to + * 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 author = http::extract_uid(¶ms); + 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; + } - 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()); + let channel_id = match params.get("channel") { + Some(cval) => { + if let Ok(num) = (*cval).to_string().parse::