FIX: Don't return post replies from other topics

It seems in some situations replies have been moved to other topics but
the `PostReply` table has not been updated. I will try and fix this in a
follow up PR, but for now this fix ensures that every time we ask a post
for its replies that we restrict it to the same topic.
This commit is contained in:
Robin Ward 2020-02-03 13:12:27 -05:00
parent ad2aa7b52c
commit f83362b05b
2 changed files with 9 additions and 2 deletions

View File

@ -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

View File

@ -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 }])