2021-07-17 23:41:41 +00:00
|
|
|
/* Contains Rocket code for chat/message functionality */
|
|
|
|
extern crate log;
|
2021-07-18 15:37:11 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use std::sync::Mutex;
|
2021-07-18 14:34:27 +00:00
|
|
|
use crate::file_io::read_json;
|
2021-07-18 02:27:05 +00:00
|
|
|
use crate::message::{Message, MessageInput};
|
2021-07-18 00:32:57 +00:00
|
|
|
use rocket_contrib::json::{Json, JsonValue};
|
2021-07-18 02:27:05 +00:00
|
|
|
use chrono::prelude::*;
|
|
|
|
use uuid::Uuid;
|
|
|
|
use crate::user::User;
|
2021-07-17 23:41:41 +00:00
|
|
|
|
2021-07-18 15:37:11 +00:00
|
|
|
static MESSAGES: Lazy<Mutex<Vec<Message>>> = Lazy::new(|| Mutex::new(Vec::new()));
|
|
|
|
|
2021-07-18 17:16:00 +00:00
|
|
|
#[get("/message/messages.json")]
|
2021-07-18 15:37:11 +00:00
|
|
|
pub fn fetch_messages() -> Json<Vec<Message>> {
|
|
|
|
let messages = {
|
|
|
|
let messages = MESSAGES.lock().unwrap();
|
|
|
|
messages.to_vec()
|
|
|
|
};
|
|
|
|
Json(messages)
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:34:27 +00:00
|
|
|
// Create full message object and write to file
|
|
|
|
fn create_message(message: Json<MessageInput>, 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> = 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,
|
|
|
|
};
|
2021-07-18 15:37:11 +00:00
|
|
|
info!("created mesage: {:?}", message_obj);
|
|
|
|
let mut messages = MESSAGES.lock().unwrap();
|
|
|
|
messages.push(message_obj.to_owned());
|
2021-07-18 14:34:27 +00:00
|
|
|
return json!({
|
|
|
|
"status": "ok",
|
|
|
|
"reason": "message created",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-18 02:27:05 +00:00
|
|
|
// Check if user can create the message, and then create more info about the message
|
2021-07-18 14:34:27 +00:00
|
|
|
fn check_token(message: Json<MessageInput>) -> JsonValue {
|
2021-07-18 02:27:05 +00:00
|
|
|
// check if token is correct for name given
|
2021-07-18 14:17:29 +00:00
|
|
|
let users: Vec<User> = read_json(); // create vector out of users in json file
|
|
|
|
|
|
|
|
for i in &users {
|
|
|
|
// loop through elements
|
|
|
|
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");
|
2021-07-18 14:34:27 +00:00
|
|
|
return create_message(message, "messages.json", i);
|
2021-07-18 14:17:29 +00:00
|
|
|
} else {
|
|
|
|
warn!("token does not match!");
|
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": "token does not match"
|
|
|
|
})
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-07-18 14:34:27 +00:00
|
|
|
warn!("user not found");
|
2021-07-18 14:17:29 +00:00
|
|
|
json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": "user not found"
|
|
|
|
})
|
2021-07-18 02:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive a basic message
|
2021-07-18 17:16:00 +00:00
|
|
|
#[post("/message/send", format = "json", data = "<message>")]
|
2021-07-18 02:27:05 +00:00
|
|
|
pub fn send_message(message: Json<MessageInput<'_>>) -> JsonValue {
|
2021-07-18 14:34:27 +00:00
|
|
|
check_token(message)
|
2021-07-17 23:41:41 +00:00
|
|
|
}
|