add: individual topic write permissions

This commit is contained in:
trisua 2025-08-04 13:23:27 -04:00
parent 3738a5cd1f
commit 8c779b2f2e
6 changed files with 80 additions and 9 deletions

View file

@ -1783,6 +1783,28 @@ impl DataManager {
}
}
/// Check if the given `uid` can post in the given `community` with the given `access`.
pub async fn check_can_post_with_access(
&self,
community: &Community,
access: &CommunityWriteAccess,
uid: usize,
) -> bool {
match *access {
CommunityWriteAccess::Owner => uid == community.owner,
CommunityWriteAccess::Joined => {
match self
.get_membership_by_owner_community(uid, community.id)
.await
{
Ok(m) => m.role.check_member(),
Err(_) => false,
}
}
_ => true,
}
}
/// Create a new post in the database.
///
/// # Arguments
@ -1840,7 +1862,15 @@ impl DataManager {
));
}
if community.topics.get(&data.topic).is_none() {
if let Some(topic) = community.topics.get(&data.topic) {
// check permission
if !self
.check_can_post_with_access(&community, &topic.write_access, data.owner)
.await
{
return Err(Error::NotAllowed);
}
} else {
return Err(Error::GeneralNotFound("topic".to_string()));
}
}

View file

@ -541,6 +541,8 @@ pub struct ForumTopic {
pub description: String,
pub color: String,
pub position: i32,
#[serde(default)]
pub write_access: CommunityWriteAccess,
}
impl ForumTopic {
@ -549,7 +551,13 @@ impl ForumTopic {
/// # Returns
/// * ID for [`Community`] hashmap
/// * [`ForumTopic`]
pub fn new(title: String, description: String, color: String, position: i32) -> (usize, Self) {
pub fn new(
title: String,
description: String,
color: String,
position: i32,
write_access: CommunityWriteAccess,
) -> (usize, Self) {
(
Snowflake::new().to_string().parse::<usize>().unwrap(),
Self {
@ -557,6 +565,7 @@ impl ForumTopic {
description,
color,
position,
write_access,
},
)
}