add: channels, messages

This commit is contained in:
trisua 2025-04-27 23:11:37 -04:00
parent 67492cf73f
commit 7774124bd0
40 changed files with 2238 additions and 115 deletions

View file

@ -18,11 +18,17 @@ pub struct Channel {
///
/// Top (0) to bottom.
pub position: usize,
/// 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,
}
impl Channel {
/// Create a new [`Channel`].
pub fn new(community: usize, owner: usize, position: usize) -> Self {
pub fn new(community: usize, owner: usize, position: usize, title: String) -> Self {
Self {
id: AlmostSnowflake::new(1234567890)
.to_string()
@ -34,8 +40,32 @@ impl Channel {
minimum_role_read: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
minimum_role_write: (CommunityPermission::DEFAULT | CommunityPermission::MEMBER).bits(),
position,
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
}
(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)
}
}
#[derive(Serialize, Deserialize)]