]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/delete.rs
Don't drop error context when adding a message to errors (#1958)
[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, DeletableObjects};
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::{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   #[tracing::instrument(skip(context, websocket_id))]
28   async fn perform(
29     &self,
30     context: &Data<LemmyContext>,
31     websocket_id: Option<ConnectionId>,
32   ) -> Result<PostResponse, LemmyError> {
33     let data: &DeletePost = self;
34     let local_user_view =
35       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36
37     let post_id = data.post_id;
38     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
39
40     // Dont delete it if its already been deleted.
41     if orig_post.deleted == data.deleted {
42       return Err(LemmyError::from_message("couldnt_update_post"));
43     }
44
45     check_community_ban(
46       local_user_view.person.id,
47       orig_post.community_id,
48       context.pool(),
49     )
50     .await?;
51     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
52
53     // Verify that only the creator can delete
54     if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
55       return Err(LemmyError::from_message("no_post_edit_allowed"));
56     }
57
58     // Update the post
59     let post_id = data.post_id;
60     let deleted = data.deleted;
61     let updated_post = blocking(context.pool(), move |conn| {
62       Post::update_deleted(conn, post_id, deleted)
63     })
64     .await??;
65
66     // apub updates
67     let community = blocking(context.pool(), move |conn| {
68       Community::read(conn, orig_post.community_id)
69     })
70     .await??;
71     send_apub_delete(
72       &local_user_view.person.clone().into(),
73       &community.into(),
74       DeletableObjects::Post(Box::new(updated_post.into())),
75       deleted,
76       context,
77     )
78     .await?;
79
80     send_post_ws_message(
81       data.post_id,
82       UserOperationCrud::DeletePost,
83       websocket_id,
84       Some(local_user_view.person.id),
85       context,
86     )
87     .await
88   }
89 }
90
91 #[async_trait::async_trait(?Send)]
92 impl PerformCrud for RemovePost {
93   type Response = PostResponse;
94
95   #[tracing::instrument(skip(context, websocket_id))]
96   async fn perform(
97     &self,
98     context: &Data<LemmyContext>,
99     websocket_id: Option<ConnectionId>,
100   ) -> Result<PostResponse, LemmyError> {
101     let data: &RemovePost = self;
102     let local_user_view =
103       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
104
105     let post_id = data.post_id;
106     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
107
108     check_community_ban(
109       local_user_view.person.id,
110       orig_post.community_id,
111       context.pool(),
112     )
113     .await?;
114
115     // Verify that only the mods can remove
116     is_mod_or_admin(
117       context.pool(),
118       local_user_view.person.id,
119       orig_post.community_id,
120     )
121     .await?;
122
123     // Update the post
124     let post_id = data.post_id;
125     let removed = data.removed;
126     let updated_post = blocking(context.pool(), move |conn| {
127       Post::update_removed(conn, post_id, removed)
128     })
129     .await??;
130
131     // Mod tables
132     let form = ModRemovePostForm {
133       mod_person_id: local_user_view.person.id,
134       post_id: data.post_id,
135       removed: Some(removed),
136       reason: data.reason.to_owned(),
137     };
138     blocking(context.pool(), move |conn| {
139       ModRemovePost::create(conn, &form)
140     })
141     .await??;
142
143     // apub updates
144     let community = blocking(context.pool(), move |conn| {
145       Community::read(conn, orig_post.community_id)
146     })
147     .await??;
148     send_apub_remove(
149       &local_user_view.person.clone().into(),
150       &community.into(),
151       DeletableObjects::Post(Box::new(updated_post.into())),
152       data.reason.clone().unwrap_or_else(|| "".to_string()),
153       removed,
154       context,
155     )
156     .await?;
157
158     send_post_ws_message(
159       data.post_id,
160       UserOperationCrud::RemovePost,
161       websocket_id,
162       Some(local_user_view.person.id),
163       context,
164     )
165     .await
166   }
167 }