Skip to content

A platform for community discussion. Free, open, simple.

FIX: Dont 404 nested view when target post is soft-deleted (#39613)

Mark VanLandingham2e24246373bf
diff --git a/app/services/nested_topic/show_context.rb b/app/services/nested_topic/show_context.rb
index e9a73246c41..bbce52d7291 100644
--- a/app/services/nested_topic/show_context.rb
+++ b/app/services/nested_topic/show_context.rb
@@ -55,10 +55,12 @@ class NestedTopic::ShowContext
end
def fetch_target_post(params:, topic_view:, loader:)
- post = topic_view.topic.posts.find_by(post_number: params.target_post_number)
- return if post.nil?
- return if loader.visible_post_types.exclude?(post.post_type)
- post
+ # apply_visibility unscopes deleted_at and filters by visible_post_types,
+ # matching the rest of the tree-loading code. Without this, a link to a
+ # since-soft-deleted post (e.g. a stale last_read_post_number from
+ # suggested topics) would 404 even though the deleted_post_placeholder
+ # path in the serializer is designed to render it.
+ loader.apply_visibility(topic_view.topic.posts).find_by(post_number: params.target_post_number)
end
def should_walk_ancestors(params:, target_post:)
diff --git a/spec/services/nested_topic/show_context_spec.rb b/spec/services/nested_topic/show_context_spec.rb
index 41e16fb9cde..7c10d3a1057 100644
--- a/spec/services/nested_topic/show_context_spec.rb
+++ b/spec/services/nested_topic/show_context_spec.rb
@@ -45,6 +45,18 @@ RSpec.describe NestedTopic::ShowContext do
it { is_expected.to fail_to_find_a_model(:target_post) }
end
+ context "when target post is soft-deleted" do
+ before { PostDestroyer.new(Discourse.system_user, target, context: "spec").destroy }
+
+ it { is_expected.to run_successfully }
+
+ it "resolves the deleted post and serializes it via the placeholder path" do
+ response = result[:response]
+ expect(response[:target_post][:id]).to eq(target.id)
+ expect(response[:target_post][:deleted_post_placeholder]).to eq(true)
+ end
+ end
+
context "when target is a root post" do
it { is_expected.to run_successfully }