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, DeletableObjects};
12 use lemmy_db_schema::{
15 moderator::{ModRemovePost, ModRemovePostForm},
20 use lemmy_utils::{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;
27 #[tracing::instrument(skip(context, websocket_id))]
30 context: &Data<LemmyContext>,
31 websocket_id: Option<ConnectionId>,
32 ) -> Result<PostResponse, LemmyError> {
33 let data: &DeletePost = self;
35 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37 let post_id = data.post_id;
38 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
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"));
46 local_user_view.person.id,
47 orig_post.community_id,
51 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
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"));
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)
67 let community = blocking(context.pool(), move |conn| {
68 Community::read(conn, orig_post.community_id)
72 &local_user_view.person.clone().into(),
74 DeletableObjects::Post(Box::new(updated_post.into())),
82 UserOperationCrud::DeletePost,
84 Some(local_user_view.person.id),
91 #[async_trait::async_trait(?Send)]
92 impl PerformCrud for RemovePost {
93 type Response = PostResponse;
95 #[tracing::instrument(skip(context, websocket_id))]
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?;
105 let post_id = data.post_id;
106 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
109 local_user_view.person.id,
110 orig_post.community_id,
115 // Verify that only the mods can remove
118 local_user_view.person.id,
119 orig_post.community_id,
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)
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(),
138 blocking(context.pool(), move |conn| {
139 ModRemovePost::create(conn, &form)
144 let community = blocking(context.pool(), move |conn| {
145 Community::read(conn, orig_post.community_id)
149 &local_user_view.person.clone().into(),
151 DeletableObjects::Post(Box::new(updated_post.into())),
152 data.reason.clone().unwrap_or_else(|| "".to_string()),
158 send_post_ws_message(
160 UserOperationCrud::RemovePost,
162 Some(local_user_view.person.id),