tetratto_core/model/
mail.rs

1use serde::{Serialize, Deserialize};
2use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
3use crate::model::auth::User;
4
5/// A letter is the most basic structure of the mail system. Letters are sent
6/// and received by users.
7#[derive(Serialize, Deserialize)]
8pub struct Letter {
9    pub id: usize,
10    pub created: usize,
11    pub owner: usize,
12    pub receivers: Vec<usize>,
13    pub subject: String,
14    pub content: String,
15    /// The ID of every use who has read the letter. Can be checked in the UI
16    /// with `user.id in letter.read_by`.
17    ///
18    /// This field can be updated by anyone in the letter's `receivers` field.
19    /// Other fields in the letter can only be updated by the letter's `owner`.
20    pub read_by: Vec<usize>,
21    /// The ID of the letter this letter is replying to.
22    pub replying_to: usize,
23    pub likes: isize,
24    pub dislikes: isize,
25}
26
27impl Letter {
28    /// Create a new [`Letter`].
29    pub fn new(
30        owner: usize,
31        receivers: Vec<usize>,
32        subject: String,
33        content: String,
34        replying_to: usize,
35    ) -> Self {
36        Self {
37            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
38            created: unix_epoch_timestamp(),
39            owner,
40            receivers,
41            subject,
42            content,
43            read_by: Vec::new(),
44            replying_to,
45            likes: 0,
46            dislikes: 0,
47        }
48    }
49
50    /// Check if the given user can read the letter.
51    pub fn can_read(&self, user: &User) -> bool {
52        (user.id == self.owner) | self.receivers.contains(&user.id)
53    }
54}