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 comment_report::CommentReport,
14 moderator::{ModRemoveComment, ModRemoveCommentForm},
17 traits::{Crud, Reportable},
19 use lemmy_db_views::structs::CommentView;
20 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
22 #[tracing::instrument(skip(context))]
23 pub async fn remove_comment(
24 data: Json<RemoveComment>,
25 context: Data<LemmyContext>,
26 ) -> Result<Json<CommentResponse>, LemmyError> {
27 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
29 let comment_id = data.comment_id;
30 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
33 local_user_view.person.id,
34 orig_comment.community.id,
39 // Verify that only a mod or admin can remove
42 local_user_view.person.id,
43 orig_comment.community.id,
48 let removed = data.removed;
49 let updated_comment = Comment::update(
53 removed: Some(removed),
58 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
60 CommentReport::resolve_all_for_object(&mut context.pool(), comment_id, local_user_view.person.id)
64 let form = ModRemoveCommentForm {
65 mod_person_id: local_user_view.person.id,
66 comment_id: data.comment_id,
67 removed: Some(removed),
68 reason: data.reason.clone(),
70 ModRemoveComment::create(&mut context.pool(), &form).await?;
72 let post_id = updated_comment.post_id;
73 let post = Post::read(&mut context.pool(), post_id).await?;
74 let recipient_ids = send_local_notifs(
77 &local_user_view.person.clone(),
83 let updated_comment_id = updated_comment.id;
85 ActivityChannel::submit_activity(
86 SendActivityData::RemoveComment(
88 local_user_view.person.clone(),
89 orig_comment.community,
97 build_comment_response(
100 Some(local_user_view),