diff --git a/server/migrations/2020-07-06-022319_messages/up.sql b/server/migrations/2020-07-06-022319_messages/up.sql index d97f62f..1674188 100644 --- a/server/migrations/2020-07-06-022319_messages/up.sql +++ b/server/migrations/2020-07-06-022319_messages/up.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS `messages`( `time` BIGINT NOT NULL, `content` VARCHAR(2048) NOT NULL, `author_id` BIGINT UNSIGNED NOT NULL, - `channel_id` BIGINT UNSIGNED NOT NULL, + `channel_name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`author_id`) REFERENCES members(`id`), FOREIGN KEY (`channel_id`) REFERENCES channels(`id`) diff --git a/server/src/auth.rs b/server/src/auth.rs index 66180b7..f1d180f 100644 --- a/server/src/auth.rs +++ b/server/src/auth.rs @@ -14,7 +14,8 @@ pub enum AuthReason { fn open_route(path: &str) -> bool { return path == routes::INVITE_JOIN } -pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> Result { +pub async fn wall_entry(path: &str, pool: &Pool, params: &mut serde_json::Value) -> Result { + use serde_json::json; // Start by Checking if the api key is in our keystore if open_route(path) { Ok(AuthReason::OpenAuth) @@ -32,7 +33,11 @@ pub async fn wall_entry(path: &str, pool: &Pool, params: &serde_json::Value) -> match db_result { Ok((_, row)) => { match row{ - Some(_) => Ok(AuthReason::Good), + Some(user_row) => { + params["userid"] = json!(user_row.0); + params["username"] = json!(user_row.1); + Ok(AuthReason::Good) + }, None => Ok(AuthReason::NoKey) } } diff --git a/server/src/main.rs b/server/src/main.rs index 0430ff0..f4fa954 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -81,9 +81,9 @@ async fn main_responder(request: Request) -> Result, hyper: let params_res = http_params::parse_params(&mut body).await; - if let Ok(params) = params_res { + if let Ok(mut params) = params_res { let pool = Pool::new(&env::var("DATABASE_URL").unwrap()); - if let Ok(auth_result) = auth::wall_entry(path, &pool, ¶ms).await { + if let Ok(auth_result) = auth::wall_entry(path, &pool, &mut params).await { // Deal with permissions errors at this point match auth_result { OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, params).await, diff --git a/server/src/messages.rs b/server/src/messages.rs index 943707a..8bba9fb 100644 --- a/server/src/messages.rs +++ b/server/src/messages.rs @@ -7,21 +7,12 @@ use hyper::{Response, Body, StatusCode}; use serde_json::Value; use chrono::Utc; -use crate::db_types::{UBigInt, VarChar}; +use crate::db_types::{UBigInt}; -struct Message { - id: UBigInt, - content: Option, // some messages later might only have file attachments and not text content - author_id: UBigInt, - channel_id: UBigInt, - permissions: UBigInt, -} - - -pub async fn insert_message_table(pool: &Pool, content: &Value, channel_id: &Value, author_id: UBigInt) +pub async fn insert_message_table(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt) -> Result<(), Error>{ - match (content.as_str(), channel_id.as_u64()) { + match (content.as_str(), channel_name.as_str()) { (Some(content), Some(channel)) => { let conn = pool.get_conn().await?; let time = Utc::now().timestamp(); diff --git a/server/tests/verify_basic_cases.sh b/server/tests/verify_basic_cases.sh index 94eee66..ed653ed 100644 --- a/server/tests/verify_basic_cases.sh +++ b/server/tests/verify_basic_cases.sh @@ -24,6 +24,14 @@ delete_channel() { log_result good_delete_channel 200 $code "$result" } +send_message() { + kv='{"secret":"secret", "content":"message sample", "channel":123}' + result=$($crl $POST $url/message/send -d "$kv") + code=$(echo "$result" | grep HTTP\/1.1 | awk '{print $2}') + # non-existant channel for now but whatever ignore for now + log_result good_send_message 200 $code "$result" +} + # Dispatcher to run our tests if [ -z $1 ];then for cmd in $active_tests;do