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};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for DeleteComment {
21 type Response = CommentResponse;
23 #[tracing::instrument(skip(context))]
24 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
25 let data: &DeleteComment = self;
26 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
28 let comment_id = data.comment_id;
29 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
31 // Dont delete it if its already been deleted.
32 if orig_comment.comment.deleted == data.deleted {
33 return Err(LemmyErrorType::CouldntUpdateComment)?;
37 local_user_view.person.id,
38 orig_comment.community.id,
43 // Verify that only the creator can delete
44 if local_user_view.person.id != orig_comment.creator.id {
45 return Err(LemmyErrorType::NoCommentEditAllowed)?;
49 let deleted = data.deleted;
50 let updated_comment = Comment::update(
53 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
56 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
58 let post_id = updated_comment.post_id;
59 let post = Post::read(&mut context.pool(), post_id).await?;
60 let recipient_ids = send_local_notifs(
63 &local_user_view.person,
70 build_comment_response(
73 Some(local_user_view),