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},
8 send::{send_comment_ws_message, send_local_notifs},
12 use lemmy_db_schema::{
14 comment::{Comment, CommentUpdateForm},
19 use lemmy_db_views::structs::CommentView;
20 use lemmy_utils::{error::LemmyError, ConnectionId};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for DeleteComment {
24 type Response = CommentResponse;
26 #[tracing::instrument(skip(context, websocket_id))]
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<CommentResponse, LemmyError> {
32 let data: &DeleteComment = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 let comment_id = data.comment_id;
37 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
39 // Dont delete it if its already been deleted.
40 if orig_comment.comment.deleted == data.deleted {
41 return Err(LemmyError::from_message("couldnt_update_comment"));
45 local_user_view.person.id,
46 orig_comment.community.id,
51 // Verify that only the creator can delete
52 if local_user_view.person.id != orig_comment.creator.id {
53 return Err(LemmyError::from_message("no_comment_edit_allowed"));
57 let deleted = data.deleted;
58 let updated_comment = Comment::update(
61 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
64 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
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,
78 let res = send_comment_ws_message(
80 UserOperationCrud::DeleteComment,
82 None, // TODO a comment delete might clear forms?
83 Some(local_user_view.person.id),