]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/remove.rs
Extract Activitypub logic into separate library (#2288)
[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,
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_removed(conn, comment_id, removed)
64     })
65     .await?
66     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
67
68     // Mod tables
69     let form = ModRemoveCommentForm {
70       mod_person_id: local_user_view.person.id,
71       comment_id: data.comment_id,
72       removed: Some(removed),
73       reason: data.reason.to_owned(),
74     };
75     blocking(context.pool(), move |conn| {
76       ModRemoveComment::create(conn, &form)
77     })
78     .await??;
79
80     let post_id = updated_comment.post_id;
81     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82     let recipient_ids = send_local_notifs(
83       vec![],
84       &updated_comment,
85       &local_user_view.person.clone(),
86       &post,
87       false,
88       context,
89     )
90     .await?;
91
92     let res = send_comment_ws_message(
93       data.comment_id,
94       UserOperationCrud::RemoveComment,
95       websocket_id,
96       None, // TODO maybe this might clear other forms
97       Some(local_user_view.person.id),
98       recipient_ids,
99       context,
100     )
101     .await?;
102
103     // Send the apub message
104     let community = blocking(context.pool(), move |conn| {
105       Community::read(conn, orig_comment.post.community_id)
106     })
107     .await??;
108     let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
109     send_apub_delete_in_community(
110       local_user_view.person,
111       community,
112       deletable,
113       data.reason.clone().or_else(|| Some("".to_string())),
114       removed,
115       context,
116     )
117     .await?;
118
119     Ok(res)
120   }
121 }