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_queries::{source::post::Post_, Crud};
13 use lemmy_db_schema::source::{community::Community, moderator::*, post::*};
14 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for DeletePost {
19 type Response = PostResponse;
23 context: &Data<LemmyContext>,
24 websocket_id: Option<ConnectionId>,
25 ) -> Result<PostResponse, LemmyError> {
26 let data: &DeletePost = self;
28 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
30 let post_id = data.post_id;
31 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
34 local_user_view.person.id,
35 orig_post.community_id,
39 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
41 // Verify that only the creator can delete
42 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
43 return Err(ApiError::err_plain("no_post_edit_allowed").into());
47 let post_id = data.post_id;
48 let deleted = data.deleted;
49 let updated_post = blocking(context.pool(), move |conn| {
50 Post::update_deleted(conn, post_id, deleted)
55 let community = blocking(context.pool(), move |conn| {
56 Community::read(conn, orig_post.community_id)
60 &local_user_view.person,
62 updated_post.ap_id.into(),
70 UserOperationCrud::DeletePost,
72 Some(local_user_view.person.id),
79 #[async_trait::async_trait(?Send)]
80 impl PerformCrud for RemovePost {
81 type Response = PostResponse;
85 context: &Data<LemmyContext>,
86 websocket_id: Option<ConnectionId>,
87 ) -> Result<PostResponse, LemmyError> {
88 let data: &RemovePost = self;
90 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
92 let post_id = data.post_id;
93 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
96 local_user_view.person.id,
97 orig_post.community_id,
102 // Verify that only the mods can remove
105 local_user_view.person.id,
106 orig_post.community_id,
111 let post_id = data.post_id;
112 let removed = data.removed;
113 let updated_post = blocking(context.pool(), move |conn| {
114 Post::update_removed(conn, post_id, removed)
119 let form = ModRemovePostForm {
120 mod_person_id: local_user_view.person.id,
121 post_id: data.post_id,
122 removed: Some(removed),
123 reason: data.reason.to_owned(),
125 blocking(context.pool(), move |conn| {
126 ModRemovePost::create(conn, &form)
131 let community = blocking(context.pool(), move |conn| {
132 Community::read(conn, orig_post.community_id)
136 &local_user_view.person,
138 updated_post.ap_id.into(),
139 data.reason.clone().unwrap_or_else(|| "".to_string()),
145 send_post_ws_message(
147 UserOperationCrud::RemovePost,
149 Some(local_user_view.person.id),