FIX: Termless hashtag search when a type is disabled (#22660)

When a type was disabled, the hashtag search _without_ a
term was erroring. This was because we weren't filtering
out the disabled types from types_in_priority_order first
like we were if there was a term provided.

This commit fixes that issue, and also makes it so
contexts_with_ordered_types and ordered_types_for_context
will only return hashtag types which are enabled.
This commit is contained in:
Martin Brennan 2023-07-19 10:10:33 +10:00 committed by GitHub
parent 2cc353104e
commit 54001060ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -57,7 +57,10 @@ class HashtagAutocompleteService
end
def self.ordered_types_for_context(context)
find_priorities_for_context(context).sort_by { |ctp| -ctp[:priority] }.map { |ctp| ctp[:type] }
find_priorities_for_context(context)
.sort_by { |ctp| -ctp[:priority] }
.map { |ctp| ctp[:type] }
.reject { |type| data_source_types.exclude?(type) }
end
def self.contexts_with_ordered_types
@ -230,16 +233,16 @@ class HashtagAutocompleteService
)
raise Discourse::InvalidParameters.new(:order) if !types_in_priority_order.is_a?(Array)
limit = [limit, SEARCH_MAX_LIMIT].min
types_in_priority_order =
types_in_priority_order.select do |type|
HashtagAutocompleteService.data_source_types.include?(type)
end
return search_without_term(types_in_priority_order, limit) if term.blank?
limited_results = []
top_ranked_type = nil
term = term.downcase
types_in_priority_order =
types_in_priority_order.select do |type|
HashtagAutocompleteService.data_source_types.include?(type)
end
# Float exact matches by slug to the top of the list, any of these will be excluded
# from further results.

View File

@ -39,6 +39,13 @@ RSpec.describe HashtagAutocompleteService do
{ "topic-composer" => %w[category tag], "awesome-composer" => %w[tag category] },
)
end
it "does not return types which have been disabled" do
SiteSetting.tagging_enabled = false
expect(HashtagAutocompleteService.contexts_with_ordered_types).to eq(
{ "topic-composer" => %w[category] },
)
end
end
describe ".data_source_icon_map" do
@ -296,6 +303,13 @@ RSpec.describe HashtagAutocompleteService do
],
)
end
it "does not error if a type provided for priority order has been disabled" do
SiteSetting.tagging_enabled = false
expect(service.search(nil, %w[category tag]).map(&:ref)).to eq(
%w[book-dome book-zone media book uncategorized the-book-club],
)
end
end
end