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},
7 websocket::UserOperationCrud,
11 comment::{Comment, CommentUpdateForm},
12 moderator::{ModRemoveComment, ModRemoveCommentForm},
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::{error::LemmyError, ConnectionId};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for RemoveComment {
22 type Response = CommentResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<CommentResponse, LemmyError> {
30 let data: &RemoveComment = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let comment_id = data.comment_id;
35 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
38 local_user_view.person.id,
39 orig_comment.community.id,
44 // Verify that only a mod or admin can remove
47 local_user_view.person.id,
48 orig_comment.community.id,
53 let removed = data.removed;
54 let updated_comment = Comment::update(
57 &CommentUpdateForm::builder().removed(Some(removed)).build(),
60 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
63 let form = ModRemoveCommentForm {
64 mod_person_id: local_user_view.person.id,
65 comment_id: data.comment_id,
66 removed: Some(removed),
67 reason: data.reason.clone(),
69 ModRemoveComment::create(context.pool(), &form).await?;
71 let post_id = updated_comment.post_id;
72 let post = Post::read(context.pool(), post_id).await?;
73 let recipient_ids = context
77 &local_user_view.person.clone(),
84 .send_comment_ws_message(
85 &UserOperationCrud::RemoveComment,
88 None, // TODO maybe this might clear other forms
89 Some(local_user_view.person.id),