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, DeleteComment},
7 utils::{check_community_ban, local_user_view_from_jwt},
11 comment::{Comment, CommentUpdateForm},
16 use lemmy_db_views::structs::CommentView;
17 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for DeleteComment {
22 type Response = CommentResponse;
24 #[tracing::instrument(skip(context))]
25 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
26 let data: &DeleteComment = 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(&mut context.pool(), comment_id, None).await?;
32 // Dont delete it if its already been deleted.
33 if orig_comment.comment.deleted == data.deleted {
34 return Err(LemmyErrorType::CouldntUpdateComment)?;
38 local_user_view.person.id,
39 orig_comment.community.id,
44 // Verify that only the creator can delete
45 if local_user_view.person.id != orig_comment.creator.id {
46 return Err(LemmyErrorType::NoCommentEditAllowed)?;
50 let deleted = data.deleted;
51 let updated_comment = Comment::update(
54 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
57 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
59 let post_id = updated_comment.post_id;
60 let post = Post::read(&mut context.pool(), post_id).await?;
61 let recipient_ids = send_local_notifs(
64 &local_user_view.person,
71 build_comment_response(
74 Some(local_user_view),