From c934a0f7592d346e8ef10f2ea9dffb0af56b9e45 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Tue, 22 Sep 2020 07:53:12 +1000 Subject: [PATCH] FIX: don't error Topic#similar_to when prepared raw is blank (#10711) If raw contains incorrect URL, `prepare_data` returns empty string: https://github.com/discourse/discourse/blob/master/lib/search.rb#L91 Therefore we should not only check if the cooked post is not blank but also if prepared data is not blank. --- app/models/topic.rb | 6 ++++-- spec/models/topic_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 3e8c333c36c..67a4ab2f2c6 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -597,9 +597,11 @@ class Topic < ActiveRecord::Base PrettyText.cook(raw[0...MAX_SIMILAR_BODY_LENGTH].strip) ) - if cooked.present? + prepared_data = cooked.present? && Search.prepare_data(cooked) + + if prepared_data.present? raw_tsquery = Search.set_tsquery_weight_filter( - Search.prepare_data(cooked), + prepared_data, 'B' ) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 72d1583790d..7ad36476dfb 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -519,6 +519,10 @@ describe Topic do expect(Topic.similar_to('some title', '#')).to eq([]) end + it 'does not result in invalid statement when prepared data is blank' do + expect(Topic.similar_to('some title', 'https://discourse.org/#INCORRECT#URI')).to be_empty + end + context 'with a similar topic' do fab!(:post) { SearchIndexer.enable