]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Replace TypedBuilder with Default in update forms (#3814)
[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 {
52       removed: Some(removed),
53       ..Default::default()
54     },
55   )
56   .await
57   .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
58
59   // Mod tables
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(),
65   };
66   ModRemoveComment::create(&mut context.pool(), &form).await?;
67
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(
71     vec![],
72     &updated_comment,
73     &local_user_view.person.clone(),
74     &post,
75     false,
76     &context,
77   )
78   .await?;
79   let updated_comment_id = updated_comment.id;
80
81   ActivityChannel::submit_activity(
82     SendActivityData::RemoveComment(
83       updated_comment,
84       local_user_view.person.clone(),
85       orig_comment.community,
86       data.reason.clone(),
87     ),
88     &context,
89   )
90   .await?;
91
92   Ok(Json(
93     build_comment_response(
94       &context,
95       updated_comment_id,
96       Some(local_user_view),
97       recipient_ids,
98     )
99     .await?,
100   ))
101 }