Skip to content

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

FIX: Don't return post replies from other topics

Robin Wardf83362b05b11
diff --git a/app/models/post.rb b/app/models/post.rb
index 96027645d10..44368e538a0 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -819,9 +819,11 @@ class Post < ActiveRecord::Base
UNION
SELECT reply_post_id, level + 1
FROM post_replies AS r
+ JOIN posts AS p ON p.id = reply_post_id
JOIN breadcrumb AS b ON (r.post_id = b.id)
WHERE r.post_id <> r.reply_post_id
- AND b.level < :max_reply_level
+ AND b.level < :max_reply_level
+ AND p.topic_id = :topic_id
), breadcrumb_with_count AS (
SELECT
id,
@@ -844,7 +846,7 @@ class Post < ActiveRecord::Base
# for example it skips a post when it contains 2 quotes (which are replies) from different posts
builder.where("count = 1") if only_replies_to_single_post
- replies = builder.query_hash(post_id: id, max_reply_level: MAX_REPLY_LEVEL)
+ replies = builder.query_hash(post_id: id, max_reply_level: MAX_REPLY_LEVEL, topic_id: topic_id)
replies.each { |r| r.symbolize_keys! }
secured_ids = Post.secured(guardian).where(id: replies.map { |r| r[:id] }).pluck(:id).to_set
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 804ee527ade..a97b2d561b2 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -951,6 +951,11 @@ describe Post do
expect(p6.reply_ids).to be_empty # quotes itself
end
+ it "ignores posts moved to other topics" do
+ p2.update_column(:topic_id, Fabricate(:topic).id)
+ expect(p1.reply_ids).to be_blank
+ end
+
it "does not skip any replies" do
expect(p1.reply_ids(only_replies_to_single_post: false)).to eq([{ id: p2.id, level: 1 }, { id: p4.id, level: 2 }, { id: p5.id, level: 3 }, { id: p6.id, level: 2 }])
expect(p2.reply_ids(only_replies_to_single_post: false)).to eq([{ id: p4.id, level: 1 }, { id: p5.id, level: 2 }, { id: p6.id, level: 1 }])