From 54001060eaca82196d9ce67531f3fdc319483b97 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Wed, 19 Jul 2023 10:10:33 +1000 Subject: [PATCH] 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. --- app/services/hashtag_autocomplete_service.rb | 13 ++++++++----- spec/services/hashtag_autocomplete_service_spec.rb | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/services/hashtag_autocomplete_service.rb b/app/services/hashtag_autocomplete_service.rb index bd40cb53213..65ee0a2e061 100644 --- a/app/services/hashtag_autocomplete_service.rb +++ b/app/services/hashtag_autocomplete_service.rb @@ -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. diff --git a/spec/services/hashtag_autocomplete_service_spec.rb b/spec/services/hashtag_autocomplete_service_spec.rb index 02789fb264e..6419472a2bc 100644 --- a/spec/services/hashtag_autocomplete_service_spec.rb +++ b/spec/services/hashtag_autocomplete_service_spec.rb @@ -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