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