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