|
|
|
@ -1,14 +1,14 @@
|
|
|
|
|
use crate::api::{APIError, CurrentUser, JsonAPI, PolicyError::*, UGC}; |
|
|
|
|
use crate::api::{CurrentUser, JsonAPI, PolicyError::*, UGC}; |
|
|
|
|
use crate::db_conn::Db; |
|
|
|
|
use crate::libs::diesel_logger::LoggingConnection; |
|
|
|
|
use crate::models::*; |
|
|
|
|
use crate::rds_conn::RdsConn; |
|
|
|
|
use crate::rds_models::*; |
|
|
|
|
use crate::schema; |
|
|
|
|
use chrono::offset::Local; |
|
|
|
|
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; |
|
|
|
|
use rocket::form::Form; |
|
|
|
|
use rocket::serde::json::json; |
|
|
|
|
use crate::libs::diesel_logger::LoggingConnection; |
|
|
|
|
use crate::schema; |
|
|
|
|
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; |
|
|
|
|
|
|
|
|
|
#[derive(FromForm)] |
|
|
|
|
pub struct DeleteInput { |
|
|
|
@ -20,14 +20,11 @@ pub struct DeleteInput {
|
|
|
|
|
|
|
|
|
|
#[post("/delete", data = "<di>")] |
|
|
|
|
pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: RdsConn) -> JsonAPI { |
|
|
|
|
let mut p: Post; |
|
|
|
|
let mut c: Comment; |
|
|
|
|
let author_hash: &str; |
|
|
|
|
match di.id_type.as_str() { |
|
|
|
|
let (author_hash, p) = match di.id_type.as_str() { |
|
|
|
|
"cid" => { |
|
|
|
|
c = Comment::get(&db, di.id).await?; |
|
|
|
|
let mut c = Comment::get(&db, di.id).await?; |
|
|
|
|
c.soft_delete(&user, &db).await?; |
|
|
|
|
p = Post::get(&db, &rconn, c.post_id).await?; |
|
|
|
|
let mut p = Post::get(&db, &rconn, c.post_id).await?; |
|
|
|
|
update!( |
|
|
|
|
p, |
|
|
|
|
posts, |
|
|
|
@ -39,20 +36,21 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: Rds
|
|
|
|
|
p.refresh_cache(&rconn, false).await; |
|
|
|
|
p.clear_comments_cache(&rconn).await; |
|
|
|
|
|
|
|
|
|
author_hash = &c.author_hash; |
|
|
|
|
(c.author_hash.clone(), p) |
|
|
|
|
} |
|
|
|
|
"pid" => { |
|
|
|
|
p = Post::get(&db, &rconn, di.id).await?; |
|
|
|
|
let mut 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; |
|
|
|
|
(p.author_hash.clone(), p) |
|
|
|
|
} |
|
|
|
|
_ => return Err(APIError::PcError(NotAllowed)), |
|
|
|
|
} |
|
|
|
|
_ => { Err(NotAllowed) }?, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if user.is_admin && !user.namehash.eq(author_hash) { |
|
|
|
|
if user.is_admin && !user.namehash.eq(&author_hash) { |
|
|
|
|
Systemlog { |
|
|
|
|
user_hash: user.namehash.clone(), |
|
|
|
|
action_type: LogType::AdminDelete, |
|
|
|
@ -73,18 +71,17 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db, rconn: Rds
|
|
|
|
|
} |
|
|
|
|
.create(&rconn) |
|
|
|
|
.await?; |
|
|
|
|
BannedUsers::add(&rconn, author_hash).await?; |
|
|
|
|
BannedUsers::add(&rconn, &author_hash).await?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(json!({ |
|
|
|
|
"code": 0 |
|
|
|
|
})) |
|
|
|
|
code0!() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(FromForm)] |
|
|
|
|
pub struct ReportInput { |
|
|
|
|
pid: i32, |
|
|
|
|
#[field(validate = len(0..1000))] |
|
|
|
|
reason: String, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -99,12 +96,57 @@ pub async fn report(ri: Form<ReportInput>, user: CurrentUser, db: Db, rconn: Rds
|
|
|
|
|
Systemlog { |
|
|
|
|
user_hash: user.namehash, |
|
|
|
|
action_type: LogType::Report, |
|
|
|
|
target: format!("#{} {}", ri.pid, if ri.reason.starts_with("评论区") { "评论区" } else {""}), |
|
|
|
|
target: format!( |
|
|
|
|
"#{} {}", |
|
|
|
|
ri.pid, |
|
|
|
|
if ri.reason.starts_with("评论区") { |
|
|
|
|
"评论区" |
|
|
|
|
} else { |
|
|
|
|
"" |
|
|
|
|
} |
|
|
|
|
), |
|
|
|
|
detail: ri.reason.clone(), |
|
|
|
|
time: Local::now(), |
|
|
|
|
}.create(&rconn) |
|
|
|
|
} |
|
|
|
|
.create(&rconn) |
|
|
|
|
.await?; |
|
|
|
|
|
|
|
|
|
code0!() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(FromForm)] |
|
|
|
|
pub struct BlockInput { |
|
|
|
|
#[field(name = "type")] |
|
|
|
|
content_type: String, |
|
|
|
|
id: i32, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[post("/block", data = "<bi>")] |
|
|
|
|
pub async fn block(bi: Form<BlockInput>, user: CurrentUser, db: Db, rconn: RdsConn) -> JsonAPI { |
|
|
|
|
let mut blk = BlockedUsers::init(user.id.ok_or_else(|| NotAllowed)?, &rconn); |
|
|
|
|
|
|
|
|
|
let nh_to_block = match bi.content_type.as_str() { |
|
|
|
|
"post" => Post::get(&db, &rconn, bi.id).await?.author_hash, |
|
|
|
|
"comment" => Comment::get(&db, bi.id).await?.author_hash, |
|
|
|
|
_ => { Err(NotAllowed) }?, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if nh_to_block.eq(&user.namehash) { |
|
|
|
|
{ Err(NotAllowed) }?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
blk.add(&nh_to_block).await?; |
|
|
|
|
let curr = BlockCounter::count_incr(&rconn, &nh_to_block).await?; |
|
|
|
|
|
|
|
|
|
if curr >= BLOCK_THRESHOLD || user.is_admin { |
|
|
|
|
DangerousUser::add(&rconn, &nh_to_block).await?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(json!({ |
|
|
|
|
"code": 0 |
|
|
|
|
"code": 0, |
|
|
|
|
"data": { |
|
|
|
|
"curr": curr, |
|
|
|
|
"threshold": BLOCK_THRESHOLD, |
|
|
|
|
}, |
|
|
|
|
})) |
|
|
|
|
} |
|
|
|
|