tawny/src/model.rs

78 lines
2.2 KiB
Rust
Raw Normal View History

2025-08-23 22:07:14 -04:00
use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
2025-08-24 12:29:36 -04:00
#[derive(Serialize, Deserialize, PartialEq, Eq)]
2025-08-23 22:07:14 -04:00
pub struct GroupChatInfo {
pub name: String,
}
2025-08-24 12:29:36 -04:00
#[derive(Serialize, Deserialize, PartialEq, Eq)]
2025-08-23 22:07:14 -04:00
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(Serialize, Deserialize)]
pub struct Message {
pub id: usize,
pub created: usize,
2025-08-24 12:29:36 -04:00
pub edited: usize,
2025-08-23 22:07:14 -04:00
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 {
2025-08-24 12:29:36 -04:00
let created = unix_epoch_timestamp();
2025-08-23 22:07:14 -04:00
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
2025-08-24 12:29:36 -04:00
created,
edited: created,
2025-08-23 22:07:14 -04:00
owner,
chat,
content,
uploads,
}
}
}