]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Remove chatserver (#2919)
[lemmy.git] / crates / api_crud / src / comment / remove.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   build_response::{build_comment_response, send_local_notifs},
5   comment::{CommentResponse, RemoveComment},
6   context::LemmyContext,
7   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     comment::{Comment, CommentUpdateForm},
12     moderator::{ModRemoveComment, ModRemoveCommentForm},
13     post::Post,
14   },
15   traits::Crud,
16 };
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::error::LemmyError;
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for RemoveComment {
22   type Response = CommentResponse;
23
24   #[tracing::instrument(skip(context))]
25   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
26     let data: &RemoveComment = self;
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(context.pool(), comment_id, None).await?;
31
32     check_community_ban(
33       local_user_view.person.id,
34       orig_comment.community.id,
35       context.pool(),
36     )
37     .await?;
38
39     // Verify that only a mod or admin can remove
40     is_mod_or_admin(
41       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       context.pool(),
51       comment_id,
52       &CommentUpdateForm::builder().removed(Some(removed)).build(),
53     )
54     .await
55     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
56
57     // Mod tables
58     let form = ModRemoveCommentForm {
59       mod_person_id: local_user_view.person.id,
60       comment_id: data.comment_id,
61       removed: Some(removed),
62       reason: data.reason.clone(),
63     };
64     ModRemoveComment::create(context.pool(), &form).await?;
65
66     let post_id = updated_comment.post_id;
67     let post = Post::read(context.pool(), post_id).await?;
68     let recipient_ids = send_local_notifs(
69       vec![],
70       &updated_comment,
71       &local_user_view.person.clone(),
72       &post,
73       false,
74       context,
75     )
76     .await?;
77
78     build_comment_response(
79       context,
80       updated_comment.id,
81       Some(local_user_view),
82       None,
83       recipient_ids,
84     )
85     .await
86   }
87 }