]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Moving settings to Database. (#2492)
[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::{blocking, check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin},
6 };
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
8 use lemmy_db_schema::{
9   source::{
10     comment::{Comment, CommentUpdateForm},
11     community::Community,
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 use lemmy_websocket::{
20   send::{send_comment_ws_message, send_local_notifs},
21   LemmyContext,
22   UserOperationCrud,
23 };
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 = blocking(context.pool(), move |conn| {
41       CommentView::read(conn, comment_id, None)
42     })
43     .await??;
44
45     check_community_ban(
46       local_user_view.person.id,
47       orig_comment.community.id,
48       context.pool(),
49     )
50     .await?;
51
52     // Verify that only a mod or admin can remove
53     is_mod_or_admin(
54       context.pool(),
55       local_user_view.person.id,
56       orig_comment.community.id,
57     )
58     .await?;
59
60     // Do the remove
61     let removed = data.removed;
62     let updated_comment = blocking(context.pool(), move |conn| {
63       Comment::update(
64         conn,
65         comment_id,
66         &CommentUpdateForm::builder().removed(Some(removed)).build(),
67       )
68     })
69     .await?
70     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
71
72     // Mod tables
73     let form = ModRemoveCommentForm {
74       mod_person_id: local_user_view.person.id,
75       comment_id: data.comment_id,
76       removed: Some(removed),
77       reason: data.reason.to_owned(),
78     };
79     blocking(context.pool(), move |conn| {
80       ModRemoveComment::create(conn, &form)
81     })
82     .await??;
83
84     let post_id = updated_comment.post_id;
85     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
86     let recipient_ids = send_local_notifs(
87       vec![],
88       &updated_comment,
89       &local_user_view.person.clone(),
90       &post,
91       false,
92       context,
93     )
94     .await?;
95
96     let res = send_comment_ws_message(
97       data.comment_id,
98       UserOperationCrud::RemoveComment,
99       websocket_id,
100       None, // TODO maybe this might clear other forms
101       Some(local_user_view.person.id),
102       recipient_ids,
103       context,
104     )
105     .await?;
106
107     // Send the apub message
108     let community = blocking(context.pool(), move |conn| {
109       Community::read(conn, orig_comment.post.community_id)
110     })
111     .await??;
112     let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
113     send_apub_delete_in_community(
114       local_user_view.person,
115       community,
116       deletable,
117       data.reason.clone().or_else(|| Some("".to_string())),
118       removed,
119       context,
120     )
121     .await?;
122
123     Ok(res)
124   }
125 }