use mysql_async::Pool; use hyper::{Response, Body, StatusCode}; use serde_json::Value; 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 if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await { *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; eprintln!("\t{}", issue); } // if there's no issue then we don't need to do anything at all } 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()); } }