]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
b8834df8d69ac4dbde26deec96b351acf1bc5533
[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   comment::{CommentResponse, RemoveComment},
5   context::LemmyContext,
6   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
7   websocket::UserOperationCrud,
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, ConnectionId};
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for RemoveComment {
22   type Response = CommentResponse;
23
24   #[tracing::instrument(skip(context, websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     websocket_id: Option<ConnectionId>,
29   ) -> Result<CommentResponse, LemmyError> {
30     let data: &RemoveComment = self;
31     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
32
33     let comment_id = data.comment_id;
34     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
35
36     check_community_ban(
37       local_user_view.person.id,
38       orig_comment.community.id,
39       context.pool(),
40     )
41     .await?;
42
43     // Verify that only a mod or admin can remove
44     is_mod_or_admin(
45       context.pool(),
46       local_user_view.person.id,
47       orig_comment.community.id,
48     )
49     .await?;
50
51     // Do the remove
52     let removed = data.removed;
53     let updated_comment = Comment::update(
54       context.pool(),
55       comment_id,
56       &CommentUpdateForm::builder().removed(Some(removed)).build(),
57     )
58     .await
59     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
60
61     // Mod tables
62     let form = ModRemoveCommentForm {
63       mod_person_id: local_user_view.person.id,
64       comment_id: data.comment_id,
65       removed: Some(removed),
66       reason: data.reason.clone(),
67     };
68     ModRemoveComment::create(context.pool(), &form).await?;
69
70     let post_id = updated_comment.post_id;
71     let post = Post::read(context.pool(), post_id).await?;
72     let recipient_ids = context
73       .send_local_notifs(
74         vec![],
75         &updated_comment,
76         &local_user_view.person.clone(),
77         &post,
78         false,
79       )
80       .await?;
81
82     let res = context
83       .send_comment_ws_message(
84         &UserOperationCrud::RemoveComment,
85         data.comment_id,
86         websocket_id,
87         None, // TODO maybe this might clear other forms
88         Some(local_user_view.person.id),
89         recipient_ids,
90       )
91       .await?;
92
93     Ok(res)
94   }
95 }