Add different message types, and check for them
parent
a1c5fa42a7
commit
70f604e4f0
34
src/chat.rs
34
src/chat.rs
|
@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
|
|||
use std::sync::Mutex;
|
||||
use crate::file_io::db_read;
|
||||
use rocket::http::{Cookie, Cookies};
|
||||
use crate::message::{Message, MessageInput};
|
||||
use crate::message::{Message, MessageInput, MessageType};
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use chrono::prelude::*;
|
||||
use uuid::Uuid;
|
||||
|
@ -23,35 +23,23 @@ pub fn fetch_messages() -> Json<Vec<Message>> {
|
|||
|
||||
// Create full message object and write to file
|
||||
fn create_message(message: Json<MessageInput>, file: &str, user: &User) -> JsonValue {
|
||||
let event_type = match message.body.chars().nth(0).unwrap() {
|
||||
'/' => MessageType::Command,
|
||||
':' => MessageType::Emote,
|
||||
_ => MessageType::Normal,
|
||||
};
|
||||
|
||||
// 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.name.to_owned(),
|
||||
body: message.body.to_string(),
|
||||
created_at: date,
|
||||
event_type: event_type,
|
||||
created_at: Utc.timestamp(1_500_000_000, 0),
|
||||
};
|
||||
info!("created mesage: {:?}", message_obj);
|
||||
|
||||
// append message to file
|
||||
let mut messages = MESSAGES.lock().unwrap();
|
||||
messages.push(message_obj.to_owned());
|
||||
return json!({
|
||||
|
|
|
@ -7,12 +7,20 @@ use uuid::Uuid;
|
|||
pub struct MessageInput<'r> {
|
||||
pub name: &'r str,
|
||||
pub body: &'r str,
|
||||
pub date: &'r str,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum MessageType {
|
||||
Normal,
|
||||
Announcement,
|
||||
Emote,
|
||||
Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Message {
|
||||
pub id: Uuid,
|
||||
pub event_type: MessageType,
|
||||
pub user: String,
|
||||
pub body: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
|
|
Loading…
Reference in New Issue