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

@ -3,7 +3,7 @@ use crate::cache::Cache;
use crate::model::communities::Poll;
use crate::model::moderation::AuditLogEntry;
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")]
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:{}");
/// 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.
///
/// # Arguments
@ -93,7 +117,7 @@ impl DataManager {
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?;
if user.id != y.owner {