WIP: Audit Logs #45
4 changed files with 38 additions and 35 deletions
|
@ -9,7 +9,11 @@ use axum::{
|
|||
};
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::CurrentUser, error::Error, objects::{AuditLog, Member, PaginationRequest, Permissions}, utils::global_checks, AppState
|
||||
AppState,
|
||||
api::v1::auth::CurrentUser,
|
||||
error::Error,
|
||||
objects::{AuditLog, Member, PaginationRequest, Permissions},
|
||||
utils::global_checks,
|
||||
};
|
||||
|
||||
pub async fn get(
|
||||
|
@ -23,15 +27,11 @@ pub async fn get(
|
|||
global_checks(&mut conn, &app_state.config, uuid).await?;
|
||||
|
||||
let caller = Member::check_membership(&mut conn, uuid, guild_uuid).await?;
|
||||
caller.check_permission(&mut conn, &app_state.cache_pool, Permissions::ManageGuild).await?;
|
||||
|
||||
caller
|
||||
.check_permission(&mut conn, &app_state.cache_pool, Permissions::ManageGuild)
|
||||
.await?;
|
||||
|
||||
let logs = AuditLog::fetch_page(
|
||||
&mut conn,
|
||||
guild_uuid,
|
||||
pagination,
|
||||
)
|
||||
.await?;
|
||||
let logs = AuditLog::fetch_page(&mut conn, guild_uuid, pagination).await?;
|
||||
|
||||
Ok((StatusCode::OK, Json(logs)))
|
||||
}
|
||||
|
|
|
@ -12,12 +12,12 @@ use axum::{
|
|||
use bytes::Bytes;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod auditlogs;
|
||||
mod bans;
|
||||
mod channels;
|
||||
mod invites;
|
||||
mod members;
|
||||
mod roles;
|
||||
mod auditlogs;
|
||||
|
||||
use crate::{
|
||||
AppState,
|
||||
|
|
|
@ -1,29 +1,33 @@
|
|||
use uuid::Uuid;
|
||||
use diesel::{insert_into, Insertable, QueryDsl, Queryable, Selectable, SelectableHelper, ExpressionMethods};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::{error::Error, objects::{load_or_empty, Pagination, PaginationRequest}, schema::audit_logs, Conn};
|
||||
use crate::{
|
||||
Conn,
|
||||
error::Error,
|
||||
objects::{Pagination, PaginationRequest, load_or_empty},
|
||||
schema::audit_logs,
|
||||
};
|
||||
use diesel::{
|
||||
ExpressionMethods, Insertable, QueryDsl, Queryable, Selectable, SelectableHelper, insert_into,
|
||||
};
|
||||
use diesel_async::RunQueryDsl;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Insertable, Selectable, Queryable, Serialize, Deserialize, Clone)]
|
||||
|
||||
#[diesel(table_name = audit_logs)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct AuditLog {
|
||||
|
||||
pub uuid: Uuid,
|
||||
pub guild_uuid: Uuid,
|
||||
pub action_id: i16,
|
||||
pub by_uuid: Uuid,
|
||||
pub channel_uuid: Option<Uuid>,
|
||||
pub user_uuid: Option<Uuid>,
|
||||
pub message_uuid: Option<Uuid>,
|
||||
pub role_uuid: Option<Uuid>,
|
||||
pub audit_message: Option<String>,
|
||||
pub changed_from: Option<String>,
|
||||
pub changed_to: Option<String>,
|
||||
pub uuid: Uuid,
|
||||
pub guild_uuid: Uuid,
|
||||
pub action_id: i16,
|
||||
pub by_uuid: Uuid,
|
||||
pub channel_uuid: Option<Uuid>,
|
||||
pub user_uuid: Option<Uuid>,
|
||||
pub message_uuid: Option<Uuid>,
|
||||
pub role_uuid: Option<Uuid>,
|
||||
pub audit_message: Option<String>,
|
||||
pub changed_from: Option<String>,
|
||||
pub changed_to: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
impl AuditLog {
|
||||
pub async fn count(conn: &mut Conn, guild_uuid: Uuid) -> Result<i64, Error> {
|
||||
use audit_logs::dsl;
|
||||
|
@ -40,7 +44,6 @@ impl AuditLog {
|
|||
guild_uuid: Uuid,
|
||||
pagination: PaginationRequest,
|
||||
) -> Result<Pagination<AuditLog>, Error> {
|
||||
|
||||
// TODO: Maybe add cache, but I do not know how
|
||||
let per_page = pagination.per_page.unwrap_or(20);
|
||||
let offset = (pagination.page - 1) * per_page;
|
||||
|
@ -59,7 +62,7 @@ impl AuditLog {
|
|||
.offset(offset as i64)
|
||||
.select(AuditLog::as_select())
|
||||
.load(conn)
|
||||
.await
|
||||
.await,
|
||||
)?;
|
||||
|
||||
let pages = (AuditLog::count(conn, guild_uuid).await? as f32 / per_page as f32).ceil();
|
||||
|
@ -70,7 +73,7 @@ impl AuditLog {
|
|||
pages: pages as i32,
|
||||
page: pagination.page,
|
||||
};
|
||||
|
||||
|
||||
Ok(paginated_logs)
|
||||
}
|
||||
|
||||
|
@ -87,7 +90,7 @@ impl AuditLog {
|
|||
audit_message: Option<String>,
|
||||
changed_from: Option<String>,
|
||||
changed_to: Option<String>,
|
||||
) ->Result<(), Error> {
|
||||
) -> Result<(), Error> {
|
||||
let audit_log = AuditLog {
|
||||
uuid: Uuid::now_v7(),
|
||||
guild_uuid,
|
||||
|
@ -99,7 +102,7 @@ impl AuditLog {
|
|||
role_uuid,
|
||||
audit_message,
|
||||
changed_from,
|
||||
changed_to
|
||||
changed_to,
|
||||
};
|
||||
|
||||
insert_into(audit_logs::table)
|
||||
|
|
|
@ -7,6 +7,7 @@ use log::debug;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
mod auditlog;
|
||||
mod bans;
|
||||
mod channel;
|
||||
mod email_token;
|
||||
|
@ -19,8 +20,8 @@ mod message;
|
|||
mod password_reset_token;
|
||||
mod role;
|
||||
mod user;
|
||||
mod auditlog;
|
||||
|
||||
pub use auditlog::AuditLog;
|
||||
pub use bans::GuildBan;
|
||||
pub use channel::Channel;
|
||||
pub use email_token::EmailToken;
|
||||
|
@ -35,7 +36,6 @@ pub use password_reset_token::PasswordResetToken;
|
|||
pub use role::Permissions;
|
||||
pub use role::Role;
|
||||
pub use user::User;
|
||||
pub use auditlog::AuditLog;
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue
If we're gonna use
OBJECT_ACTION
format (e.g.MEMBER_BAN
) for the WebSocket events, should we use it for audit logs as well instead of using integers?