feat: record and show systemlog, admin delete log
This commit is contained in:
@@ -2,6 +2,8 @@ use crate::api::{APIError, CurrentUser, PolicyError::*, API, UGC};
|
||||
use crate::db_conn::Db;
|
||||
use crate::models::*;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::*;
|
||||
use chrono::offset::Local;
|
||||
use rocket::form::Form;
|
||||
use rocket::serde::json::{json, Value};
|
||||
|
||||
@@ -21,9 +23,11 @@ pub async fn delete(
|
||||
rconn: RdsConn,
|
||||
) -> API<Value> {
|
||||
let mut p: Post;
|
||||
let mut c: Comment;
|
||||
let author_hash: &str;
|
||||
match di.id_type.as_str() {
|
||||
"cid" => {
|
||||
let mut c = Comment::get(&db, di.id).await?;
|
||||
c = Comment::get(&db, di.id).await?;
|
||||
c.soft_delete(&user, &db).await?;
|
||||
p = Post::get(&db, &rconn, c.post_id).await?;
|
||||
p.change_n_comments(&db, -1).await?;
|
||||
@@ -31,16 +35,32 @@ pub async fn delete(
|
||||
|
||||
p.refresh_cache(&rconn, false).await;
|
||||
p.clear_comments_cache(&rconn).await;
|
||||
|
||||
author_hash = &c.author_hash;
|
||||
}
|
||||
"pid" => {
|
||||
p = Post::get(&db, &rconn, di.id).await?;
|
||||
p.soft_delete(&user, &db).await?;
|
||||
// 如果是删除,需要也从0号缓存队列中去掉
|
||||
p.refresh_cache(&rconn, true).await;
|
||||
|
||||
author_hash = &p.author_hash;
|
||||
}
|
||||
_ => return Err(APIError::PcError(NotAllowed)),
|
||||
}
|
||||
|
||||
if user.is_admin && author_hash != user.namehash {
|
||||
Systemlog {
|
||||
user_hash: user.namehash,
|
||||
action_type: LogType::AdminDelete,
|
||||
target: format!("#{}, {}={}", p.id, di.id_type, di.id),
|
||||
detail: di.note.clone(),
|
||||
time: Local::now(),
|
||||
}
|
||||
.create(&rconn)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(json!({
|
||||
"code": 0
|
||||
}))
|
||||
|
||||
@@ -100,7 +100,6 @@ pub async fn ps2outputs(
|
||||
|
||||
#[get("/getone?<pid>")]
|
||||
pub async fn get_one(pid: i32, user: CurrentUser, db: Db, rconn: RdsConn) -> JsonAPI {
|
||||
// let p = Post::get(&db, pid).await?;
|
||||
let p = Post::get(&db, &rconn, pid).await?;
|
||||
p.check_permission(&user, "ro")?;
|
||||
Ok(json!({
|
||||
|
||||
@@ -1,16 +1,26 @@
|
||||
use crate::api::{CurrentUser, API};
|
||||
use crate::db_conn::Db;
|
||||
use crate::api::{CurrentUser, JsonAPI};
|
||||
use crate::random_hasher::RandomHasher;
|
||||
use crate::rds_conn::RdsConn;
|
||||
use crate::rds_models::Systemlog;
|
||||
use rocket::serde::json::{json, Value};
|
||||
use rocket::State;
|
||||
|
||||
#[get("/systemlog")]
|
||||
pub async fn get_systemlog(user: CurrentUser, rh: &State<RandomHasher>, db: Db) -> API<Value> {
|
||||
pub async fn get_systemlog(user: CurrentUser, rh: &State<RandomHasher>, rconn: RdsConn) -> JsonAPI {
|
||||
let logs = Systemlog::get_list(&rconn, 50).await?;
|
||||
|
||||
Ok(json!({
|
||||
"tmp_token": rh.get_tmp_token(),
|
||||
"salt": look!(rh.salt),
|
||||
"start_time": rh.start_time.timestamp(),
|
||||
"custom_title": user.custom_title,
|
||||
"data": [],
|
||||
"data": logs.into_iter().map(|log|
|
||||
json!({
|
||||
"type": log.action_type,
|
||||
"user": look!(log.user_hash),
|
||||
"timestamp": log.time.timestamp(),
|
||||
"detail": format!("{}\n{}", &log.target, &log.detail)
|
||||
})
|
||||
).collect::<Vec<Value>>(),
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user