add: user account warnings

This commit is contained in:
trisua 2025-04-11 22:12:43 -04:00
parent 535a854a47
commit 5995aaf31c
16 changed files with 459 additions and 1 deletions

View file

@ -24,6 +24,7 @@ impl DataManager {
execute!(&conn, common::CREATE_TABLE_IPBANS).unwrap();
execute!(&conn, common::CREATE_TABLE_AUDIT_LOG).unwrap();
execute!(&conn, common::CREATE_TABLE_REPORTS).unwrap();
execute!(&conn, common::CREATE_TABLE_USER_WARNINGS).unwrap();
Ok(())
}

View file

@ -9,3 +9,4 @@ pub const CREATE_TABLE_USERBLOCKS: &str = include_str!("./sql/create_userblocks.
pub const CREATE_TABLE_IPBANS: &str = include_str!("./sql/create_ipbans.sql");
pub const CREATE_TABLE_AUDIT_LOG: &str = include_str!("./sql/create_audit_log.sql");
pub const CREATE_TABLE_REPORTS: &str = include_str!("./sql/create_reports.sql");
pub const CREATE_TABLE_USER_WARNINGS: &str = include_str!("./sql/create_user_warnings.sql");

View file

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS user_warnings (
id BIGINT NOT NULL PRIMARY KEY,
created BIGINT NOT NULL,
receiver BIGINT NOT NULL,
moderator BIGINT NOT NULL,
content TEXT NOT NULL
)

View file

@ -9,6 +9,7 @@ mod notifications;
mod posts;
mod reactions;
mod reports;
mod user_warnings;
mod userblocks;
mod userfollows;

View file

@ -0,0 +1,150 @@
use super::*;
use crate::cache::Cache;
use crate::model::auth::{Notification, UserWarning};
use crate::model::moderation::AuditLogEntry;
use crate::model::{Error, Result, auth::User, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row, query_rows, params};
#[cfg(feature = "sqlite")]
use rusqlite::Row;
#[cfg(feature = "postgres")]
use tokio_postgres::Row;
impl DataManager {
/// Get a [`UserWarning`] from an SQL row.
pub(crate) fn get_user_warning_from_row(
#[cfg(feature = "sqlite")] x: &Row<'_>,
#[cfg(feature = "postgres")] x: &Row,
) -> UserWarning {
UserWarning {
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
receiver: get!(x->2(i64)) as usize,
moderator: get!(x->3(i64)) as usize,
content: get!(x->4(String)),
}
}
auto_method!(get_user_warning_by_ip(&str)@get_user_warning_from_row -> "SELECT * FROM user_warning WHERE ip = $1" --name="user warning" --returns=UserWarning --cache-key-tmpl="atto.user_warning:{}");
/// Get all user warnings by user (paginated).
///
/// # Arguments
/// * `user` - the ID of the user to fetch warnings for
/// * `batch` - the limit of items in each page
/// * `page` - the page number
pub async fn get_user_warnings_by_user(
&self,
user: usize,
batch: usize,
page: usize,
) -> Result<Vec<UserWarning>> {
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 user_warnings WHERE receiver = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(user as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_user_warning_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("user warning".to_string()));
}
Ok(res.unwrap())
}
/// Create a new user warning in the database.
///
/// # Arguments
/// * `data` - a mock [`UserWarning`] object to insert
pub async fn create_user_warning(&self, data: UserWarning) -> Result<()> {
let user = self.get_user_by_id(data.moderator).await?;
// ONLY moderators can create warnings
if !user.permissions.check(FinePermission::MANAGE_WARNINGS) {
return Err(Error::NotAllowed);
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"INSERT INTO user_warnings VALUES ($1, $2, $3, $4, $5)",
params![
&(data.id as i64),
&(data.created as i64),
&(data.receiver as i64),
&(data.moderator as i64),
&data.content
]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// create audit log entry
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!(
"invoked `create_user_warning` with x value `{}`",
data.receiver
),
))
.await?;
// send notification
self.create_notification(Notification::new(
"You have received a new account warning.".to_string(),
data.content,
data.receiver,
))
.await?;
// return
Ok(())
}
pub async fn delete_user_warning(&self, id: usize, user: User) -> Result<()> {
// ONLY moderators can manage warnings
if !user.permissions.check(FinePermission::MANAGE_WARNINGS) {
return Err(Error::NotAllowed);
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"DELETE FROM user_warnings WHERE id = $1",
&[&(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!("atto.user_warning:{}", id)).await;
// create audit log entry
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `delete_user_warning` with x value `{id}`"),
))
.await?;
// return
Ok(())
}
}