Updating messages table now on request for send_message
- has not undergone any testing however and we're not building a response yet
This commit is contained in:
parent
1944152252
commit
ffaf602bef
@ -1,17 +1,19 @@
|
|||||||
use std::borrow::Cow;
|
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 mysql_async::error::Error;
|
||||||
use hyper::{Response, Body};
|
use hyper::{Response, Body};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
use crate::members::Member;
|
use crate::members::Member;
|
||||||
use crate::channels::{Channel, ChannelID};
|
use crate::channels::{Channel, ChannelID, ChannelType};
|
||||||
|
|
||||||
|
|
||||||
struct Message {
|
struct Message {
|
||||||
author: Member,
|
author: Member,
|
||||||
date: u64,
|
date: u64, // used as thr primary key in our table
|
||||||
content: String,
|
content: String,
|
||||||
channel: Channel
|
channel: Channel
|
||||||
}
|
}
|
||||||
@ -48,13 +50,48 @@ fn validate_params(p: &Value, keys: Vec<&str>) -> bool {
|
|||||||
return fail == false;
|
return fail == false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn update_messages_table(pool: &Pool, secret: &str, content: Option<&str>, id: Option<ChannelID>)
|
async fn update_messages_table(pool: &Pool, secret: &str, content: Option<&str>, id: Option<ChannelID>)
|
||||||
-> Result<(), mysql_async::error::Error> {
|
-> Result<(), mysql_async::error::Error> {
|
||||||
match (content, id) {
|
match (content, id) {
|
||||||
(Some(content), Some(id)) => {
|
(Some(content), Some(id)) => {
|
||||||
let conn = pool.get_conn().await?;
|
let conn = pool.get_conn().await?;
|
||||||
// insert the thing into our db
|
// get the user data first since we kinda need it
|
||||||
Ok(())
|
// make sure the channel exists
|
||||||
|
const CID_QUERY: &'static str = "SELECT kind from `channels` WHERE id = :id";
|
||||||
|
let (conn, row): (Conn, Option<i32>) = 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<u64>) = 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 <update_messages_table, batch_exec>"),
|
||||||
|
"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");
|
let x = Cow::from("Missing required parameters to create message");
|
||||||
@ -75,7 +112,6 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
|
|||||||
let secret: &str = params.get("secret").unwrap().as_str().unwrap(); //auth sucess guarantees this param is fine
|
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 {
|
match update_messages_table(pool, secret, content, id).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user