1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{DeletePost, PostResponse},
7 send_activity::{ActivityChannel, SendActivityData},
8 utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
10 use lemmy_db_schema::{
11 source::post::{Post, PostUpdateForm},
14 use lemmy_utils::error::{LemmyError, LemmyErrorType};
16 #[tracing::instrument(skip(context))]
17 pub async fn delete_post(
18 data: Json<DeletePost>,
19 context: Data<LemmyContext>,
20 ) -> Result<Json<PostResponse>, LemmyError> {
21 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
23 let post_id = data.post_id;
24 let orig_post = Post::read(&mut context.pool(), post_id).await?;
26 // Dont delete it if its already been deleted.
27 if orig_post.deleted == data.deleted {
28 return Err(LemmyErrorType::CouldntUpdatePost)?;
32 local_user_view.person.id,
33 orig_post.community_id,
37 check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?;
39 // Verify that only the creator can delete
40 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
41 return Err(LemmyErrorType::NoPostEditAllowed)?;
45 let post = Post::update(
49 deleted: Some(data.deleted),
55 let person_id = local_user_view.person.id;
56 ActivityChannel::submit_activity(
57 SendActivityData::DeletePost(post, local_user_view.person, data.0.clone()),
62 build_post_response(&context, orig_post.community_id, person_id, data.post_id).await