From f2cc05c6c6a21e28891e44943d5e22129a1972bf Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Tue, 10 Jul 2018 11:17:28 +0300 Subject: [PATCH] FIX: ignore self-quotes from the same post when saving (#6082) --- app/models/post.rb | 4 +++- app/models/quoted_post.rb | 2 ++ spec/models/post_spec.rb | 6 ++++++ spec/models/quoted_post_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index de539046ee5..8d4462a1059 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -630,7 +630,9 @@ class Post < ActiveRecord::Base raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote| args = parse_quote_into_arguments(quote) # If the topic attribute is present, ensure it's the same topic - temp_collector << args[:post] unless (args[:topic].present? && topic_id != args[:topic]) + if !(args[:topic].present? && topic_id != args[:topic]) && args[:post] != post_number + temp_collector << args[:post] + end end temp_collector.uniq! diff --git a/app/models/quoted_post.rb b/app/models/quoted_post.rb index 26cc80ef8d9..6726de0883c 100644 --- a/app/models/quoted_post.rb +++ b/app/models/quoted_post.rb @@ -19,6 +19,8 @@ class QuotedPost < ActiveRecord::Base next if topic_id == 0 || post_number == 0 next if uniq[[topic_id, post_number]] + next if post.topic_id == topic_id && post.post_number == post_number + uniq[[topic_id, post_number]] = true begin diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 9c89c807dda..9ea0db26dc2 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -744,6 +744,12 @@ describe Post do expect(reply.quoted_post_numbers).to be_blank end + it "doesn't find the quote in the same post" do + reply = Fabricate.build(:post, post_args.merge(post_number: 646)) + reply.raw = "[quote=\"EvilTrout, post:#{reply.post_number}, topic:#{post.topic_id}\"]hello[/quote]" + reply.extract_quoted_post_numbers + expect(reply.quoted_post_numbers).to be_blank + end end describe 'a new reply' do diff --git a/spec/models/quoted_post_spec.rb b/spec/models/quoted_post_spec.rb index b5cbee0acc0..7636a29d052 100644 --- a/spec/models/quoted_post_spec.rb +++ b/spec/models/quoted_post_spec.rb @@ -33,6 +33,28 @@ describe QuotedPost do expect(QuotedPost.where(post_id: post4.id).pluck(:quoted_post_id)).to contain_exactly(post1.id, post2.id, post5.id) end + it "doesn't count quotes from the same post" do + SiteSetting.queue_jobs = false + + topic = Fabricate(:topic) + post = create_post(topic: topic, post_number: 1, raw: "foo bar") + + post.cooked = <<-HTML + + HTML + post.save! + + QuotedPost.extract_from(post) + expect(QuotedPost.where(post_id: post.id).count).to eq(0) + expect(QuotedPost.where(quoted_post_id: post.id).count).to eq(0) + end + it 'correctly handles deltas' do post1 = Fabricate(:post) post2 = Fabricate(:post)