tetratto_core/model/
channels.rs

1use std::collections::HashMap;
2
3use serde::{Serialize, Deserialize};
4use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
5
6use super::communities_permissions::CommunityPermission;
7
8/// A channel is a more "chat-like" feed in communities.
9#[derive(Clone, Serialize, Deserialize)]
10pub struct Channel {
11    pub id: usize,
12    pub community: usize,
13    pub owner: usize,
14    pub created: usize,
15    /// The minimum role (as bits) that can read this channel.
16    pub minimum_role_read: u32,
17    /// The minimum role (as bits) that can write to this channel.
18    pub minimum_role_write: u32,
19    /// The position of this channel in the UI.
20    ///
21    /// Top (0) to bottom.
22    pub position: usize,
23    /// The members of the chat (ids). Should be empty if `community > 0`.
24    ///
25    /// The owner should not be a member of the channel since any member can update members.
26    pub members: Vec<usize>,
27    /// The title of the channel.
28    pub title: String,
29    /// The timestamp of the last message in the channel.
30    pub last_message: usize,
31}
32
33impl Channel {
34    /// Create a new [`Channel`].
35    pub fn new(community: usize, owner: usize, position: usize, title: String) -> Self {
36        let created = unix_epoch_timestamp();
37
38        Self {
39            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
40            community,
41            owner,
42            created,
43            minimum_role_read: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
44            minimum_role_write: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
45            position,
46            members: Vec::new(),
47            title,
48            last_message: created,
49        }
50    }
51
52    /// Check if the given `uid` can post in the channel.
53    pub fn check_post(&self, uid: usize, membership: Option<CommunityPermission>) -> bool {
54        let mut is_member = false;
55
56        if let Some(membership) = membership {
57            is_member = membership.bits() >= self.minimum_role_write
58        }
59
60        (uid == self.owner) | is_member | self.members.contains(&uid)
61    }
62
63    /// Check if the given `uid` can post in the channel.
64    pub fn check_read(&self, uid: usize, membership: Option<CommunityPermission>) -> bool {
65        let mut is_member = false;
66
67        if let Some(membership) = membership {
68            is_member = membership.bits() >= self.minimum_role_read
69        }
70
71        (uid == self.owner) | is_member | self.members.contains(&uid)
72    }
73}
74
75#[derive(Clone, Serialize, Deserialize)]
76pub struct Message {
77    pub id: usize,
78    pub channel: usize,
79    pub owner: usize,
80    pub created: usize,
81    pub edited: usize,
82    pub content: String,
83    pub context: MessageContext,
84    pub reactions: HashMap<String, usize>,
85}
86
87impl Message {
88    pub fn new(channel: usize, owner: usize, content: String) -> Self {
89        let now = unix_epoch_timestamp();
90
91        Self {
92            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
93            channel,
94            owner,
95            created: now,
96            edited: now,
97            content,
98            context: MessageContext,
99            reactions: HashMap::new(),
100        }
101    }
102}
103
104#[derive(Clone, Serialize, Deserialize)]
105pub struct MessageContext;
106
107impl Default for MessageContext {
108    fn default() -> Self {
109        Self
110    }
111}
112
113#[derive(Clone, Serialize, Deserialize)]
114pub struct MessageReaction {
115    pub id: usize,
116    pub created: usize,
117    pub owner: usize,
118    pub message: usize,
119    pub emoji: String,
120}
121
122impl MessageReaction {
123    /// Create a new [`MessageReaction`].
124    pub fn new(owner: usize, message: usize, emoji: String) -> Self {
125        Self {
126            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
127            created: unix_epoch_timestamp(),
128            owner,
129            message,
130            emoji,
131        }
132    }
133}