1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, DeleteComment},
6 utils::{check_community_ban, local_user_view_from_jwt},
7 websocket::UserOperationCrud,
11 comment::{Comment, CommentUpdateForm},
16 use lemmy_db_views::structs::CommentView;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for DeleteComment {
21 type Response = CommentResponse;
23 #[tracing::instrument(skip(context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<CommentResponse, LemmyError> {
29 let data: &DeleteComment = self;
30 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
32 let comment_id = data.comment_id;
33 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
35 // Dont delete it if its already been deleted.
36 if orig_comment.comment.deleted == data.deleted {
37 return Err(LemmyError::from_message("couldnt_update_comment"));
41 local_user_view.person.id,
42 orig_comment.community.id,
47 // Verify that only the creator can delete
48 if local_user_view.person.id != orig_comment.creator.id {
49 return Err(LemmyError::from_message("no_comment_edit_allowed"));
53 let deleted = data.deleted;
54 let updated_comment = Comment::update(
57 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
60 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
62 let post_id = updated_comment.post_id;
63 let post = Post::read(context.pool(), post_id).await?;
64 let recipient_ids = context
68 &local_user_view.person,
75 .send_comment_ws_message(
76 &UserOperationCrud::DeleteComment,
80 Some(local_user_view.person.id),