1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::{build_comment_response, send_local_notifs},
5 comment::{CommentResponse, RemoveComment},
7 utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
11 comment::{Comment, CommentUpdateForm},
12 moderator::{ModRemoveComment, ModRemoveCommentForm},
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for RemoveComment {
23 type Response = CommentResponse;
25 #[tracing::instrument(skip(context))]
26 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
27 let data: &RemoveComment = self;
28 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
30 let comment_id = data.comment_id;
31 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
34 local_user_view.person.id,
35 orig_comment.community.id,
40 // Verify that only a mod or admin can remove
43 local_user_view.person.id,
44 orig_comment.community.id,
49 let removed = data.removed;
50 let updated_comment = Comment::update(
53 &CommentUpdateForm::builder().removed(Some(removed)).build(),
56 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
59 let form = ModRemoveCommentForm {
60 mod_person_id: local_user_view.person.id,
61 comment_id: data.comment_id,
62 removed: Some(removed),
63 reason: data.reason.clone(),
65 ModRemoveComment::create(&mut context.pool(), &form).await?;
67 let post_id = updated_comment.post_id;
68 let post = Post::read(&mut context.pool(), post_id).await?;
69 let recipient_ids = send_local_notifs(
72 &local_user_view.person.clone(),
79 build_comment_response(
82 Some(local_user_view),