fix: delete memberships when community is deleted from account deletion

This commit is contained in:
trisua 2025-05-09 22:59:19 -04:00
parent 3706764437
commit 9ce1161361
4 changed files with 28 additions and 13 deletions

View file

@ -208,14 +208,8 @@ impl DataManager {
self.cache_clear_user(&user).await;
// delete communities
let res = execute!(
&conn,
"DELETE FROM communities WHERE owner = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
for community in self.get_communities_by_owner(user.id).await? {
self.delete_community(community.id, &user).await?;
}
// delete memberships

View file

@ -176,6 +176,27 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all communities by their owner.
pub async fn get_communities_by_owner(&self, id: usize) -> Result<Vec<Community>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM communities WHERE owner = $1",
params![&(id as i64)],
|x| { Self::get_community_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("communities".to_string()));
}
Ok(res.unwrap())
}
/// Create a new community in the database.
///
/// # Arguments
@ -287,7 +308,7 @@ impl DataManager {
.await;
}
pub async fn delete_community(&self, id: usize, user: User) -> Result<()> {
pub async fn delete_community(&self, id: usize, user: &User) -> Result<()> {
let y = self.get_community_by_id(id).await?;
if user.id != y.owner {