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