1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 build_response::{build_comment_response, send_local_notifs},
5 comment::{CommentResponse, RemoveComment},
7 send_activity::{ActivityChannel, SendActivityData},
8 utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
10 use lemmy_db_schema::{
12 comment::{Comment, CommentUpdateForm},
13 moderator::{ModRemoveComment, ModRemoveCommentForm},
18 use lemmy_db_views::structs::CommentView;
19 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
21 #[tracing::instrument(skip(context))]
22 pub async fn remove_comment(
23 data: Json<RemoveComment>,
24 context: Data<LemmyContext>,
25 ) -> Result<Json<CommentResponse>, LemmyError> {
26 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
28 let comment_id = data.comment_id;
29 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
32 local_user_view.person.id,
33 orig_comment.community.id,
38 // Verify that only a mod or admin can remove
41 local_user_view.person.id,
42 orig_comment.community.id,
47 let removed = data.removed;
48 let updated_comment = Comment::update(
52 removed: Some(removed),
57 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
60 let form = ModRemoveCommentForm {
61 mod_person_id: local_user_view.person.id,
62 comment_id: data.comment_id,
63 removed: Some(removed),
64 reason: data.reason.clone(),
66 ModRemoveComment::create(&mut context.pool(), &form).await?;
68 let post_id = updated_comment.post_id;
69 let post = Post::read(&mut context.pool(), post_id).await?;
70 let recipient_ids = send_local_notifs(
73 &local_user_view.person.clone(),
79 let updated_comment_id = updated_comment.id;
81 ActivityChannel::submit_activity(
82 SendActivityData::RemoveComment(
84 local_user_view.person.clone(),
85 orig_comment.community,
93 build_comment_response(
96 Some(local_user_view),