]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/delete.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / api_crud / src / post / delete.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   get_local_user_view_from_jwt,
7   is_mod_or_admin,
8   post::*,
9 };
10 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
11 use lemmy_db_queries::{source::post::Post_, Crud};
12 use lemmy_db_schema::source::{community::Community, moderator::*, post::*};
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
15
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeletePost {
18   type Response = PostResponse;
19
20   async fn perform(
21     &self,
22     context: &Data<LemmyContext>,
23     websocket_id: Option<ConnectionId>,
24   ) -> Result<PostResponse, LemmyError> {
25     let data: &DeletePost = self;
26     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
27
28     let post_id = data.post_id;
29     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
30
31     check_community_ban(
32       local_user_view.person.id,
33       orig_post.community_id,
34       context.pool(),
35     )
36     .await?;
37
38     // Verify that only the creator can delete
39     if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
40       return Err(ApiError::err("no_post_edit_allowed").into());
41     }
42
43     // Update the post
44     let post_id = data.post_id;
45     let deleted = data.deleted;
46     let updated_post = blocking(context.pool(), move |conn| {
47       Post::update_deleted(conn, post_id, deleted)
48     })
49     .await??;
50
51     // apub updates
52     let community = blocking(context.pool(), move |conn| {
53       Community::read(conn, orig_post.community_id)
54     })
55     .await??;
56     send_apub_delete(
57       &local_user_view.person,
58       &community,
59       updated_post.ap_id.into(),
60       deleted,
61       context,
62     )
63     .await?;
64
65     send_post_ws_message(
66       data.post_id,
67       UserOperationCrud::DeletePost,
68       websocket_id,
69       Some(local_user_view.person.id),
70       context,
71     )
72     .await
73   }
74 }
75
76 #[async_trait::async_trait(?Send)]
77 impl PerformCrud for RemovePost {
78   type Response = PostResponse;
79
80   async fn perform(
81     &self,
82     context: &Data<LemmyContext>,
83     websocket_id: Option<ConnectionId>,
84   ) -> Result<PostResponse, LemmyError> {
85     let data: &RemovePost = self;
86     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
87
88     let post_id = data.post_id;
89     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
90
91     check_community_ban(
92       local_user_view.person.id,
93       orig_post.community_id,
94       context.pool(),
95     )
96     .await?;
97
98     // Verify that only the mods can remove
99     is_mod_or_admin(
100       context.pool(),
101       local_user_view.person.id,
102       orig_post.community_id,
103     )
104     .await?;
105
106     // Update the post
107     let post_id = data.post_id;
108     let removed = data.removed;
109     let updated_post = blocking(context.pool(), move |conn| {
110       Post::update_removed(conn, post_id, removed)
111     })
112     .await??;
113
114     // Mod tables
115     let form = ModRemovePostForm {
116       mod_person_id: local_user_view.person.id,
117       post_id: data.post_id,
118       removed: Some(removed),
119       reason: data.reason.to_owned(),
120     };
121     blocking(context.pool(), move |conn| {
122       ModRemovePost::create(conn, &form)
123     })
124     .await??;
125
126     // apub updates
127     let community = blocking(context.pool(), move |conn| {
128       Community::read(conn, orig_post.community_id)
129     })
130     .await??;
131     send_apub_remove(
132       &local_user_view.person,
133       &community,
134       updated_post.ap_id.into(),
135       data.reason.clone().unwrap_or_else(|| "".to_string()),
136       removed,
137       context,
138     )
139     .await?;
140
141     send_post_ws_message(
142       data.post_id,
143       UserOperationCrud::RemovePost,
144       websocket_id,
145       Some(local_user_view.person.id),
146       context,
147     )
148     .await
149   }
150 }