1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
10 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
11 use lemmy_db_queries::{source::post::Post_, Crud};
12 use lemmy_db_schema::source::{community::Community, moderator::*, post::*};
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeletePost {
18 type Response = PostResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PostResponse, LemmyError> {
25 let data: &DeletePost = self;
26 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
28 let post_id = data.post_id;
29 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
32 local_user_view.person.id,
33 orig_post.community_id,
38 // Verify that only the creator can delete
39 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
40 return Err(ApiError::err("no_post_edit_allowed").into());
44 let post_id = data.post_id;
45 let deleted = data.deleted;
46 let updated_post = blocking(context.pool(), move |conn| {
47 Post::update_deleted(conn, post_id, deleted)
52 let community = blocking(context.pool(), move |conn| {
53 Community::read(conn, orig_post.community_id)
57 &local_user_view.person,
59 updated_post.ap_id.into(),
67 UserOperationCrud::DeletePost,
69 Some(local_user_view.person.id),
76 #[async_trait::async_trait(?Send)]
77 impl PerformCrud for RemovePost {
78 type Response = PostResponse;
82 context: &Data<LemmyContext>,
83 websocket_id: Option<ConnectionId>,
84 ) -> Result<PostResponse, LemmyError> {
85 let data: &RemovePost = self;
86 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
88 let post_id = data.post_id;
89 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
92 local_user_view.person.id,
93 orig_post.community_id,
98 // Verify that only the mods can remove
101 local_user_view.person.id,
102 orig_post.community_id,
107 let post_id = data.post_id;
108 let removed = data.removed;
109 let updated_post = blocking(context.pool(), move |conn| {
110 Post::update_removed(conn, post_id, removed)
115 let form = ModRemovePostForm {
116 mod_person_id: local_user_view.person.id,
117 post_id: data.post_id,
118 removed: Some(removed),
119 reason: data.reason.to_owned(),
121 blocking(context.pool(), move |conn| {
122 ModRemovePost::create(conn, &form)
127 let community = blocking(context.pool(), move |conn| {
128 Community::read(conn, orig_post.community_id)
132 &local_user_view.person,
134 updated_post.ap_id.into(),
135 data.reason.clone().unwrap_or_else(|| "".to_string()),
141 send_post_ws_message(
143 UserOperationCrud::RemovePost,
145 Some(local_user_view.person.id),