From 37888d98184501b39cc1a829e9f7f03678128820 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 3 Feb 2020 13:34:35 -0500 Subject: [PATCH] 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. --- app/models/post.rb | 3 ++- spec/models/post_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index 44368e538a0..0e582145e35 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index a97b2d561b2..33204e30f29 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -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 }])