tawny/src/model.rs
2025-08-24 17:58:39 -04:00

103 lines
2.7 KiB
Rust

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<usize>,
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<usize>,
}
impl Chat {
/// Create a new [`Chat`].
pub fn new(style: ChatStyle, members: Vec<usize>) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
style,
members,
last_message_created: 0,
last_message_read_by: Vec::new(),
}
}
}
#[derive(Clone, 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<usize>,
}
impl Message {
/// Create a new [`Message`].
pub fn new(owner: usize, chat: usize, content: String, uploads: Vec<usize>) -> Self {
let created = unix_epoch_timestamp();
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created,
edited: created,
owner,
chat,
content,
uploads,
}
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum SocketMethod {
/// A message creation event.
MessageCreate,
/// A message deletion event.
MessageDelete,
/// A message update event.
MessageUpdate,
/// A chat update event.
ChatUpdate,
/// Simple ping.
Ping,
}
#[derive(Serialize, Deserialize)]
pub struct SocketMessage {
pub method: SocketMethod,
pub body: String,
}
impl SocketMessage {
pub fn to_string(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}