Skip to content

A platform for community discussion. Free, open, simple.

FIX: Apply one_per_topic only from category's allowed tag groups (#37004)

Alan Guo Xiang Tan01c915bb99ea
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index bb69511ec9d..6423506ae8c 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -419,8 +419,8 @@ module DiscourseTagging
def self.filter_allowed_tags(guardian, opts = {})
selected_tag_ids = opts[:selected_tags] ? Tag.where_name(opts[:selected_tags]).pluck(:id) : []
category = opts[:category]
- category_has_restricted_tags =
- category ? (category.tags.count > 0 || category.tag_groups.count > 0) : false
+ category_has_tag_groups = category && category.tag_groups.count > 0
+ category_has_restricted_tags = category_has_tag_groups || (category && category.tags.count > 0)
# If guardian is nil, it means the caller doesn't want tags to be filtered
# based on guardian rules. Use the same rules as for staff users.
@@ -562,13 +562,22 @@ module DiscourseTagging
end
if builder_params[:selected_tag_ids] && (opts[:for_input] || opts[:for_topic])
- one_tag_per_group_ids = DB.query(<<~SQL, builder_params[:selected_tag_ids]).map(&:id)
+ one_tag_per_group_sql = +<<~SQL
SELECT DISTINCT(tg.id)
FROM tag_groups tg
INNER JOIN tag_group_memberships tgm ON tg.id = tgm.tag_group_id AND tgm.tag_id IN (?)
WHERE tg.one_per_topic
SQL
+ query_params = [builder_params[:selected_tag_ids]]
+
+ if category_has_tag_groups
+ one_tag_per_group_sql << "AND tg.id IN (SELECT tag_group_id FROM category_tag_groups WHERE category_id = ?)"
+ query_params << category.id
+ end
+
+ one_tag_per_group_ids = DB.query(one_tag_per_group_sql, *query_params).map(&:id)
+
if one_tag_per_group_ids.present?
builder.where(
"t.id NOT IN (SELECT DISTINCT tag_id FROM tag_group_restrictions WHERE tag_group_id IN (?)) OR id IN (:selected_tag_ids)",
diff --git a/spec/lib/discourse_tagging_spec.rb b/spec/lib/discourse_tagging_spec.rb
index bc7a1433dad..9f0b4a97bdb 100644
--- a/spec/lib/discourse_tagging_spec.rb
+++ b/spec/lib/discourse_tagging_spec.rb
@@ -889,6 +889,62 @@ RSpec.describe DiscourseTagging do
).to be_empty
end
end
+
+ context "with tags in multiple groups" do
+ fab!(:shared_tag1) { Fabricate(:tag, name: "shared1") }
+ fab!(:shared_tag2) { Fabricate(:tag, name: "shared2") }
+ fab!(:unrestricted_tag_group) { Fabricate(:tag_group, tags: [shared_tag1, shared_tag2]) }
+ fab!(:category_tag_group) { Fabricate(:tag_group, tags: [shared_tag1, shared_tag2]) }
+ fab!(:category) { Fabricate(:category, allowed_tag_groups: [category_tag_group.name]) }
+
+ it "correctly respects enabled one_per_topic option from tag_group that is restricted to a category" do
+ category_tag_group.update!(one_per_topic: true)
+ unrestricted_tag_group.update!(one_per_topic: false)
+
+ allowed =
+ DiscourseTagging.filter_allowed_tags(
+ Guardian.new(user),
+ for_input: true,
+ category: category,
+ )
+
+ expect(allowed.map(&:name)).to contain_exactly("shared1", "shared2")
+
+ allowed_after_selection =
+ DiscourseTagging.filter_allowed_tags(
+ Guardian.new(user),
+ for_input: true,
+ category: category,
+ selected_tags: ["shared1"],
+ )
+
+ expect(allowed_after_selection.map(&:name)).to be_empty
+ end
+
+ it "does not apply enabled one_per_topic option from other tag groups when category restricts to a tag group that disables one_per_topic option" do
+ category_tag_group.update!(one_per_topic: false)
+ unrestricted_tag_group.update!(one_per_topic: true)
+
+ allowed =
+ DiscourseTagging.filter_allowed_tags(
+ Guardian.new(user),
+ for_input: true,
+ category: category,
+ )
+
+ expect(allowed.map(&:name)).to contain_exactly("shared1", "shared2")
+
+ allowed_after_selection =
+ DiscourseTagging.filter_allowed_tags(
+ Guardian.new(user),
+ for_input: true,
+ category: category,
+ selected_tags: ["shared1"],
+ )
+
+ expect(allowed_after_selection.map(&:name)).to contain_exactly("shared2")
+ end
+ end
end
end