use serde::{Deserialize, Serialize}; use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp}; #[derive(Serialize, Deserialize, PartialEq, Eq)] pub struct GroupChatInfo { pub name: String, } #[derive(Serialize, Deserialize, PartialEq, Eq)] pub enum ChatStyle { /// Direct messages between two users. Direct, /// Messages between a group of users (up to 10). Group(GroupChatInfo), } #[derive(Serialize, Deserialize)] pub struct Chat { pub id: usize, pub created: usize, pub style: ChatStyle, /// When the last member of the chat leaves, the chat will be deleted. pub members: Vec, pub last_message_created: usize, /// The IDs of each user in the chat who read the last message. /// /// Will always have the ID of the user who sent the last message as index 0. /// /// The UI should show two checkmarks once this vector has a length of at least 2. /// /// Read receipts are stored by chat instead of message since it's easier to /// keep up with if we store by chat instead. This will also declutter /// the UI and prevent every message showing a read receipt. pub last_message_read_by: Vec, } impl Chat { /// Create a new [`Chat`]. pub fn new(style: ChatStyle, members: Vec) -> Self { Self { id: Snowflake::new().to_string().parse::().unwrap(), created: unix_epoch_timestamp(), style, members, last_message_created: 0, last_message_read_by: Vec::new(), } } } #[derive(Serialize, Deserialize)] pub struct Message { pub id: usize, pub created: usize, pub edited: usize, pub owner: usize, pub chat: usize, pub content: String, pub uploads: Vec, } impl Message { /// Create a new [`Message`]. pub fn new(owner: usize, chat: usize, content: String, uploads: Vec) -> Self { let created = unix_epoch_timestamp(); Self { id: Snowflake::new().to_string().parse::().unwrap(), created, edited: created, owner, chat, content, uploads, } } }