diff --git a/src/chat.rs b/src/chat.rs index 5ee6e48..835889a 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1,14 +1,51 @@ /* Contains Rocket code for chat/message functionality */ extern crate log; -use crate::file_io::{read_json}; +use crate::file_io::read_json; use crate::message::{Message, MessageInput}; use rocket_contrib::json::{Json, JsonValue}; use chrono::prelude::*; use uuid::Uuid; use crate::user::User; +// Create full message object and write to file +fn create_message(message: Json, file: &str, user: &User) -> JsonValue { + // create full message object + // append message to file + + // Create proper datetime format out of string + let date_split: Vec<&str> = message.date.split("-").collect(); + + let year: i32 = match date_split[0].trim().parse() { // extract year + Err(why) => panic!("could not extract year from given date: {}", why), + Ok(year) => year, + }; + + let month: u32 = match date_split[1].trim().parse() { // extract month + Err(why) => panic!("could not extract month from given date: {}", why), + Ok(month) => month, + }; + + let day: u32 = match date_split[2].trim().parse() { // extract day + Err(why) => panic!("could not extract day from given date: {}", why), + Ok(month) => month, + }; + + let date: DateTime = Utc.ymd(year, month, day).and_hms(9, 10, 11); + let message_obj: Message = Message { + id: Uuid::new_v4(), + user: user.to_owned(), + body: message.body.to_string(), + created_at: date, + }; + println!("{:?}", message_obj); + return json!({ + "status": "ok", + "reason": "message created", + }); +} + // Check if user can create the message, and then create more info about the message -fn create_message(message: Json, file: &str) -> JsonValue { +fn check_token(message: Json) -> JsonValue { // check if token is correct for name given let users: Vec = read_json(); // create vector out of users in json file @@ -17,10 +54,7 @@ fn create_message(message: Json, file: &str) -> JsonValue { if i.name == message.name.to_lowercase() { // if it finds the user in the file if i.session_token == message.token { // if token matches info!("user exists and given token matches"); - return json!({ - "status": "ok", - "reason": "token matches" - }) + return create_message(message, "messages.json", i); } else { warn!("token does not match!"); return json!({ @@ -30,31 +64,15 @@ fn create_message(message: Json, file: &str) -> JsonValue { }; }; }; + warn!("user not found"); json!({ "status": "fail", "reason": "user not found" }) -/* - // create full message object - // append message to file - - let date_split: Vec<&str> = message.date.split("-").collect(); - let year: i32 = match date_split[0].trim().parse() { - Err(why) => panic!("could not extract year from given date: {}", why), - Ok(year) => year, - }; - let date: DateTime = Utc.ymd(year, 7, 7).and_hms(9, 10, 11); - let message_obj: Message = Message { - id: Uuid::new_v4(), - user: User { name: message.name.to_string(), pin_hashed: "no".to_string(), pronouns: "she/her".to_string(), session_token: "NULL".to_string() }, - body: message.body.to_string(), - created_at: Utc.ymd(2005, 7, 8).and_hms(9, 10, 11), - }; - println!("{:?}", message_obj);*/ } // Receive a basic message #[post("/api/message/send", format = "json", data = "")] pub fn send_message(message: Json>) -> JsonValue { - create_message(message, "messages.json") + check_token(message) }