From: RocketDerp <113625597+RocketDerp@users.noreply.github.com>
Date: Mon, 17 Jul 2023 15:07:45 +0000 (-0700)
Subject: Post remove delete federation outbound fix0 (#3613)
X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/README.md?a=commitdiff_plain;h=38c621091273822f798dfd75046b9ce724cf4bc1;p=lemmy.git

Post remove delete federation outbound fix0 (#3613)

* add new function build_post_response_deleted_allowed

* PostDelete uses new function build_post_response_deleted_allowed

* RemovePost uses new build_post_response_deleted_allowed function

* code comments about mod or admin flag having other use

* reformat "cargo +nightly fmt --all"
---

diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs
index 8b96206c..acb7355b 100644
--- a/crates/api_common/src/build_response.rs
+++ b/crates/api_common/src/build_response.rs
@@ -82,6 +82,19 @@ pub async fn build_post_response(
   Ok(PostResponse { post_view })
 }
 
+// this is a variation of build_post_response that returns post even if end-user-delted or mod-removed.
+// Assumption is that before this function is ever called, the user is already confirmed to be authorized to view post.
+// See GitHub Issue #3588
+pub async fn build_post_response_deleted_allowed(
+  context: &Data<LemmyContext>,
+  _community_id: CommunityId,
+  person_id: PersonId,
+  post_id: PostId,
+) -> Result<PostResponse, LemmyError> {
+  let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), Some(true)).await?;
+  Ok(PostResponse { post_view })
+}
+
 // TODO: this function is a mess and should be split up to handle email seperately
 #[tracing::instrument(skip_all)]
 pub async fn send_local_notifs(
diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs
index eaeb66c4..fa1c0310 100644
--- a/crates/api_crud/src/post/delete.rs
+++ b/crates/api_crud/src/post/delete.rs
@@ -1,7 +1,7 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
-  build_response::build_post_response,
+  build_response::build_post_response_deleted_allowed,
   context::LemmyContext,
   post::{DeletePost, PostResponse},
   utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
@@ -52,7 +52,7 @@ impl PerformCrud for DeletePost {
     )
     .await?;
 
-    build_post_response(
+    build_post_response_deleted_allowed(
       context,
       orig_post.community_id,
       local_user_view.person.id,
diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs
index 7950d504..8feff19f 100644
--- a/crates/api_crud/src/post/remove.rs
+++ b/crates/api_crud/src/post/remove.rs
@@ -1,7 +1,7 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
-  build_response::build_post_response,
+  build_response::build_post_response_deleted_allowed,
   context::LemmyContext,
   post::{PostResponse, RemovePost},
   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
@@ -61,7 +61,7 @@ impl PerformCrud for RemovePost {
     };
     ModRemovePost::create(&mut context.pool(), &form).await?;
 
-    build_post_response(
+    build_post_response_deleted_allowed(
       context,
       orig_post.community_id,
       local_user_view.person.id,
diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs
index 0e6ad703..48faeca2 100644
--- a/crates/db_views/src/post_view.rs
+++ b/crates/db_views/src/post_view.rs
@@ -146,6 +146,7 @@ impl PostView {
       .into_boxed();
 
     // Hide deleted and removed for non-admins or mods
+    // Note: one special use case for this flag variable is when end-user-delete post or mod-removed post.
     if !is_mod_or_admin.unwrap_or(false) {
       query = query
         .filter(community::removed.eq(false))