1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 get_local_user_view_from_jwt,
11 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
12 use lemmy_db_schema::{
15 moderator::{ModRemovePost, ModRemovePostForm},
20 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
21 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for DeletePost {
25 type Response = PostResponse;
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<PostResponse, LemmyError> {
32 let data: &DeletePost = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 let post_id = data.post_id;
37 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
39 // Dont delete it if its already been deleted.
40 if orig_post.deleted == data.deleted {
41 return Err(ApiError::err_plain("couldnt_update_post").into());
45 local_user_view.person.id,
46 orig_post.community_id,
50 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
52 // Verify that only the creator can delete
53 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
54 return Err(ApiError::err_plain("no_post_edit_allowed").into());
58 let post_id = data.post_id;
59 let deleted = data.deleted;
60 let updated_post = blocking(context.pool(), move |conn| {
61 Post::update_deleted(conn, post_id, deleted)
66 let community = blocking(context.pool(), move |conn| {
67 Community::read(conn, orig_post.community_id)
71 &local_user_view.person.clone().into(),
73 updated_post.ap_id.into(),
81 UserOperationCrud::DeletePost,
83 Some(local_user_view.person.id),
90 #[async_trait::async_trait(?Send)]
91 impl PerformCrud for RemovePost {
92 type Response = PostResponse;
96 context: &Data<LemmyContext>,
97 websocket_id: Option<ConnectionId>,
98 ) -> Result<PostResponse, LemmyError> {
99 let data: &RemovePost = self;
100 let local_user_view =
101 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
103 let post_id = data.post_id;
104 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
107 local_user_view.person.id,
108 orig_post.community_id,
113 // Verify that only the mods can remove
116 local_user_view.person.id,
117 orig_post.community_id,
122 let post_id = data.post_id;
123 let removed = data.removed;
124 let updated_post = blocking(context.pool(), move |conn| {
125 Post::update_removed(conn, post_id, removed)
130 let form = ModRemovePostForm {
131 mod_person_id: local_user_view.person.id,
132 post_id: data.post_id,
133 removed: Some(removed),
134 reason: data.reason.to_owned(),
136 blocking(context.pool(), move |conn| {
137 ModRemovePost::create(conn, &form)
142 let community = blocking(context.pool(), move |conn| {
143 Community::read(conn, orig_post.community_id)
147 &local_user_view.person.clone().into(),
149 updated_post.ap_id.into(),
150 data.reason.clone().unwrap_or_else(|| "".to_string()),
156 send_post_ws_message(
158 UserOperationCrud::RemovePost,
160 Some(local_user_view.person.id),