2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2022-06-13 19:15:04 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-06-13 19:15:04 +00:00
|
|
|
site::{PurgeComment, PurgeItemResponse},
|
2023-07-26 18:01:15 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, sanitize_html_opt},
|
2022-06-13 19:15:04 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
comment::Comment,
|
|
|
|
moderator::{AdminPurgeComment, AdminPurgeCommentForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn purge_comment(
|
|
|
|
data: Json<PurgeComment>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<PurgeItemResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Only let admin purge an item
|
|
|
|
is_admin(&local_user_view)?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
let comment_id = data.comment_id;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Read the comment to get the post_id
|
|
|
|
let comment = Comment::read(&mut context.pool(), comment_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
let post_id = comment.post_id;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// TODO read comments for pictrs images and purge them
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
Comment::delete(&mut context.pool(), comment_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Mod tables
|
|
|
|
let reason = sanitize_html_opt(&data.reason);
|
|
|
|
let form = AdminPurgeCommentForm {
|
|
|
|
admin_person_id: local_user_view.person.id,
|
|
|
|
reason,
|
|
|
|
post_id,
|
|
|
|
};
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
AdminPurgeComment::create(&mut context.pool(), &form).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
Ok(Json(PurgeItemResponse { success: true }))
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|