add: delete uploads and polls when user is deleted

This commit is contained in:
trisua 2025-06-07 21:18:28 -04:00
parent f6eb46e7e8
commit 40fce4bc77
6 changed files with 66 additions and 5 deletions

View file

@ -1 +1,2 @@
* me@trisua.com # * me@trisua.com
* @t

View file

@ -416,6 +416,16 @@ impl DataManager {
remove_file(banner).unwrap(); remove_file(banner).unwrap();
} }
// delete uploads
for upload in self.get_uploads_by_owner_all(user.id).await? {
self.delete_upload(upload.id).await?;
}
// delete polls
for poll in self.get_polls_by_owner_all(user.id).await? {
self.delete_poll(poll.id, &user).await?;
}
// ... // ...
Ok(()) Ok(())
} }

View file

@ -129,7 +129,9 @@ impl DataManager {
} }
// incr notification count // incr notification count
self.incr_user_notifications(data.owner).await.unwrap(); if let Err(e) = self.incr_user_notifications(data.owner).await {
return Err(e);
};
// post event // post event
let mut con = self.2.get_con().await; let mut con = self.2.get_con().await;

View file

@ -3,7 +3,7 @@ use crate::cache::Cache;
use crate::model::communities::Poll; use crate::model::communities::Poll;
use crate::model::moderation::AuditLogEntry; use crate::model::moderation::AuditLogEntry;
use crate::model::{Error, Result, auth::User, permissions::FinePermission}; use crate::model::{Error, Result, auth::User, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row, params}; use crate::{auto_method, execute, get, params, query_row, query_rows};
#[cfg(feature = "sqlite")] #[cfg(feature = "sqlite")]
use rusqlite::Row; use rusqlite::Row;
@ -35,6 +35,30 @@ impl DataManager {
auto_method!(get_poll_by_id()@get_poll_from_row -> "SELECT * FROM polls WHERE id = $1" --name="poll" --returns=Poll --cache-key-tmpl="atto.poll:{}"); auto_method!(get_poll_by_id()@get_poll_from_row -> "SELECT * FROM polls WHERE id = $1" --name="poll" --returns=Poll --cache-key-tmpl="atto.poll:{}");
/// Get all polls by their owner.
///
/// # Arguments
/// * `owner` - the ID of the owner of the polls
pub async fn get_polls_by_owner_all(&self, owner: usize) -> Result<Vec<Poll>> {
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 polls WHERE owner = $1 ORDER BY created DESC",
&[&(owner as i64)],
|x| { Self::get_poll_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("poll".to_string()));
}
Ok(res.unwrap())
}
/// Create a new poll in the database. /// Create a new poll in the database.
/// ///
/// # Arguments /// # Arguments
@ -93,7 +117,7 @@ impl DataManager {
Ok(data.id) Ok(data.id)
} }
pub async fn delete_poll(&self, id: usize, user: User) -> Result<()> { pub async fn delete_poll(&self, id: usize, user: &User) -> Result<()> {
let y = self.get_poll_by_id(id).await?; let y = self.get_poll_by_id(id).await?;
if user.id != y.owner { if user.id != y.owner {

View file

@ -1547,7 +1547,7 @@ impl DataManager {
// remove poll // remove poll
if y.poll_id != 0 { if y.poll_id != 0 {
self.delete_poll(y.poll_id, user).await?; self.delete_poll(y.poll_id, &user).await?;
} }
// return // return

View file

@ -83,6 +83,30 @@ impl DataManager {
Ok(res.unwrap()) Ok(res.unwrap())
} }
/// Get all uploads by their owner.
///
/// # Arguments
/// * `owner` - the ID of the owner of the upload
pub async fn get_uploads_by_owner_all(&self, owner: usize) -> Result<Vec<MediaUpload>> {
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 uploads WHERE owner = $1 ORDER BY created DESC",
&[&(owner as i64)],
|x| { Self::get_upload_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("upload".to_string()));
}
Ok(res.unwrap())
}
/// Create a new upload in the database. /// Create a new upload in the database.
/// ///
/// # Arguments /// # Arguments