]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Making the chat server an actor. (#2793)
[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::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 =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33
34     let comment_id = data.comment_id;
35     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
36
37     check_community_ban(
38       local_user_view.person.id,
39       orig_comment.community.id,
40       context.pool(),
41     )
42     .await?;
43
44     // Verify that only a mod or admin can remove
45     is_mod_or_admin(
46       context.pool(),
47       local_user_view.person.id,
48       orig_comment.community.id,
49     )
50     .await?;
51
52     // Do the remove
53     let removed = data.removed;
54     let updated_comment = Comment::update(
55       context.pool(),
56       comment_id,
57       &CommentUpdateForm::builder().removed(Some(removed)).build(),
58     )
59     .await
60     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
61
62     // Mod tables
63     let form = ModRemoveCommentForm {
64       mod_person_id: local_user_view.person.id,
65       comment_id: data.comment_id,
66       removed: Some(removed),
67       reason: data.reason.clone(),
68     };
69     ModRemoveComment::create(context.pool(), &form).await?;
70
71     let post_id = updated_comment.post_id;
72     let post = Post::read(context.pool(), post_id).await?;
73     let recipient_ids = context
74       .send_local_notifs(
75         vec![],
76         &updated_comment,
77         &local_user_view.person.clone(),
78         &post,
79         false,
80       )
81       .await?;
82
83     let res = context
84       .send_comment_ws_message(
85         &UserOperationCrud::RemoveComment,
86         data.comment_id,
87         websocket_id,
88         None, // TODO maybe this might clear other forms
89         Some(local_user_view.person.id),
90         recipient_ids,
91       )
92       .await?;
93
94     Ok(res)
95   }
96 }