/messages/send now correctyl tells the user they're wrong about sendingmessages to a non-existant channel

This commit is contained in:
shockrah 2020-12-30 01:00:47 -08:00
parent 7c95519402
commit dfe53b323e
2 changed files with 44 additions and 6 deletions

View File

@ -125,14 +125,31 @@ impl Message {
VALUES (:time, :content, :author, :channel)"; VALUES (:time, :content, :author, :channel)";
let now = Utc::now().timestamp(); let now = Utc::now().timestamp();
conn.prep_exec(q, params!{ let res = conn.prep_exec(q, params!{
"time" => now, "time" => now,
"content" => content, "content" => content,
"author" => uid, "author" => uid,
"channel" => cid "channel" => cid
}).await?; }).await;
if let Err(e) = res {
return match e {
SqlError::Server(err) => {
if err.code == 1452 {
return Ok(Response::RestrictedInput("Channel not found".into()))
}
else {
Ok(Response::Other(sql_err!("db::messages::send")))
}
},
_ => Ok(Response::Other(sql_err!("db::messages::send")))
}
}
// all good response
else {
return Ok(Response::Empty); return Ok(Response::Empty);
} }
}
} }

View File

@ -1,6 +1,8 @@
use mysql_async::Pool; use mysql_async::Pool;
use hyper::{Response, Body, StatusCode}; use hyper::{Response, Body, StatusCode};
use serde_json::Value; use serde_json::Value;
use hyper::header::HeaderValue;
use serde_json::json;
pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Value) { pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Value) {
@ -19,11 +21,30 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
if let (Some(message), Some(cid)) = (content, channel) { if let (Some(message), Some(cid)) = (content, channel) {
// call returns empty on sucess so we don't need to do anything // call returns empty on sucess so we don't need to do anything
// TODO: loggin // TODO: loggin
if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await { 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; *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
eprintln!("\t{}", issue); eprintln!("\t{}", issue);
} }
// if there's no issue then we don't need to do anything at all 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 { else {
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;