use common::{ constant::LOG_PAGE_LIMIT, model::{AdminAction, AdminActionKind, AdminLogEntry, Id, LogAction, LogEntry}, time::now_ms, }; use super::{Connection, all, community, exec, user}; use crate::error::ServerError; pub async fn record( community_id: Id, actor_id: Option, target_id: Option, action: &LogAction, db: &Connection, ) -> Result<(), ServerError> { exec( db, "INSERT INTO community_log (community_id, created_at, actor_id, target_id, action) VALUES (?1, ?2, ?3, ?4, ?5)", (community_id, now_ms(), actor_id, target_id, bitcode::encode(action)), ) .await } pub async fn list(community_id: Id, db: &Connection) -> Result, ServerError> { let raws = all( db, "SELECT id, created_at, actor_id, target_id, action FROM community_log WHERE community_id = ?1 ORDER BY id DESC LIMIT ?2", (community_id, LOG_PAGE_LIMIT), |r| { Ok(( r.get::()?, r.get::()?, r.get::>()?, r.get::>()?, r.blob::()?, )) }, ) .await?; let mut entries = Vec::new(); for (id, created_at, actor_id, target_id, action) in raws { entries.push(LogEntry { id, created_at, actor: user::read_optional(actor_id, db).await?, target: user::read_optional(target_id, db).await?, action, }); } Ok(entries) } pub async fn record_admin( actor_id: Option, target_id: Option, community: Option<&str>, action: &AdminAction, db: &Connection, ) -> Result<(), ServerError> { exec( db, "INSERT INTO admin_log (created_at, actor_id, target_id, community, kind, action) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", ( now_ms(), actor_id, target_id, community, action.kind().as_str(), bitcode::encode(action), ), ) .await } pub async fn list_admin( actor_id: Option, target_id: Option, kind: Option, after_ms: Option, before_ms: Option, db: &Connection, ) -> Result, ServerError> { let raws = all( db, "SELECT id, created_at, actor_id, target_id, community, action FROM admin_log WHERE (?1 IS NULL OR actor_id = ?1) AND (?2 IS NULL OR target_id = ?2) AND (?3 IS NULL OR kind = ?3) AND (?4 IS NULL OR created_at >= ?4) AND (?5 IS NULL OR created_at < ?5) ORDER BY id DESC LIMIT ?6", ( actor_id, target_id, kind.map(AdminActionKind::as_str), after_ms.map(|ms| ms as i64), before_ms.map(|ms| ms as i64), LOG_PAGE_LIMIT, ), |r| { Ok(( r.get::()?, r.get::()?, r.get::>()?, r.get::>()?, r.get::>()?, r.blob::()?, )) }, ) .await?; let mut entries = Vec::new(); for (id, created_at, actor_id, target_id, community, action) in raws { entries.push(AdminLogEntry { id, actor: user::read_optional(actor_id, db).await?, target: user::read_optional(target_id, db).await?, created_at, community, action, }); } Ok(entries) } pub async fn list_communities( actor_id: Option, target_id: Option, after_ms: Option, before_ms: Option, db: &Connection, ) -> Result, ServerError> { let raws = all( db, "SELECT l.id, l.created_at, l.actor_id, l.target_id, l.community_id, l.action FROM community_log l JOIN community c ON c.id = l.community_id WHERE (?1 IS NULL OR l.actor_id = ?1) AND (?2 IS NULL OR l.target_id = ?2) AND (?3 IS NULL OR l.created_at >= ?3) AND (?4 IS NULL OR l.created_at < ?4) ORDER BY l.id DESC LIMIT ?5", ( actor_id, target_id, after_ms.map(|ms| ms as i64), before_ms.map(|ms| ms as i64), LOG_PAGE_LIMIT, ), |r| { Ok(( r.get::()?, r.get::()?, r.get::>()?, r.get::>()?, r.get::()?, r.blob::()?, )) }, ) .await?; let mut entries = Vec::new(); for (id, created_at, actor_id, target_id, community_id, action) in raws { entries.push(AdminLogEntry { id, actor: user::read_optional(actor_id, db).await?, target: user::read_optional(target_id, db).await?, created_at, community: Some(community::label(community_id, db).await?), action: AdminAction::Community(action), }); } Ok(entries) }