From e95022e994a053bbaf7dfb58cd1a1aa81df1a99f Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Sat, 17 Jul 2021 22:27:05 -0400 Subject: [PATCH] Add more messaging funcionality --- README.md | 12 +++++++++++- src/chat.rs | 33 ++++++++++++++++++++++++++++++--- src/message.rs | 11 +++++++++-- src/user.rs | 2 +- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 73e5848..90baca3 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,18 @@ Whenever user sends a message, client will send message & token and backend will - [x] Basic auth api - [ ] Basic messaging system + - [ ] Finish up `chat::create_message()` + - [ ] Create `chat::read_messages()` + - [ ] Create `chat::delete_message()` + - [ ] Different types of message events? eg. default, announcement, command + - [ ] Emote support? - [ ] Token generation & storage + - [ ] API to refresh token + - [ ] Store token in json + - [ ] API to check token? - [x] Pronouns -- [ ] Change pronouns + - [ ] Change pronouns + - [ ] Multiple sets of pronouns +- [ ] Some form of plural support? - [ ] User management (banning, etc.) - [ ] Blacklist words from chat/names diff --git a/src/chat.rs b/src/chat.rs index 7629ae0..05d23c7 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1,12 +1,39 @@ /* Contains Rocket code for chat/message functionality */ extern crate log; -use crate::message::Message; +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; +// Check if user can create the message, and then create more info about the message +fn create_message(message: Json, file: &str, token: &str) { + // check if token is correct for name given + // 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 { +pub fn send_message(message: Json>) -> JsonValue { + create_message(message, "messages.json", "token"); json!({ "status": "ok", - "reason": "bruh" + "reason": "" }) } diff --git a/src/message.rs b/src/message.rs index 45179ac..ae6502e 100644 --- a/src/message.rs +++ b/src/message.rs @@ -4,9 +4,16 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; #[derive(Deserialize, Serialize)] -pub struct Message<'r> { +pub struct MessageInput<'r> { + pub name: &'r str, + pub body: &'r str, + pub date: &'r str, +} + +#[derive(Debug)] +pub struct Message { pub id: Uuid, pub user: User, - pub body: &'r str, + pub body: String, pub created_at: DateTime, } diff --git a/src/user.rs b/src/user.rs index ad2aff9..915bd4a 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; // Struct to store basic user data -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize, Debug)] pub struct User { pub name: String, pub pin_hashed: String,