You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
47 lines
1.2 KiB
use crate::api::{APIError, CurrentUser, PolicyError::*, API, UGC}; |
|
use crate::db_conn::Db; |
|
use crate::models::*; |
|
use crate::rds_conn::RdsConn; |
|
use rocket::form::Form; |
|
use rocket::serde::json::{json, Value}; |
|
|
|
#[derive(FromForm)] |
|
pub struct DeleteInput { |
|
#[field(name = "type")] |
|
id_type: String, |
|
id: i32, |
|
note: String, |
|
} |
|
|
|
#[post("/delete", data = "<di>")] |
|
pub async fn delete( |
|
di: Form<DeleteInput>, |
|
user: CurrentUser, |
|
db: Db, |
|
rconn: RdsConn, |
|
) -> API<Value> { |
|
let mut p: Post; |
|
match di.id_type.as_str() { |
|
"cid" => { |
|
let mut 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?; |
|
p.change_hot_score(&db, -1).await?; |
|
|
|
p.clear_comments_cache(&rconn).await; |
|
} |
|
"pid" => { |
|
p = Post::get(&db, &rconn, di.id).await?; |
|
p.soft_delete(&user, &db).await?; |
|
} |
|
_ => return Err(APIError::PcError(NotAllowed)), |
|
} |
|
|
|
// 如果是删除,需要也从0号缓存队列中去掉 |
|
p.refresh_cache(&rconn, true).await; |
|
|
|
Ok(json!({ |
|
"code": 0 |
|
})) |
|
}
|
|
|