A platform for community discussion. Free, open, simple.
FIX: Resolve tag synonyms and reject max_results:0 in post filters
Régis Hanol9b2983c2f6f9
diff --git a/frontend/discourse/app/lib/filter-suggestions.js b/frontend/discourse/app/lib/filter-suggestions.jsindex 082345494eb..e5019208358 100644--- a/frontend/discourse/app/lib/filter-suggestions.js+++ b/frontend/discourse/app/lib/filter-suggestions.js@@ -570,6 +570,7 @@ class FilterTypeValueSuggester {];return options+ .filter((opt) => Number(opt.value) >= (this.tip.min ?? 0)).filter((opt) => !this.searchTerm || opt.value.includes(this.searchTerm)).map((opt) => ({name: this.buildSuggestionName(opt.value),diff --git a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js b/frontend/discourse/tests/unit/lib/filter-suggestions-test.jsindex 9f7800cb782..71cb7343ccd 100644--- a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js+++ b/frontend/discourse/tests/unit/lib/filter-suggestions-test.js@@ -246,6 +246,22 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {assert.true(names.includes("status:unsolved"), "includes status:unsolved");});+ test("number suggestions respect the minimum", async function (assert) {+ const tips = [{ name: "max_results:", type: "number", min: 1 }];++ const { suggestions } = await FilterSuggestions.getSuggestions(+ "max_results:",+ tips,+ buildContext()+ );++ assert.deepEqual(+ suggestions.map((suggestion) => suggestion.term),+ ["1", "5", "10", "20"],+ "omits values below the minimum"+ );+ });+test("top-level tips are limited to 20", async function (assert) {const many = [];for (let i = 0; i < 30; i++) {diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex c824664a376..80c19b8ffe4 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -746,6 +746,17 @@ module DiscourseTaggingfilter_visible_in_accessible_categories(permitted, guardian)end+ def self.visible_tag_ids_resolving_synonyms(tag_names, guardian = nil)+ tag_ids =+ filter_visible(Tag, guardian)+ .where_name(tag_names)+ .pluck(:id, :target_tag_id)+ .map { |id, target_tag_id| target_tag_id || id }+ .uniq++ filter_visible(Tag.where(id: tag_ids), guardian).pluck(:id)+ end+def self.filter_visible(query, guardian = nil)guardian&.is_admin? ? query : query.where(id: visible_tags(guardian).select(:id))enddiff --git a/lib/posts_filter.rb b/lib/posts_filter.rbindex 18cf0359215..cb7395740bb 100644--- a/lib/posts_filter.rb+++ b/lib/posts_filter.rb@@ -44,10 +44,8 @@ class PostsFilternorepliessingle_user].freeze- CUSTOM_FILTER_PREFIXES = [nil, ""].freeze- private_constant :CUSTOM_FILTER_PREFIXES- attr_reader :term, :filters, :order, :guardian, :limit, :offset, :invalid_filters+ attr_reader :invalid_filtersdef self.add_filter(name, aliases: [], enabled: -> { true }, &block)raise ArgumentError, "block is required" if block.blank?@@ -68,10 +66,6 @@ class PostsFiltercustom_filters.find { |name, config| name == key || config[:aliases].include?(key) }&.lastend- def self.word_to_date(str)- ::Search.word_to_date(str)- end-def self.category_ids_from_param(category_param, exact: false)category_param.to_s@@ -241,6 +235,7 @@ class PostsFiltername: "max_results:",description: I18n.t("posts_filter.description.max_results"),type: "number",+ min: 1,},]@@ -266,14 +261,6 @@ class PostsFiltersearchend- def set_order!(order)- @order = order- end-- def limit_by_user!(limit)- @limit = limit if limit.to_i < @limit.to_i || @limit.nil?- end-def searchbase_relation = secure_base_relationfiltered = filtered_relation(base_relation)@@ -285,6 +272,10 @@ class PostsFilterprivate+ def limit_by_user!(limit)+ @limit = limit if limit.to_i < @limit.to_i || @limit.nil?+ end+def reset_filter_state!@filters = []@limit = @initial_limit@@ -460,13 +451,12 @@ class PostsFilterwhen "post_type"!parsed_filter[:exclude] && POST_TYPE_VALUES.include?(value.downcase)when "max_results"- !parsed_filter[:exclude] && value.match?(/\A\d+\z/)+ !parsed_filter[:exclude] && value.match?(/\A[1-9]\d*\z/)when "order"!parsed_filter[:exclude] && ORDER_VALUES.include?(value.downcase)elsecustom_filter = self.class.custom_filter_for(key)- custom_filter.present? && CUSTOM_FILTER_PREFIXES.include?(parsed_filter[:prefix]) &&- custom_filter[:enabled].call+ custom_filter.present? && parsed_filter[:prefix].empty? && custom_filter[:enabled].callendend@@ -498,7 +488,7 @@ class PostsFilterlimit_by_user!(parsed_filter[:value].to_i)relationwhen "order"- set_order!(order_value(parsed_filter[:value]))+ @order = order_value(parsed_filter[:value])relationwhen "topics"filter_topics(relation, parsed_filter)@@ -539,7 +529,7 @@ class PostsFilterenddef filter_by_date(relation, condition, date_str)- if date = self.class.word_to_date(date_str)+ if date = ::Search.word_to_date(date_str)relation.where(condition, date)elserelation@@ -550,7 +540,7 @@ class PostsFiltertag_names = split_values(parsed_filter[:value])return relation if tag_names.empty?- tag_ids = DiscourseTagging.filter_visible(Tag, @guardian).where_name(tag_names).pluck(:id)+ tag_ids = DiscourseTagging.visible_tag_ids_resolving_synonyms(tag_names, @guardian)return relation.where("1 = 0") if tag_ids.empty? && !parsed_filter[:exclude]return relation if tag_ids.empty?diff --git a/spec/lib/posts_filter_spec.rb b/spec/lib/posts_filter_spec.rbindex fc304a0d30a..5fc2739c803 100644--- a/spec/lib/posts_filter_spec.rb+++ b/spec/lib/posts_filter_spec.rb@@ -93,6 +93,7 @@ RSpec.describe PostsFilter doexpect(options.find { |option| option[:name] == "topic:" }).to include(prefixes: [{ name: "-", description: I18n.t("posts_filter.description.exclude_topic") }],)+ expect(options.find { |option| option[:name] == "max_results:" }).to include(min: 1)expect(options.find { |option| option[:name] == "order:" }).not_to include(:type,:extra_entries,@@ -100,6 +101,13 @@ RSpec.describe PostsFilter doendit "filters posts by tags and categories" do+ Fabricate(:tag, name: "feature-request", target_tag: feature_tag)++ expect(filtered_post_ids("tag:feature-request")).to contain_exactly(+ feature_post.id,+ feature_bug_post.id,+ )+expect(filtered_post_ids("tag:feature")).to contain_exactly(feature_post.id,feature_bug_post.id,@@ -114,6 +122,19 @@ RSpec.describe PostsFilter do)end+ it "excludes hidden tag targets reached through visible synonyms" do+ synonym = Fabricate(:tag, target_tag: feature_tag)+ announcement_category.update!(allowed_tags: [synonym.name])+ private_category = Fabricate(:private_category, group: Group[:staff])+ private_category.update!(allowed_tags: [feature_tag.name])++ expect(filtered_post_ids("tag:#{synonym.name}", guardian: user.guardian)).to be_empty+ expect(filtered_post_ids("tag:#{synonym.name}", guardian: admin.guardian)).to contain_exactly(+ feature_post.id,+ feature_bug_post.id,+ )+ end+it "supports exact and nested category matching" dochild_category =Fabricate(@@ -214,7 +235,7 @@ RSpec.describe PostsFilter do)expect(filtered_post_ids("post_type:reply")).to contain_exactly(reply_post.id)expect(filtered_post_ids("status:closed")).to contain_exactly(bug_post.id)- expect(filtered_post_ids("after:#{2.days.ago.to_date}")).to include(feature_bug_post.id)+ expect(filtered_post_ids("after:#{2.days.ago.to_date}")).not_to include(feature_post.id)expect(filtered_post_ids("topic_before:#{3.days.ago.to_date}")).to contain_exactly(feature_post.id,reply_post.id,@@ -423,9 +444,13 @@ RSpec.describe PostsFilter doendit "tracks invalid filter fragments" do- filter = described_class.new("invalidfilter tag:feature order:missing")+ filter = described_class.new("invalidfilter tag:feature order:missing max_results:0")- expect(filter.invalid_filters).to contain_exactly("invalidfilter", "order:missing")+ expect(filter.invalid_filters).to contain_exactly(+ "invalidfilter",+ "order:missing",+ "max_results:0",+ )endit "omits secure categories without a guardian and always omits PMs" do