]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Automatically resolve report when post/comment is removed (#3850)
[lemmy.git] / crates / api_crud / src / comment / remove.rs
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},
6   context::LemmyContext,
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
9 };
10 use lemmy_db_schema::{
11   source::{
12     comment::{Comment, CommentUpdateForm},
13     comment_report::CommentReport,
14     moderator::{ModRemoveComment, ModRemoveCommentForm},
15     post::Post,
16   },
17   traits::{Crud, Reportable},
18 };
19 use lemmy_db_views::structs::CommentView;
20 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
21
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?;
28
29   let comment_id = data.comment_id;
30   let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
31
32   check_community_ban(
33     local_user_view.person.id,
34     orig_comment.community.id,
35     &mut context.pool(),
36   )
37   .await?;
38
39   // Verify that only a mod or admin can remove
40   is_mod_or_admin(
41     &mut context.pool(),
42     local_user_view.person.id,
43     orig_comment.community.id,
44   )
45   .await?;
46
47   // Do the remove
48   let removed = data.removed;
49   let updated_comment = Comment::update(
50     &mut context.pool(),
51     comment_id,
52     &CommentUpdateForm {
53       removed: Some(removed),
54       ..Default::default()
55     },
56   )
57   .await
58   .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
59
60   CommentReport::resolve_all_for_object(&mut context.pool(), comment_id, local_user_view.person.id)
61     .await?;
62
63   // Mod tables
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(),
69   };
70   ModRemoveComment::create(&mut context.pool(), &form).await?;
71
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(
75     vec![],
76     &updated_comment,
77     &local_user_view.person.clone(),
78     &post,
79     false,
80     &context,
81   )
82   .await?;
83   let updated_comment_id = updated_comment.id;
84
85   ActivityChannel::submit_activity(
86     SendActivityData::RemoveComment(
87       updated_comment,
88       local_user_view.person.clone(),
89       orig_comment.community,
90       data.reason.clone(),
91     ),
92     &context,
93   )
94   .await?;
95
96   Ok(Json(
97     build_comment_response(
98       &context,
99       updated_comment_id,
100       Some(local_user_view),
101       recipient_ids,
102     )
103     .await?,
104   ))
105 }