1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, RemoveComment},
6 utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin},
8 send::{send_comment_ws_message, send_local_notifs},
12 use lemmy_db_schema::{
14 comment::{Comment, CommentUpdateForm},
15 moderator::{ModRemoveComment, ModRemoveCommentForm},
20 use lemmy_db_views::structs::CommentView;
21 use lemmy_utils::{error::LemmyError, ConnectionId};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for RemoveComment {
25 type Response = CommentResponse;
27 #[tracing::instrument(skip(context, websocket_id))]
30 context: &Data<LemmyContext>,
31 websocket_id: Option<ConnectionId>,
32 ) -> Result<CommentResponse, LemmyError> {
33 let data: &RemoveComment = self;
35 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37 let comment_id = data.comment_id;
38 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
41 local_user_view.person.id,
42 orig_comment.community.id,
47 // Verify that only a mod or admin can remove
50 local_user_view.person.id,
51 orig_comment.community.id,
56 let removed = data.removed;
57 let updated_comment = Comment::update(
60 &CommentUpdateForm::builder().removed(Some(removed)).build(),
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
66 let form = ModRemoveCommentForm {
67 mod_person_id: local_user_view.person.id,
68 comment_id: data.comment_id,
69 removed: Some(removed),
70 reason: data.reason.clone(),
72 ModRemoveComment::create(context.pool(), &form).await?;
74 let post_id = updated_comment.post_id;
75 let post = Post::read(context.pool(), post_id).await?;
76 let recipient_ids = send_local_notifs(
79 &local_user_view.person.clone(),
86 let res = send_comment_ws_message(
88 UserOperationCrud::RemoveComment,
90 None, // TODO maybe this might clear other forms
91 Some(local_user_view.person.id),