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};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for RemoveComment {
22 type Response = CommentResponse;
24 #[tracing::instrument(skip(context))]
25 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
26 let data: &RemoveComment = self;
27 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
29 let comment_id = data.comment_id;
30 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
33 local_user_view.person.id,
34 orig_comment.community.id,
39 // Verify that only a mod or admin can remove
42 local_user_view.person.id,
43 orig_comment.community.id,
48 let removed = data.removed;
49 let updated_comment = Comment::update(
52 &CommentUpdateForm::builder().removed(Some(removed)).build(),
55 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
58 let form = ModRemoveCommentForm {
59 mod_person_id: local_user_view.person.id,
60 comment_id: data.comment_id,
61 removed: Some(removed),
62 reason: data.reason.clone(),
64 ModRemoveComment::create(context.pool(), &form).await?;
66 let post_id = updated_comment.post_id;
67 let post = Post::read(context.pool(), post_id).await?;
68 let recipient_ids = send_local_notifs(
71 &local_user_view.person.clone(),
78 build_comment_response(
81 Some(local_user_view),