A platform for community discussion. Free, open, simple.
FIX: don't mislabel allowed tags as disabled in composer tag search (#39300)
Régis HANOL480b13c711c7
diff --git a/app/services/tags/search.rb b/app/services/tags/search.rbindex 33688bda826..f483d8b28d4 100644--- a/app/services/tags/search.rb+++ b/app/services/tags/search.rb@@ -114,11 +114,11 @@ class Tags::Searchcontext[:forbidden_message] = nilend- def append_disabled_tags(params:, tags:, guardian:)+ def append_disabled_tags(params:, category:, tags:, guardian:)selected_ids = params.resolved_selected_tag_idsskip_ids = tags.map { |t| t[:id] } | selected_ids- excluded_tags =+ candidate_tags =DiscourseTagging.filter_visible(Tag.where("position(LOWER(?) IN LOWER(tags.name)) <> 0", params.term),@@ -126,6 +126,11 @@ class Tags::Search).where.not(id: skip_ids).limit(SiteSetting.max_tag_search_results)+ .to_a++ return if candidate_tags.empty?++ excluded_tags = reject_allowed_tags(candidate_tags, params:, category:, tags:, guardian:)disabled =excluded_tags.map do |tag|@@ -136,11 +141,12 @@ class Tags::Searchcontext[:tags] = tags.concat(disabled) if disabled.present?end- def detect_forbidden_tag(params:, tags:, guardian:)+ def detect_forbidden_tag(params:, category:, tags:, guardian:)return if tags.any? { |h| h[:name].downcase == params.term.downcase }tag = Tag.where_name(params.term).firstreturn unless tag && guardian.can_see_tag?(tag)+ return if reject_allowed_tags([tag], params:, category:, tags:, guardian:).empty?context[:forbidden] = params.qcontext[:forbidden_message] = explain_exclusion(@@ -151,6 +157,23 @@ class Tags::Search)end+ def reject_allowed_tags(candidates, params:, category:, tags:, guardian:)+ return candidates unless params.capped_limit && tags.size >= params.capped_limit++ only_tag_names = candidates.map(&:name)++ allowed_names =+ DiscourseTagging+ .filter_allowed_tags(+ guardian,+ **params.filter_options.merge(category:, only_tag_names:, limit: nil),+ )+ .map(&:name)+ .to_set++ candidates.reject { |tag| allowed_names.include?(tag.name) }+ end+def explain_exclusion(tag, params, selected_ids, guardian)if params.excludeSynonyms && tag.synonym?return I18n.t("tags.forbidden.synonym", tag_name: tag.target_tag.name)diff --git a/spec/services/tags/search_spec.rb b/spec/services/tags/search_spec.rbindex 2334ef7b7ce..7bdd7948f7d 100644--- a/spec/services/tags/search_spec.rb+++ b/spec/services/tags/search_spec.rb@@ -117,7 +117,7 @@ RSpec.describe(Tags::Search) dofab!(:parent_tag) { Fabricate(:tag, name: "parent") }fab!(:child_tag) { Fabricate(:tag, name: "childtag") }fab!(:tag_group) do- Fabricate(:tag_group, name: "Child Group", parent_tag: parent_tag, tags: [child_tag])+ Fabricate(:tag_group, name: "Child Group", parent_tag:, tags: [child_tag])endlet(:params) { { q: "childtag", filterForInput: true } }@@ -139,7 +139,7 @@ RSpec.describe(Tags::Search) dofab!(:category)fab!(:restricted_tag) { Fabricate(:tag, name: "restricted") }- before { CategoryTag.create!(category: category, tag: restricted_tag) }+ before { CategoryTag.create!(category:, tag: restricted_tag) }let(:params) { { q: "restricted", filterForInput: true } }@@ -156,9 +156,34 @@ RSpec.describe(Tags::Search) doendend+ context "when allowed tags are cut off by the limit" do+ fab!(:tag_foo1) { Fabricate(:tag, name: "foomatch1") }+ fab!(:tag_foo2) { Fabricate(:tag, name: "foomatch2") }++ let(:params) { { q: "foomatch", filterForInput: true, limit: 1 } }++ it "does not mislabel allowed tags as disabled" do+ disabled = result[:tags].select { |t| t[:disabled] }+ expect(disabled).to be_empty+ end+ end++ context "when an exact-match allowed tag is cut off by the limit" do+ fab!(:tag_exact) { Fabricate(:tag, name: "exacthit") }+ fab!(:tag_noise1) { Fabricate(:tag, name: "aexacthit") }+ fab!(:tag_noise2) { Fabricate(:tag, name: "bexacthit") }++ let(:params) { { q: "exacthit", filterForInput: true, limit: 1 } }++ it "does not mark an allowed tag as forbidden" do+ expect(result[:forbidden]).to be_nil+ expect(result[:forbidden_message]).to be_nil+ end+ end+context "when a forbidden tag is detected" dofab!(:target_tag) { Fabricate(:tag, name: "maintag") }- fab!(:synonym_tag) { Fabricate(:tag, name: "syntag", target_tag: target_tag) }+ fab!(:synonym_tag) { Fabricate(:tag, name: "syntag", target_tag:) }let(:params) { { q: "syntag", excludeSynonyms: true } }