Create full message object out of sent message
parent
bedb5ea974
commit
0302f816ec
66
src/chat.rs
66
src/chat.rs
|
@ -1,14 +1,51 @@
|
||||||
/* Contains Rocket code for chat/message functionality */
|
/* Contains Rocket code for chat/message functionality */
|
||||||
extern crate log;
|
extern crate log;
|
||||||
use crate::file_io::{read_json};
|
use crate::file_io::read_json;
|
||||||
use crate::message::{Message, MessageInput};
|
use crate::message::{Message, MessageInput};
|
||||||
use rocket_contrib::json::{Json, JsonValue};
|
use rocket_contrib::json::{Json, JsonValue};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use crate::user::User;
|
use crate::user::User;
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
};
|
||||||
|
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
|
// Check if user can create the message, and then create more info about the message
|
||||||
fn create_message(message: Json<MessageInput>, file: &str) -> JsonValue {
|
fn check_token(message: Json<MessageInput>) -> JsonValue {
|
||||||
// check if token is correct for name given
|
// check if token is correct for name given
|
||||||
let users: Vec<User> = read_json(); // create vector out of users in json file
|
let users: Vec<User> = read_json(); // create vector out of users in json file
|
||||||
|
|
||||||
|
@ -17,10 +54,7 @@ fn create_message(message: Json<MessageInput>, file: &str) -> JsonValue {
|
||||||
if i.name == message.name.to_lowercase() { // if it finds the user in the file
|
if i.name == message.name.to_lowercase() { // if it finds the user in the file
|
||||||
if i.session_token == message.token { // if token matches
|
if i.session_token == message.token { // if token matches
|
||||||
info!("user exists and given token matches");
|
info!("user exists and given token matches");
|
||||||
return json!({
|
return create_message(message, "messages.json", i);
|
||||||
"status": "ok",
|
|
||||||
"reason": "token matches"
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
warn!("token does not match!");
|
warn!("token does not match!");
|
||||||
return json!({
|
return json!({
|
||||||
|
@ -30,31 +64,15 @@ fn create_message(message: Json<MessageInput>, file: &str) -> JsonValue {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
warn!("user not found");
|
||||||
json!({
|
json!({
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"reason": "user not found"
|
"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> = 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
|
// Receive a basic message
|
||||||
#[post("/api/message/send", format = "json", data = "<message>")]
|
#[post("/api/message/send", format = "json", data = "<message>")]
|
||||||
pub fn send_message(message: Json<MessageInput<'_>>) -> JsonValue {
|
pub fn send_message(message: Json<MessageInput<'_>>) -> JsonValue {
|
||||||
create_message(message, "messages.json")
|
check_token(message)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue