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()));
}
}