1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, DeleteComment},
6 utils::{check_community_ban, get_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;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 let comment_id = data.comment_id;
34 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
36 // Dont delete it if its already been deleted.
37 if orig_comment.comment.deleted == data.deleted {
38 return Err(LemmyError::from_message("couldnt_update_comment"));
42 local_user_view.person.id,
43 orig_comment.community.id,
48 // Verify that only the creator can delete
49 if local_user_view.person.id != orig_comment.creator.id {
50 return Err(LemmyError::from_message("no_comment_edit_allowed"));
54 let deleted = data.deleted;
55 let updated_comment = Comment::update(
58 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
61 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
63 let post_id = updated_comment.post_id;
64 let post = Post::read(context.pool(), post_id).await?;
65 let recipient_ids = context
69 &local_user_view.person,
76 .send_comment_ws_message(
77 &UserOperationCrud::DeleteComment,
81 Some(local_user_view.person.id),