]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
b2071298656f6074f07223d6c0c68c57337ab626
[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     moderator::{ModRemoveComment, ModRemoveCommentForm},
14     post::Post,
15   },
16   traits::Crud,
17 };
18 use lemmy_db_views::structs::CommentView;
19 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
20
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?;
27
28   let comment_id = data.comment_id;
29   let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
30
31   check_community_ban(
32     local_user_view.person.id,
33     orig_comment.community.id,
34     &mut context.pool(),
35   )
36   .await?;
37
38   // Verify that only a mod or admin can remove
39   is_mod_or_admin(
40     &mut context.pool(),
41     local_user_view.person.id,
42     orig_comment.community.id,
43   )
44   .await?;
45
46   // Do the remove
47   let removed = data.removed;
48   let updated_comment = Comment::update(
49     &mut context.pool(),
50     comment_id,
51     &CommentUpdateForm::builder().removed(Some(removed)).build(),
52   )
53   .await
54   .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
55
56   // Mod tables
57   let form = ModRemoveCommentForm {
58     mod_person_id: local_user_view.person.id,
59     comment_id: data.comment_id,
60     removed: Some(removed),
61     reason: data.reason.clone(),
62   };
63   ModRemoveComment::create(&mut context.pool(), &form).await?;
64
65   let post_id = updated_comment.post_id;
66   let post = Post::read(&mut context.pool(), post_id).await?;
67   let recipient_ids = send_local_notifs(
68     vec![],
69     &updated_comment,
70     &local_user_view.person.clone(),
71     &post,
72     false,
73     &context,
74   )
75   .await?;
76   let updated_comment_id = updated_comment.id;
77
78   ActivityChannel::submit_activity(
79     SendActivityData::RemoveComment(
80       updated_comment,
81       local_user_view.person.clone(),
82       orig_comment.community,
83       data.reason.clone(),
84     ),
85     &context,
86   )
87   .await?;
88
89   Ok(Json(
90     build_comment_response(
91       &context,
92       updated_comment_id,
93       Some(local_user_view),
94       None,
95       recipient_ids,
96     )
97     .await?,
98   ))
99 }