mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Miscellaneous tagging errors (#21490)
* FIX: Displaying the wrong number of minimum tags in the composer When the minimum number of tags set for the category is larger than the minimum number of tags set in the category tag-groups, the composer was displaying the wrong value. This commit fixes the value displayed in the composer to show the max value between the required for the category and the tag-groups set for the category. This bug was reported on Meta in https://meta.discourse.org/t/tags-from-multiple-tag-groups-required-only-suggest-select-at-least-one-tag/263817 * FIX: Limiting tags in categories not working as expected When a category was restricted to a tag group A, which was set to only allow one tag from the group per topic, selecting a tag belonging only to A returned other tags from A that also belonged to other group/s (if any). Example: Tag group A: alpha, beta, gamma, epsilon, delta Tag group B: alpha, beta, gamma Both tag groups set to only allow one tag from the group per topic. If Category 1 was set to only allow tags from the tag group A, and the first tag selected was epsilon, then, because they also belonged to tag group B, the tags alpha, beta, and gamma were still returned as valid options when they should not be. This commit ensures that once a tag from a tag group that restricts its tags to one per topic is selected, no other tag from this group is returned. This bug was reported on Meta in https://meta.discourse.org/t/limiting-tags-to-categories-not-working-as-expected/263143. * FIX: Moving topics does not prompt to add required tag for new category When a topic moved from a category to another, the tag requirements of the new category were not being checked. This allowed a topic to be created and moved to a category: - that limited the tags to a tag group, with the topic containing tags not allowed. - that required N tags from a tag group, with the topic not containing the required tags. This bug was reported on Meta in https://meta.discourse.org/t/moving-tagged-topics-does-not-prompt-to-add-required-tag-for-new-category/264138. * FIX: Editing topics with tag groups from parents allows incorrect tagging When there was a combination between parent tags defined in a tag group set to allow only one tag from the group per topic, and other tag groups relying on this restriction to combine the children tag types with the parent tag, editing a topic could allow the user to insert an invalid combination of these tags. Example: Automakers tag group: landhover, toyota - group set to limit one tag from the group per topic Toyota models group: land-cruiser, hilux, corolla Landhover models group: evoque, defender, discovery If a topic was initially set up with the tags toyota, land-cruiser it was possible to edit it by removing the tag toyota and adding the tag landhover and other landhover model tags like evoque for example. In this case, the topic would end up with the tags toyota, land-cruiser, landhover, evoque because Discourse will automatically insert the missing parent tag toyota when it detects the tag land-cruiser. This combination of tags would violate the restriction specified in the Automakers tag group resulting in an invalid combination of tags. This commit enforces that the "one tag from the group per topic" restriction is verified before updating the topic tags and also make sure the verification checks the compatibility of parent tags that would be automatically inserted. After the changes, the user will receive an error similar to: The tags land-cruiser, landhover cannot be used simultaneously. Please include only one of them.
This commit is contained in:
@@ -102,6 +102,12 @@ module DiscourseTagging
|
||||
end
|
||||
end
|
||||
|
||||
# tests if there are conflicts between tags on tag groups that only allow one tag from the group before adding
|
||||
# mandatory parent tags because later we want to test if the mandatory parent tags introduce any conflicts
|
||||
# and be able to pinpoint the tag that is introducing it
|
||||
# guardian like above is nil to prevent stripping tags that already passed validation
|
||||
return false unless validate_one_tag_from_group_per_topic(nil, topic, category, tags)
|
||||
|
||||
# add missing mandatory parent tags
|
||||
tag_ids = tags.map(&:id)
|
||||
|
||||
@@ -132,7 +138,57 @@ module DiscourseTagging
|
||||
.compact
|
||||
.uniq
|
||||
|
||||
tags = tags + Tag.where(id: missing_parent_tag_ids).all unless missing_parent_tag_ids.empty?
|
||||
missing_parent_tags = Tag.where(id: missing_parent_tag_ids).all
|
||||
|
||||
tags = tags + missing_parent_tags unless missing_parent_tags.empty?
|
||||
|
||||
parent_tag_conflicts =
|
||||
filter_tags_violating_one_tag_from_group_per_topic(
|
||||
nil, # guardian like above is nil to prevent stripping tags that already passed validation
|
||||
topic.category,
|
||||
tags,
|
||||
)
|
||||
|
||||
if parent_tag_conflicts.present?
|
||||
# we need to get the original tag names that introduced conflicting missing parent tags to return an useful
|
||||
# error message
|
||||
parent_child_names_map = {}
|
||||
parent_tags_map.each do |tag_id, parent_tag_ids|
|
||||
next if (tag_ids & parent_tag_ids).size > 0 # tag already has a parent tag
|
||||
|
||||
parent_tag = tags.select { |t| t.id == parent_tag_ids.first }.first
|
||||
original_child_tag = tags.select { |t| t.id == tag_id }.first
|
||||
|
||||
next unless parent_tag.present? && original_child_tag.present?
|
||||
parent_child_names_map[parent_tag.name] = original_child_tag.name
|
||||
end
|
||||
|
||||
# replaces the added missing parent tags with the original tag
|
||||
parent_tag_conflicts.map do |_, conflicting_tags|
|
||||
topic.errors.add(
|
||||
:base,
|
||||
I18n.t(
|
||||
"tags.limited_to_one_tag_from_group",
|
||||
tags:
|
||||
conflicting_tags
|
||||
.map do |tag|
|
||||
tag_name = tag.name
|
||||
|
||||
if parent_child_names_map[tag_name].present?
|
||||
parent_child_names_map[tag_name]
|
||||
else
|
||||
tag_name
|
||||
end
|
||||
end
|
||||
.uniq
|
||||
.sort
|
||||
.join(", "),
|
||||
),
|
||||
)
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
return false unless validate_min_required_tags_for_category(guardian, topic, category, tags)
|
||||
return false unless validate_required_tags_from_group(guardian, topic, category, tags)
|
||||
@@ -163,6 +219,19 @@ module DiscourseTagging
|
||||
false
|
||||
end
|
||||
|
||||
def self.validate_category_tags(guardian, model, category, tags = [])
|
||||
existing_tags = tags.present? ? Tag.where(name: tags) : []
|
||||
valid_tags = guardian.can_create_tag? ? tags : existing_tags
|
||||
|
||||
# all add to model (topic) errors
|
||||
valid = validate_min_required_tags_for_category(guardian, model, category, valid_tags)
|
||||
valid &&= validate_required_tags_from_group(guardian, model, category, existing_tags)
|
||||
valid &&= validate_category_restricted_tags(guardian, model, category, valid_tags)
|
||||
valid &&= validate_one_tag_from_group_per_topic(guardian, model, category, valid_tags)
|
||||
|
||||
valid
|
||||
end
|
||||
|
||||
def self.validate_min_required_tags_for_category(guardian, model, category, tags = [])
|
||||
if !guardian.is_staff? && category && category.minimum_required_tags > 0 &&
|
||||
tags.length < category.minimum_required_tags
|
||||
@@ -250,6 +319,54 @@ module DiscourseTagging
|
||||
true
|
||||
end
|
||||
|
||||
def self.validate_one_tag_from_group_per_topic(guardian, model, category, tags = [])
|
||||
tags_cant_be_used = filter_tags_violating_one_tag_from_group_per_topic(guardian, category, tags)
|
||||
|
||||
return true if tags_cant_be_used.blank?
|
||||
|
||||
tags_cant_be_used.each do |_, incompatible_tags|
|
||||
model.errors.add(
|
||||
:base,
|
||||
I18n.t(
|
||||
"tags.limited_to_one_tag_from_group",
|
||||
tags: incompatible_tags.map(&:name).sort.join(", "),
|
||||
),
|
||||
)
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def self.filter_tags_violating_one_tag_from_group_per_topic(guardian, category, tags = [])
|
||||
return [] if tags.size < 2
|
||||
|
||||
# ensures that tags are a list of tag names
|
||||
tags = tags.map(&:name) if Tag === tags[0]
|
||||
|
||||
allowed_tags =
|
||||
filter_allowed_tags(
|
||||
guardian,
|
||||
category: category,
|
||||
only_tag_names: tags,
|
||||
for_topic: true,
|
||||
order_search_results: true,
|
||||
)
|
||||
|
||||
return {} if allowed_tags.size < 2
|
||||
|
||||
tags_by_group_map =
|
||||
allowed_tags
|
||||
.sort_by { |tag| [tag.tag_group_id || -1, tag.name] }
|
||||
.inject({}) do |hash, tag|
|
||||
next hash unless tag.one_per_topic
|
||||
|
||||
hash[tag.tag_group_id] = (hash[tag.tag_group_id] || []) << tag
|
||||
hash
|
||||
end
|
||||
|
||||
tags_by_group_map.select { |_, group_tags| group_tags.size > 1 }
|
||||
end
|
||||
|
||||
TAG_GROUP_RESTRICTIONS_SQL ||= <<~SQL
|
||||
tag_group_restrictions AS (
|
||||
SELECT t.id as tag_id, tgm.id as tgm_id, tg.id as tag_group_id, tg.parent_tag_id as parent_tag_id,
|
||||
@@ -457,7 +574,7 @@ module DiscourseTagging
|
||||
|
||||
if !one_tag_per_group_ids.empty?
|
||||
builder.where(
|
||||
"tag_group_id IS NULL OR tag_group_id NOT IN (?) OR id IN (:selected_tag_ids)",
|
||||
"t.id NOT IN (SELECT DISTINCT tag_id FROM tag_group_restrictions WHERE tag_group_id IN (?)) OR id IN (:selected_tag_ids)",
|
||||
one_tag_per_group_ids,
|
||||
)
|
||||
end
|
||||
|
||||
@@ -90,12 +90,7 @@ class PostRevisor
|
||||
elsif new_category.nil? || tc.guardian.can_move_topic_to_category?(new_category_id)
|
||||
tags = fields[:tags] || tc.topic.tags.map(&:name)
|
||||
if new_category &&
|
||||
!DiscourseTagging.validate_min_required_tags_for_category(
|
||||
tc.guardian,
|
||||
tc.topic,
|
||||
new_category,
|
||||
tags,
|
||||
)
|
||||
!DiscourseTagging.validate_category_tags(tc.guardian, tc.topic, new_category, tags)
|
||||
tc.check_result(false)
|
||||
next
|
||||
end
|
||||
|
||||
@@ -28,18 +28,9 @@ class TopicCreator
|
||||
category = find_category
|
||||
if category.present? && guardian.can_tag?(topic)
|
||||
tags = @opts[:tags].presence || []
|
||||
existing_tags = tags.present? ? Tag.where(name: tags) : []
|
||||
valid_tags = guardian.can_create_tag? ? tags : existing_tags
|
||||
|
||||
# all add to topic.errors
|
||||
DiscourseTagging.validate_min_required_tags_for_category(
|
||||
guardian,
|
||||
topic,
|
||||
category,
|
||||
valid_tags,
|
||||
)
|
||||
DiscourseTagging.validate_required_tags_from_group(guardian, topic, category, existing_tags)
|
||||
DiscourseTagging.validate_category_restricted_tags(guardian, topic, category, valid_tags)
|
||||
# adds topic.errors
|
||||
DiscourseTagging.validate_category_tags(guardian, topic, category, tags)
|
||||
end
|
||||
|
||||
DiscourseEvent.trigger(:after_validate_topic, topic, self)
|
||||
|
||||
Reference in New Issue
Block a user