add: ability to transfer community ownership

This commit is contained in:
trisua 2025-04-24 16:57:25 -04:00
parent d42375441f
commit 0c814e95d7
5 changed files with 139 additions and 2 deletions

View file

@ -397,6 +397,68 @@ impl DataManager {
Ok(())
}
pub async fn update_community_owner(
&self,
id: usize,
user: User,
new_owner: usize,
) -> Result<()> {
let y = self.get_community_by_id(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::MANAGE_COMMUNITIES) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(crate::model::moderation::AuditLogEntry::new(
user.id,
format!("invoked `update_community_owner` with x value `{id}`"),
))
.await?
}
}
let new_owner_membership = self
.get_membership_by_owner_community(new_owner, y.id)
.await?;
let current_owner_membership = self
.get_membership_by_owner_community(y.owner, y.id)
.await?;
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE communities SET owner = $1 WHERE id = $2",
params![&(new_owner as i64), &(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.cache_clear_community(&y).await;
// update memberships
self.update_membership_role(
new_owner_membership.id,
CommunityPermission::DEFAULT | CommunityPermission::ADMINISTRATOR,
)
.await?;
self.update_membership_role(
current_owner_membership.id,
CommunityPermission::DEFAULT | CommunityPermission::MEMBER,
)
.await?;
// return
Ok(())
}
auto_method!(update_community_context(CommunityContext)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET context = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
auto_method!(update_community_read_access(CommunityReadAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
auto_method!(update_community_write_access(CommunityWriteAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);