1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, RemoveComment},
6 utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
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;
31 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
33 let comment_id = data.comment_id;
34 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
37 local_user_view.person.id,
38 orig_comment.community.id,
43 // Verify that only a mod or admin can remove
46 local_user_view.person.id,
47 orig_comment.community.id,
52 let removed = data.removed;
53 let updated_comment = Comment::update(
56 &CommentUpdateForm::builder().removed(Some(removed)).build(),
59 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
62 let form = ModRemoveCommentForm {
63 mod_person_id: local_user_view.person.id,
64 comment_id: data.comment_id,
65 removed: Some(removed),
66 reason: data.reason.clone(),
68 ModRemoveComment::create(context.pool(), &form).await?;
70 let post_id = updated_comment.post_id;
71 let post = Post::read(context.pool(), post_id).await?;
72 let recipient_ids = context
76 &local_user_view.person.clone(),
83 .send_comment_ws_message(
84 &UserOperationCrud::RemoveComment,
87 None, // TODO maybe this might clear other forms
88 Some(local_user_view.person.id),