2025-04-24 19:06:30 -04:00
|
|
|
use serde::{Serialize, Deserialize};
|
2025-05-06 16:13:48 -04:00
|
|
|
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
2025-04-24 19:06:30 -04:00
|
|
|
|
|
|
|
use super::communities_permissions::CommunityPermission;
|
|
|
|
|
|
|
|
/// A channel is a more "chat-like" feed in communities.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Channel {
|
|
|
|
pub id: usize,
|
|
|
|
pub community: usize,
|
|
|
|
pub owner: usize,
|
|
|
|
pub created: usize,
|
|
|
|
/// The minimum role (as bits) that can read this channel.
|
|
|
|
pub minimum_role_read: u32,
|
|
|
|
/// The minimum role (as bits) that can write to this channel.
|
|
|
|
pub minimum_role_write: u32,
|
|
|
|
/// The position of this channel in the UI.
|
|
|
|
///
|
|
|
|
/// Top (0) to bottom.
|
2025-04-25 16:22:38 -04:00
|
|
|
pub position: usize,
|
2025-04-27 23:11:37 -04:00
|
|
|
/// The members of the chat (ids). Should be empty if `community > 0`.
|
|
|
|
///
|
|
|
|
/// The owner should not be a member of the channel since any member can update members.
|
|
|
|
pub members: Vec<usize>,
|
|
|
|
/// The title of the channel.
|
|
|
|
pub title: String,
|
2025-04-24 19:06:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Channel {
|
|
|
|
/// Create a new [`Channel`].
|
2025-04-27 23:11:37 -04:00
|
|
|
pub fn new(community: usize, owner: usize, position: usize, title: String) -> Self {
|
2025-04-24 19:06:30 -04:00
|
|
|
Self {
|
2025-05-06 16:13:48 -04:00
|
|
|
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
2025-04-24 19:06:30 -04:00
|
|
|
community,
|
|
|
|
owner,
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
minimum_role_read: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
|
|
|
|
minimum_role_write: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
|
2025-04-25 16:22:38 -04:00
|
|
|
position,
|
2025-04-27 23:11:37 -04:00
|
|
|
members: Vec::new(),
|
|
|
|
title,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the given `uid` can post in the channel.
|
|
|
|
pub fn check_post(&self, uid: usize, membership: Option<CommunityPermission>) -> bool {
|
|
|
|
let mut is_member = false;
|
|
|
|
|
|
|
|
if let Some(membership) = membership {
|
|
|
|
is_member = membership.bits() >= self.minimum_role_write
|
2025-04-24 19:06:30 -04:00
|
|
|
}
|
2025-04-27 23:11:37 -04:00
|
|
|
|
|
|
|
(uid == self.owner) | is_member | self.members.contains(&uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the given `uid` can post in the channel.
|
|
|
|
pub fn check_read(&self, uid: usize, membership: Option<CommunityPermission>) -> bool {
|
|
|
|
let mut is_member = false;
|
|
|
|
|
|
|
|
if let Some(membership) = membership {
|
|
|
|
is_member = membership.bits() >= self.minimum_role_read
|
|
|
|
}
|
|
|
|
|
|
|
|
(uid == self.owner) | is_member | self.members.contains(&uid)
|
2025-04-24 19:06:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-01 16:43:58 -04:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2025-04-24 19:06:30 -04:00
|
|
|
pub struct Message {
|
|
|
|
pub id: usize,
|
|
|
|
pub channel: usize,
|
|
|
|
pub owner: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub edited: usize,
|
|
|
|
pub content: String,
|
|
|
|
pub context: MessageContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
pub fn new(channel: usize, owner: usize, content: String) -> Self {
|
|
|
|
let now = unix_epoch_timestamp() as usize;
|
|
|
|
|
|
|
|
Self {
|
2025-05-06 16:13:48 -04:00
|
|
|
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
2025-04-24 19:06:30 -04:00
|
|
|
channel,
|
|
|
|
owner,
|
|
|
|
created: now,
|
|
|
|
edited: now,
|
|
|
|
content,
|
2025-04-29 16:53:34 -04:00
|
|
|
context: MessageContext,
|
2025-04-24 19:06:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-01 16:43:58 -04:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2025-04-24 19:06:30 -04:00
|
|
|
pub struct MessageContext;
|
|
|
|
|
|
|
|
impl Default for MessageContext {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|