FIX: Never return the same reply more than once via reply_ids

If our reply tree somehow ends up with cycles or other odd
structures, we only want to consider a reply once, at the first
level in the tree that it appears.
This commit is contained in:
Robin Ward 2020-02-03 13:34:35 -05:00
parent 7640914552
commit 37888d9818
2 changed files with 7 additions and 1 deletions

View File

@ -834,9 +834,10 @@ class Post < ActiveRecord::Base
WHERE r.reply_post_id <> r.post_id
GROUP BY id, level
)
SELECT id, level
SELECT id, MIN(level) AS level
FROM breadcrumb_with_count
/*where*/
GROUP BY id
ORDER BY id
SQL

View File

@ -956,6 +956,11 @@ describe Post do
expect(p1.reply_ids).to be_blank
end
it "doesn't include the same reply twice" do
PostReply.create!(post: p4, reply: p1)
expect(p1.reply_ids.size).to eq(4)
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 }])