Skip to main content

tetratto_core/model/
stacks.rs

1use serde::{Serialize, Deserialize};
2use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
3
4#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
5#[derive(Default)]
6pub enum StackPrivacy {
7    /// Can be viewed by anyone.
8    Public,
9    /// Can only be viewed by the stack's owner (and users with `MANAGE_STACKS`).
10    #[default]
11    Private,
12}
13
14
15#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
16#[derive(Default)]
17pub enum StackMode {
18    /// `users` vec contains ID of users to INCLUDE into the timeline;
19    /// every other user is excluded
20    #[default]
21    Include,
22    /// `users` vec contains ID of users to EXCLUDE from the timeline;
23    /// every other user is included
24    Exclude,
25    /// `users` vec contains ID of users to show in a user listing on the stack's
26    /// page (instead of a timeline).
27    ///
28    /// Other users can block the entire list (creating a `StackBlock`, not a `UserBlock`).
29    BlockList,
30    /// `users` vec contains ID of users who are allowed to view posts posted to the stack.
31    Circle,
32}
33
34
35#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
36#[derive(Default)]
37pub enum StackSort {
38    #[default]
39    Created,
40    Likes,
41}
42
43
44#[derive(Clone, Debug, Serialize, Deserialize)]
45pub struct UserStack {
46    pub id: usize,
47    pub created: usize,
48    pub owner: usize,
49    pub name: String,
50    pub users: Vec<usize>,
51    pub privacy: StackPrivacy,
52    pub mode: StackMode,
53    pub sort: StackSort,
54    /// Locked stacks cannot be deleted or have their mode changed. Stacks cannot
55    /// be locked after creation, and must be locked by the server.
56    pub is_locked: bool,
57}
58
59impl UserStack {
60    /// Create a new [`UserStack`].
61    pub fn new(name: String, owner: usize, users: Vec<usize>) -> Self {
62        Self {
63            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
64            created: unix_epoch_timestamp(),
65            owner,
66            name,
67            users,
68            privacy: StackPrivacy::default(),
69            mode: StackMode::default(),
70            sort: StackSort::default(),
71            is_locked: false,
72        }
73    }
74}
75
76#[derive(Clone, Debug, Serialize, Deserialize)]
77pub struct StackBlock {
78    pub id: usize,
79    pub created: usize,
80    pub initiator: usize,
81    pub stack: usize,
82}
83
84impl StackBlock {
85    /// Create a new [`StackBlock`].
86    pub fn new(initiator: usize, stack: usize) -> Self {
87        Self {
88            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
89            created: unix_epoch_timestamp(),
90            initiator,
91            stack,
92        }
93    }
94}