diff --git a/server/src/messages.rs b/server/src/messages.rs index 00f0060..b74fbc5 100644 --- a/server/src/messages.rs +++ b/server/src/messages.rs @@ -1,17 +1,19 @@ use std::borrow::Cow; -use mysql_async::Pool; +use mysql_async::{Conn, Pool, params}; +use mysql_async::prelude::{Queryable}; use mysql_async::error::Error; use hyper::{Response, Body}; use serde_json::Value; +use chrono::Utc; use crate::members::Member; -use crate::channels::{Channel, ChannelID}; +use crate::channels::{Channel, ChannelID, ChannelType}; struct Message { author: Member, - date: u64, + date: u64, // used as thr primary key in our table content: String, channel: Channel } @@ -48,13 +50,48 @@ fn validate_params(p: &Value, keys: Vec<&str>) -> bool { return fail == false; } + async fn update_messages_table(pool: &Pool, secret: &str, content: Option<&str>, id: Option) -> Result<(), mysql_async::error::Error> { match (content, id) { (Some(content), Some(id)) => { let conn = pool.get_conn().await?; - // insert the thing into our db - Ok(()) + // get the user data first since we kinda need it + // make sure the channel exists + const CID_QUERY: &'static str = "SELECT kind from `channels` WHERE id = :id"; + let (conn, row): (Conn, Option) = conn + .first_exec(CID_QUERY, params!{"id"=>id}) + .await?; + + let channel_exists = match row { + Some(r) => r == ChannelType::Text.as_i32(), + None => false + }; + // may as well grab the user id now since the fail case is going to be rare (hopefully) + const UID_QUERY : &'static str = "SELECT userid FROM `keys` WHERE `secret` = :secret"; + let (conn, row): (Conn, Option) = conn + .first_exec(UID_QUERY, params!{"secret"=>secret}) + .await?; + // for a message entry we need: (DATE, CONTENT, AUTHOR_ID, Channel_id) + if channel_exists { + // NOTE: not checking if the user exists as our auth model does that + // for us already + conn.batch_exec( + "INSERT INTO `messages` (id, content, author_id, channel_id) \ + VALUES (:mid, :content, :aid, :cid)", + params!{ + "mid" => Utc::now().timestamp() as u64, + "content" => content, + "aid" => row.expect("uid is None when it should have come back as Some(u64):\ + Occurence in "), + "cid" => id + }).await?; + Ok(()) + } + else { + let x = Cow::from("Channel does not exist"); + Err(Error::Other(x)) + } } _ => { let x = Cow::from("Missing required parameters to create message"); @@ -75,7 +112,6 @@ pub async fn send_message(pool: &Pool, response: &mut Response, params: Va let secret: &str = params.get("secret").unwrap().as_str().unwrap(); //auth sucess guarantees this param is fine match update_messages_table(pool, secret, content, id).await { Ok(_) => { - }, Err(err) => { }