2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-25 14:55:15 -05:00
|
|
|
module DiscourseTagging
|
2024-10-15 21:09:07 -05:00
|
|
|
TAGS_FIELD_NAME = "tags"
|
|
|
|
TAGS_FILTER_REGEXP = /[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/ # /?#[]@!$&'()*+,;=.%\`^|{}"<>
|
|
|
|
TAGS_STAFF_CACHE_KEY = "staff_tag_names"
|
2016-04-25 14:55:15 -05:00
|
|
|
|
2024-10-15 21:09:07 -05:00
|
|
|
TAG_GROUP_TAG_IDS_SQL = <<-SQL
|
2019-04-26 13:39:39 -05:00
|
|
|
SELECT tag_id
|
|
|
|
FROM tag_group_memberships tgm
|
|
|
|
INNER JOIN tag_groups tg
|
|
|
|
ON tgm.tag_group_id = tg.id
|
|
|
|
SQL
|
|
|
|
|
2022-12-14 21:01:44 -06:00
|
|
|
def self.term_types
|
|
|
|
@term_types ||= Enum.new(contains: 0, starts_with: 1)
|
|
|
|
end
|
|
|
|
|
2017-02-28 11:08:06 -06:00
|
|
|
def self.tag_topic_by_names(topic, guardian, tag_names_arg, append: false)
|
2018-02-21 09:52:56 -06:00
|
|
|
if guardian.can_tag?(topic)
|
2016-05-04 13:02:47 -05:00
|
|
|
tag_names = DiscourseTagging.tags_for_saving(tag_names_arg, guardian) || []
|
|
|
|
|
2019-12-04 12:33:51 -06:00
|
|
|
if !tag_names.empty?
|
|
|
|
Tag
|
|
|
|
.where_name(tag_names)
|
|
|
|
.joins(:target_tag)
|
|
|
|
.includes(:target_tag)
|
|
|
|
.each { |tag| tag_names[tag_names.index(tag.name)] = tag.target_tag.name }
|
|
|
|
end
|
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
# tags currently on the topic
|
2018-02-13 14:46:25 -06:00
|
|
|
old_tag_names = topic.tags.pluck(:name) || []
|
2020-10-14 12:15:54 -05:00
|
|
|
# tags we're trying to add to the topic
|
2016-05-04 13:02:47 -05:00
|
|
|
new_tag_names = tag_names - old_tag_names
|
2020-10-14 12:15:54 -05:00
|
|
|
# tag names being removed from the topic
|
2016-05-04 13:02:47 -05:00
|
|
|
removed_tag_names = old_tag_names - tag_names
|
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
# tag names which are visible, but not usable, by *some users*
|
|
|
|
readonly_tags = DiscourseTagging.readonly_tag_names(guardian)
|
2021-05-20 20:43:47 -05:00
|
|
|
# tags names which are not visible or usable by this user
|
2020-10-14 12:15:54 -05:00
|
|
|
hidden_tags = DiscourseTagging.hidden_tag_names(guardian)
|
2018-04-20 14:25:28 -05:00
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
# tag names which ARE permitted by *this user*
|
|
|
|
permitted_tags = DiscourseTagging.permitted_tag_names(guardian)
|
2016-05-04 13:02:47 -05:00
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
# If this user has explicit permission to use certain tags,
|
|
|
|
# we need to ensure those tags are removed from the list of
|
|
|
|
# restricted tags
|
|
|
|
readonly_tags = readonly_tags - permitted_tags if permitted_tags.present?
|
|
|
|
|
|
|
|
# visible, but not usable, tags this user is trying to use
|
|
|
|
disallowed_tags = new_tag_names & readonly_tags
|
|
|
|
# hidden tags this user is trying to use
|
|
|
|
disallowed_tags += new_tag_names & hidden_tags
|
2018-04-18 11:51:25 -05:00
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
if disallowed_tags.present?
|
|
|
|
topic.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t("tags.restricted_tag_disallowed", tag: disallowed_tags.join(" ")),
|
|
|
|
)
|
|
|
|
return false
|
2016-05-04 13:02:47 -05:00
|
|
|
end
|
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
removed_readonly_tags = removed_tag_names & readonly_tags
|
|
|
|
if removed_readonly_tags.present?
|
|
|
|
topic.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t("tags.restricted_tag_remove_disallowed", tag: removed_readonly_tags.join(" ")),
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
tag_names += removed_tag_names & hidden_tags
|
|
|
|
|
2018-03-28 13:40:26 -05:00
|
|
|
category = topic.category
|
|
|
|
tag_names = tag_names + old_tag_names if append
|
|
|
|
|
|
|
|
if tag_names.present?
|
2016-12-09 00:24:12 -06:00
|
|
|
# guardian is explicitly nil cause we don't want to strip all
|
|
|
|
# staff tags that already passed validation
|
2018-03-13 15:34:28 -05:00
|
|
|
tags =
|
|
|
|
filter_allowed_tags(
|
|
|
|
nil, # guardian
|
|
|
|
for_topic: true,
|
|
|
|
category: category,
|
2019-11-12 13:28:44 -06:00
|
|
|
selected_tags: tag_names,
|
|
|
|
only_tag_names: tag_names,
|
|
|
|
)
|
|
|
|
|
2021-11-23 05:00:45 -06:00
|
|
|
# keep existent tags that current user cannot use
|
|
|
|
tags += Tag.where(name: old_tag_names & tag_names)
|
|
|
|
|
2019-11-12 13:28:44 -06:00
|
|
|
tags = Tag.where(id: tags.map(&:id)).all.to_a if tags.size > 0
|
2016-05-30 15:37:06 -05:00
|
|
|
|
2019-06-07 00:45:16 -05:00
|
|
|
if tags.size < tag_names.size &&
|
|
|
|
(
|
|
|
|
category.nil? || category.allow_global_tags ||
|
|
|
|
(category.tags.count == 0 && category.tag_groups.count == 0)
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
2016-05-04 13:02:47 -05:00
|
|
|
tag_names.each do |name|
|
2018-10-05 04:23:52 -05:00
|
|
|
tags << Tag.create(name: name) unless Tag.where_name(name).exists?
|
2016-05-04 13:02:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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.
2023-05-15 15:19:41 -05:00
|
|
|
# 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)
|
|
|
|
|
2019-04-26 13:39:39 -05:00
|
|
|
# add missing mandatory parent tags
|
2019-10-16 13:27:30 -05:00
|
|
|
tag_ids = tags.map(&:id)
|
|
|
|
|
2022-09-30 03:28:09 -05:00
|
|
|
parent_tags_map =
|
|
|
|
DB
|
|
|
|
.query(
|
|
|
|
"
|
|
|
|
SELECT tgm.tag_id, tg.parent_tag_id
|
|
|
|
FROM tag_groups tg
|
|
|
|
INNER JOIN tag_group_memberships tgm
|
|
|
|
ON tgm.tag_group_id = tg.id
|
|
|
|
WHERE tg.parent_tag_id IS NOT NULL
|
|
|
|
AND tgm.tag_id IN (?)
|
|
|
|
",
|
|
|
|
tag_ids,
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
2022-09-30 03:28:09 -05:00
|
|
|
.inject({}) do |h, v|
|
2019-10-16 13:27:30 -05:00
|
|
|
h[v.tag_id] ||= []
|
|
|
|
h[v.tag_id] << v.parent_tag_id
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
missing_parent_tag_ids =
|
|
|
|
parent_tags_map
|
|
|
|
.map do |_, parent_tag_ids|
|
|
|
|
(tag_ids & parent_tag_ids).size == 0 ? parent_tag_ids.first : nil
|
|
|
|
end
|
|
|
|
.compact
|
|
|
|
.uniq
|
2019-04-26 13:39:39 -05: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.
2023-05-15 15:19:41 -05:00
|
|
|
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
|
|
|
|
|
2024-05-27 05:27:13 -05:00
|
|
|
next if parent_tag.blank? || original_child_tag.blank?
|
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.
2023-05-15 15:19:41 -05:00
|
|
|
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
|
2019-04-26 13:39:39 -05:00
|
|
|
|
2019-10-30 13:49:00 -05:00
|
|
|
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)
|
2018-04-14 12:50:43 -05:00
|
|
|
|
2019-07-25 11:46:16 -05:00
|
|
|
if tags.size == 0
|
|
|
|
topic.errors.add(:base, I18n.t("tags.forbidden.invalid", count: new_tag_names.size))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-05-04 13:02:47 -05:00
|
|
|
topic.tags = tags
|
|
|
|
else
|
2019-10-30 13:49:00 -05:00
|
|
|
return false unless validate_min_required_tags_for_category(guardian, topic, category)
|
|
|
|
return false unless validate_required_tags_from_group(guardian, topic, category)
|
2018-04-14 12:50:43 -05:00
|
|
|
|
2016-05-04 13:02:47 -05:00
|
|
|
topic.tags = []
|
|
|
|
end
|
2016-07-07 21:58:18 -05:00
|
|
|
topic.tags_changed = true
|
2021-03-25 22:53:47 -05:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(
|
|
|
|
:topic_tags_changed,
|
|
|
|
topic,
|
|
|
|
old_tag_names: old_tag_names,
|
|
|
|
new_tag_names: topic.tags.map(&:name),
|
2024-09-06 09:23:30 -05:00
|
|
|
user: guardian.user,
|
2021-03-25 22:53:47 -05:00
|
|
|
)
|
|
|
|
|
2024-03-27 04:57:10 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
topic.errors.add(:base, I18n.t("tags.user_not_permitted"))
|
|
|
|
false
|
2016-05-04 13:02:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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.
2023-05-15 15:19:41 -05:00
|
|
|
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
|
|
|
|
|
2021-04-18 18:43:50 -05:00
|
|
|
def self.validate_min_required_tags_for_category(guardian, model, category, tags = [])
|
2019-10-30 13:49:00 -05:00
|
|
|
if !guardian.is_staff? && category && category.minimum_required_tags > 0 &&
|
|
|
|
tags.length < category.minimum_required_tags
|
2021-04-18 18:43:50 -05:00
|
|
|
model.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags),
|
|
|
|
)
|
2019-10-30 13:49:00 -05:00
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-18 18:43:50 -05:00
|
|
|
def self.validate_required_tags_from_group(guardian, model, category, tags = [])
|
2022-04-06 08:08:06 -05:00
|
|
|
return true if guardian.is_staff? || category.nil?
|
|
|
|
|
|
|
|
success = true
|
|
|
|
category.category_required_tag_groups.each do |crtg|
|
|
|
|
if tags.length < crtg.min_count ||
|
|
|
|
crtg.tag_group.tags.where("tags.id in (?)", tags.map(&:id)).count < crtg.min_count
|
|
|
|
success = false
|
|
|
|
|
|
|
|
model.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"tags.required_tags_from_group",
|
|
|
|
count: crtg.min_count,
|
|
|
|
tag_group_name: crtg.tag_group.name,
|
|
|
|
tags: crtg.tag_group.tags.order(:id).pluck(:name).join(", "),
|
|
|
|
),
|
2019-10-30 13:49:00 -05:00
|
|
|
)
|
2022-04-06 08:08:06 -05:00
|
|
|
end
|
2019-10-30 13:49:00 -05:00
|
|
|
end
|
2022-04-06 08:08:06 -05:00
|
|
|
|
|
|
|
success
|
2019-10-30 13:49:00 -05:00
|
|
|
end
|
|
|
|
|
2022-03-28 13:25:26 -05:00
|
|
|
def self.validate_category_restricted_tags(guardian, model, category, tags = [])
|
|
|
|
return true if tags.blank? || category.blank?
|
|
|
|
|
|
|
|
tags = tags.map(&:name) if Tag === tags[0]
|
|
|
|
tags_restricted_to_categories = Hash.new { |h, k| h[k] = Set.new }
|
|
|
|
|
|
|
|
query = Tag.where(name: tags)
|
|
|
|
query
|
|
|
|
.joins(tag_groups: :categories)
|
|
|
|
.pluck(:name, "categories.id")
|
|
|
|
.each { |(tag, cat_id)| tags_restricted_to_categories[tag] << cat_id }
|
|
|
|
query
|
|
|
|
.joins(:categories)
|
|
|
|
.pluck(:name, "categories.id")
|
|
|
|
.each { |(tag, cat_id)| tags_restricted_to_categories[tag] << cat_id }
|
2023-01-09 06:10:19 -06:00
|
|
|
|
2022-03-28 13:25:26 -05:00
|
|
|
unallowed_tags =
|
|
|
|
tags_restricted_to_categories.keys.select do |tag|
|
|
|
|
!tags_restricted_to_categories[tag].include?(category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if unallowed_tags.present?
|
|
|
|
msg =
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
|
|
|
|
count: unallowed_tags.size,
|
|
|
|
tags: unallowed_tags.sort.join(", "),
|
|
|
|
category: category.name,
|
|
|
|
)
|
|
|
|
model.errors.add(:base, msg)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if !category.allow_global_tags && category.has_restricted_tags?
|
|
|
|
unrestricted_tags = tags - tags_restricted_to_categories.keys
|
|
|
|
if unrestricted_tags.present?
|
|
|
|
msg =
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.category_does_not_allow_tags",
|
|
|
|
count: unrestricted_tags.size,
|
|
|
|
tags: unrestricted_tags.sort.join(", "),
|
|
|
|
category: category.name,
|
|
|
|
)
|
|
|
|
model.errors.add(:base, msg)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
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.
2023-05-15 15:19:41 -05:00
|
|
|
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
|
|
|
|
|
2024-10-15 21:09:07 -05:00
|
|
|
TAG_GROUP_RESTRICTIONS_SQL = <<~SQL
|
2019-11-12 13:28:44 -06:00
|
|
|
tag_group_restrictions AS (
|
2019-12-10 09:19:03 -06:00
|
|
|
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,
|
2019-11-12 13:28:44 -06:00
|
|
|
tg.one_per_topic as one_per_topic
|
|
|
|
FROM tags t
|
|
|
|
LEFT OUTER JOIN tag_group_memberships tgm ON tgm.tag_id = t.id /*and_name_like*/
|
|
|
|
LEFT OUTER JOIN tag_groups tg ON tg.id = tgm.tag_group_id
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2024-10-15 21:09:07 -05:00
|
|
|
CATEGORY_RESTRICTIONS_SQL = <<~SQL
|
2019-11-12 13:28:44 -06:00
|
|
|
category_restrictions AS (
|
2023-10-10 12:30:24 -05:00
|
|
|
SELECT t.id as tag_id, ct.id as ct_id, ct.category_id as category_id, NULL AS category_tag_group_id
|
2019-11-12 13:28:44 -06:00
|
|
|
FROM tags t
|
|
|
|
INNER JOIN category_tags ct ON t.id = ct.tag_id /*and_name_like*/
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
2023-10-10 12:30:24 -05:00
|
|
|
SELECT t.id as tag_id, ctg.id as ctg_id, ctg.category_id as category_id, ctg.tag_group_id AS category_tag_group_id
|
2019-11-12 13:28:44 -06:00
|
|
|
FROM tags t
|
|
|
|
INNER JOIN tag_group_memberships tgm ON tgm.tag_id = t.id /*and_name_like*/
|
|
|
|
INNER JOIN category_tag_groups ctg ON tgm.tag_group_id = ctg.tag_group_id
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2024-10-15 21:09:07 -05:00
|
|
|
PERMITTED_TAGS_SQL = <<~SQL
|
2019-11-12 13:28:44 -06:00
|
|
|
permitted_tag_groups AS (
|
|
|
|
SELECT tg.id as tag_group_id, tgp.group_id as group_id, tgp.permission_type as permission_type
|
|
|
|
FROM tags t
|
|
|
|
INNER JOIN tag_group_memberships tgm ON tgm.tag_id = t.id /*and_name_like*/
|
|
|
|
INNER JOIN tag_groups tg ON tg.id = tgm.tag_group_id
|
|
|
|
INNER JOIN tag_group_permissions tgp
|
2020-10-14 12:15:54 -05:00
|
|
|
ON tg.id = tgp.tag_group_id /*and_group_ids*/
|
2019-11-12 13:28:44 -06:00
|
|
|
AND tgp.permission_type = #{TagGroupPermission.permission_types[:full]}
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2016-05-30 15:37:06 -05:00
|
|
|
# Options:
|
|
|
|
# term: a search term to filter tags by name
|
2022-12-14 21:01:44 -06:00
|
|
|
# term_type: whether to search by "starts_with" or "contains" with the term
|
2019-11-12 13:28:44 -06:00
|
|
|
# limit: max number of results
|
2016-05-30 15:37:06 -05:00
|
|
|
# category: a Category to which the object being tagged belongs
|
2016-06-09 15:00:19 -05:00
|
|
|
# for_input: result is for an input field, so only show permitted tags
|
2018-03-13 15:34:28 -05:00
|
|
|
# for_topic: results are for tagging a topic
|
2016-06-09 15:00:19 -05:00
|
|
|
# selected_tags: an array of tag names that are in the current selection
|
2019-11-12 13:28:44 -06:00
|
|
|
# only_tag_names: limit results to tags with these names
|
2019-12-04 12:33:51 -06:00
|
|
|
# exclude_synonyms: exclude synonyms from results
|
2019-12-10 09:19:03 -06:00
|
|
|
# order_search_results: result should be ordered for name search results
|
|
|
|
# order_popularity: order result by topic_count
|
2022-12-07 21:47:59 -06:00
|
|
|
# excluded_tag_names: an array of tag names not to include in the results
|
2019-11-12 13:28:44 -06:00
|
|
|
def self.filter_allowed_tags(guardian, opts = {})
|
2018-10-05 04:23:52 -05:00
|
|
|
selected_tag_ids = opts[:selected_tags] ? Tag.where_name(opts[:selected_tags]).pluck(:id) : []
|
2019-11-12 13:28:44 -06:00
|
|
|
category = opts[:category]
|
|
|
|
category_has_restricted_tags =
|
|
|
|
category ? (category.tags.count > 0 || category.tag_groups.count > 0) : false
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
filter_for_non_staff = !guardian.nil? && !guardian.is_staff?
|
|
|
|
|
|
|
|
builder_params = {}
|
|
|
|
|
|
|
|
builder_params[:selected_tag_ids] = selected_tag_ids unless selected_tag_ids.empty?
|
|
|
|
|
|
|
|
sql = +"WITH #{TAG_GROUP_RESTRICTIONS_SQL}, #{CATEGORY_RESTRICTIONS_SQL}"
|
|
|
|
if (opts[:for_input] || opts[:for_topic]) && filter_for_non_staff
|
|
|
|
sql << ", #{PERMITTED_TAGS_SQL} "
|
2020-10-14 12:15:54 -05:00
|
|
|
builder_params[:group_ids] = permitted_group_ids(guardian)
|
|
|
|
sql.gsub!("/*and_group_ids*/", "AND group_id IN (:group_ids)")
|
2019-11-12 13:28:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
outer_join = category.nil? || category.allow_global_tags || !category_has_restricted_tags
|
|
|
|
|
2023-01-19 19:50:24 -06:00
|
|
|
topic_count_column = Tag.topic_count_column(guardian)
|
|
|
|
|
2019-12-10 09:19:03 -06:00
|
|
|
distinct_clause =
|
|
|
|
if opts[:order_popularity]
|
2023-01-19 19:50:24 -06:00
|
|
|
"DISTINCT ON (#{topic_count_column}, name)"
|
2019-12-17 03:55:06 -06:00
|
|
|
elsif opts[:order_search_results] && opts[:term].present?
|
2023-01-19 19:50:24 -06:00
|
|
|
"DISTINCT ON (lower(name) = lower(:cleaned_term), #{topic_count_column}, name)"
|
2019-12-10 09:19:03 -06:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2019-11-12 13:28:44 -06:00
|
|
|
sql << <<~SQL
|
2023-01-19 19:50:24 -06:00
|
|
|
SELECT #{distinct_clause} t.id, t.name, t.#{topic_count_column}, t.pm_topic_count, t.description,
|
2019-11-12 13:28:44 -06:00
|
|
|
tgr.tgm_id as tgm_id, tgr.tag_group_id as tag_group_id, tgr.parent_tag_id as parent_tag_id,
|
2019-12-04 12:33:51 -06:00
|
|
|
tgr.one_per_topic as one_per_topic, t.target_tag_id
|
2019-11-12 13:28:44 -06:00
|
|
|
FROM tags t
|
|
|
|
INNER JOIN tag_group_restrictions tgr ON tgr.tag_id = t.id
|
|
|
|
#{outer_join ? "LEFT OUTER" : "INNER"}
|
2023-10-10 12:30:24 -05:00
|
|
|
JOIN category_restrictions cr ON t.id = cr.tag_id AND (tgr.tag_group_id = cr.category_tag_group_id OR cr.category_tag_group_id IS NULL)
|
2019-11-12 13:28:44 -06:00
|
|
|
/*where*/
|
|
|
|
/*order_by*/
|
|
|
|
/*limit*/
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder = DB.build(sql)
|
|
|
|
|
|
|
|
if !opts[:for_topic] && builder_params[:selected_tag_ids]
|
|
|
|
builder.where("id NOT IN (:selected_tag_ids)")
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:only_tag_names]
|
2019-11-18 12:20:37 -06:00
|
|
|
builder.where("LOWER(name) IN (:only_tag_names)")
|
|
|
|
builder_params[:only_tag_names] = opts[:only_tag_names].map(&:downcase)
|
2019-11-12 13:28:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# parent tag requirements
|
|
|
|
if opts[:for_input]
|
|
|
|
builder.where(
|
2023-01-09 06:10:19 -06:00
|
|
|
(
|
2019-11-12 13:28:44 -06:00
|
|
|
if builder_params[:selected_tag_ids]
|
|
|
|
"tgm_id IS NULL OR parent_tag_id IS NULL OR parent_tag_id IN (:selected_tag_ids)"
|
2023-01-09 06:10:19 -06:00
|
|
|
else
|
2019-11-12 13:28:44 -06:00
|
|
|
"tgm_id IS NULL OR parent_tag_id IS NULL"
|
2023-01-09 06:10:19 -06:00
|
|
|
end
|
|
|
|
),
|
2019-11-12 13:28:44 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if category && category_has_restricted_tags
|
|
|
|
builder.where(
|
|
|
|
category.allow_global_tags ? "category_id = ? OR category_id IS NULL" : "category_id = ?",
|
|
|
|
category.id,
|
|
|
|
)
|
|
|
|
elsif category || opts[:for_input] || opts[:for_topic]
|
|
|
|
# tags not restricted to any categories
|
|
|
|
builder.where("category_id IS NULL")
|
|
|
|
end
|
|
|
|
|
|
|
|
if filter_for_non_staff && (opts[:for_input] || opts[:for_topic])
|
|
|
|
# exclude staff-only tag groups
|
|
|
|
builder.where(
|
|
|
|
"tag_group_id IS NULL OR tag_group_id IN (SELECT tag_group_id FROM permitted_tag_groups)",
|
|
|
|
)
|
2018-03-13 15:34:28 -05:00
|
|
|
end
|
|
|
|
|
2016-05-30 15:37:06 -05:00
|
|
|
term = opts[:term]
|
|
|
|
if term.present?
|
2019-11-18 12:20:37 -06:00
|
|
|
builder_params[:cleaned_term] = term
|
2022-12-14 21:01:44 -06:00
|
|
|
|
|
|
|
if opts[:term_type] == DiscourseTagging.term_types[:starts_with]
|
2024-05-02 10:13:45 -05:00
|
|
|
builder.where("starts_with(LOWER(name), LOWER(:cleaned_term))")
|
|
|
|
sql.gsub!("/*and_name_like*/", "AND starts_with(LOWER(t.name), LOWER(:cleaned_term))")
|
2022-12-14 21:01:44 -06:00
|
|
|
else
|
2024-05-02 10:13:45 -05:00
|
|
|
builder.where("position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")
|
|
|
|
sql.gsub!("/*and_name_like*/", "AND position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")
|
2022-12-14 21:01:44 -06:00
|
|
|
end
|
2019-11-12 13:28:44 -06:00
|
|
|
else
|
|
|
|
sql.gsub!("/*and_name_like*/", "")
|
2016-05-30 15:37:06 -05:00
|
|
|
end
|
|
|
|
|
2021-06-02 11:43:34 -05:00
|
|
|
# show required tags for non-staff
|
|
|
|
# or for staff when
|
|
|
|
# - there are more available tags than the query limit
|
|
|
|
# - and no search term has been included
|
2022-04-06 08:08:06 -05:00
|
|
|
required_tag_ids = nil
|
2022-04-21 07:13:52 -05:00
|
|
|
required_category_tag_group = nil
|
2022-04-06 08:08:06 -05:00
|
|
|
if opts[:for_input] && category&.category_required_tag_groups.present? &&
|
|
|
|
(filter_for_non_staff || term.blank?)
|
|
|
|
category.category_required_tag_groups.each do |crtg|
|
|
|
|
group_tags = crtg.tag_group.tags.pluck(:id)
|
|
|
|
next if (group_tags & selected_tag_ids).size >= crtg.min_count
|
|
|
|
if filter_for_non_staff || group_tags.size >= opts[:limit].to_i
|
2022-04-21 07:13:52 -05:00
|
|
|
required_category_tag_group = crtg
|
2022-04-06 08:08:06 -05:00
|
|
|
required_tag_ids = group_tags
|
|
|
|
builder.where("id IN (?)", required_tag_ids)
|
|
|
|
end
|
|
|
|
break
|
2016-07-08 16:13:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-12 13:28:44 -06:00
|
|
|
if filter_for_non_staff
|
2020-10-14 12:15:54 -05:00
|
|
|
group_ids = permitted_group_ids(guardian)
|
|
|
|
|
|
|
|
builder.where(<<~SQL, group_ids, group_ids)
|
2019-11-12 13:28:44 -06:00
|
|
|
id NOT IN (
|
2020-10-14 12:15:54 -05:00
|
|
|
(SELECT tgm.tag_id
|
|
|
|
FROM tag_group_permissions tgp
|
|
|
|
INNER JOIN tag_groups tg ON tgp.tag_group_id = tg.id
|
|
|
|
INNER JOIN tag_group_memberships tgm ON tg.id = tgm.tag_group_id
|
|
|
|
WHERE tgp.group_id NOT IN (?))
|
|
|
|
|
|
|
|
EXCEPT
|
|
|
|
|
|
|
|
(SELECT tgm.tag_id
|
|
|
|
FROM tag_group_permissions tgp
|
|
|
|
INNER JOIN tag_groups tg ON tgp.tag_group_id = tg.id
|
|
|
|
INNER JOIN tag_group_memberships tgm ON tg.id = tgm.tag_group_id
|
|
|
|
WHERE tgp.group_id IN (?))
|
2019-11-12 13:28:44 -06:00
|
|
|
)
|
|
|
|
SQL
|
2019-04-26 13:39:39 -05:00
|
|
|
end
|
2016-05-30 15:37:06 -05:00
|
|
|
|
2019-11-12 13:28:44 -06:00
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
2023-10-10 12:30:24 -05:00
|
|
|
if one_tag_per_group_ids.present?
|
2019-11-12 13:28:44 -06:00
|
|
|
builder.where(
|
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.
2023-05-15 15:19:41 -05:00
|
|
|
"t.id NOT IN (SELECT DISTINCT tag_id FROM tag_group_restrictions WHERE tag_group_id IN (?)) OR id IN (:selected_tag_ids)",
|
2019-11-12 13:28:44 -06:00
|
|
|
one_tag_per_group_ids,
|
|
|
|
)
|
2019-04-26 13:39:39 -05:00
|
|
|
end
|
2016-05-30 15:37:06 -05:00
|
|
|
end
|
|
|
|
|
2019-12-04 12:33:51 -06:00
|
|
|
builder.where("target_tag_id IS NULL") if opts[:exclude_synonyms]
|
|
|
|
|
|
|
|
if opts[:exclude_has_synonyms]
|
|
|
|
builder.where("id NOT IN (SELECT target_tag_id FROM tags WHERE target_tag_id IS NOT NULL)")
|
|
|
|
end
|
|
|
|
|
2022-12-07 21:47:59 -06:00
|
|
|
builder.where("name NOT IN (?)", opts[:excluded_tag_names]) if opts[:excluded_tag_names]&.any?
|
|
|
|
|
2021-06-02 11:43:34 -05:00
|
|
|
if opts[:limit]
|
|
|
|
if required_tag_ids && term.blank?
|
|
|
|
# override limit so all required tags are shown by default
|
|
|
|
builder.limit(required_tag_ids.size)
|
|
|
|
else
|
|
|
|
builder.limit(opts[:limit])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 09:19:03 -06:00
|
|
|
if opts[:order_popularity]
|
2023-01-19 19:50:24 -06:00
|
|
|
builder.order_by("#{topic_count_column} DESC, name")
|
2019-11-18 12:20:37 -06:00
|
|
|
elsif opts[:order_search_results] && !term.blank?
|
2023-01-19 19:50:24 -06:00
|
|
|
builder.order_by("lower(name) = lower(:cleaned_term) DESC, #{topic_count_column} DESC, name")
|
2019-11-18 12:20:37 -06:00
|
|
|
end
|
|
|
|
|
2019-11-12 13:28:44 -06:00
|
|
|
result = builder.query(builder_params).uniq { |t| t.id }
|
2022-04-21 07:13:52 -05:00
|
|
|
|
|
|
|
if opts[:with_context]
|
|
|
|
context = {}
|
|
|
|
if required_category_tag_group
|
|
|
|
context[:required_tag_group] = {
|
|
|
|
name: required_category_tag_group.tag_group.name,
|
|
|
|
min_count: required_category_tag_group.min_count,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
[result, context]
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
2019-04-26 13:39:39 -05:00
|
|
|
end
|
|
|
|
|
2022-11-22 12:55:57 -06:00
|
|
|
def self.visible_tags(guardian)
|
|
|
|
if guardian&.is_staff?
|
|
|
|
Tag.all
|
|
|
|
else
|
|
|
|
# Visible tags either have no permissions or have allowable permissions
|
|
|
|
Tag
|
|
|
|
.where.not(id: TagGroupMembership.joins(tag_group: :tag_group_permissions).select(:tag_id))
|
|
|
|
.or(
|
|
|
|
Tag.where(
|
|
|
|
id:
|
|
|
|
TagGroupPermission
|
|
|
|
.joins(tag_group: :tag_group_memberships)
|
|
|
|
.where(group_id: permitted_group_ids_query(guardian))
|
|
|
|
.select("tag_group_memberships.tag_id"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
2018-03-26 16:04:55 -05:00
|
|
|
end
|
|
|
|
|
2022-11-22 12:55:57 -06:00
|
|
|
def self.filter_visible(query, guardian = nil)
|
|
|
|
guardian&.is_staff? ? query : query.where(id: visible_tags(guardian).select(:id))
|
2018-03-26 16:04:55 -05:00
|
|
|
end
|
|
|
|
|
2022-11-22 12:55:57 -06:00
|
|
|
def self.hidden_tag_names(guardian = nil)
|
|
|
|
guardian&.is_staff? ? [] : Tag.where.not(id: visible_tags(guardian).select(:id)).pluck(:name)
|
2020-10-14 12:15:54 -05:00
|
|
|
end
|
|
|
|
|
2022-11-22 12:55:57 -06:00
|
|
|
def self.permitted_group_ids_query(guardian = nil)
|
2020-10-27 13:17:13 -05:00
|
|
|
if guardian&.authenticated?
|
2022-11-22 12:55:57 -06:00
|
|
|
Group.from(
|
|
|
|
Group.sanitize_sql(
|
|
|
|
[
|
|
|
|
"(SELECT ? AS id UNION #{guardian.user.groups.select(:id).to_sql}) as groups",
|
|
|
|
Group::AUTO_GROUPS[:everyone],
|
2023-01-09 06:10:19 -06:00
|
|
|
],
|
2022-11-22 12:55:57 -06:00
|
|
|
),
|
|
|
|
).select(:id)
|
|
|
|
else
|
|
|
|
Group.from(
|
|
|
|
Group.sanitize_sql(["(SELECT ? AS id) AS groups", Group::AUTO_GROUPS[:everyone]]),
|
|
|
|
).select(:id)
|
2020-10-14 12:15:54 -05:00
|
|
|
end
|
2022-11-22 12:55:57 -06:00
|
|
|
end
|
2020-10-14 12:15:54 -05:00
|
|
|
|
2022-11-22 12:55:57 -06:00
|
|
|
def self.permitted_group_ids(guardian = nil)
|
|
|
|
permitted_group_ids_query(guardian).pluck(:id)
|
2020-10-14 12:15:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# read-only tags for this user
|
|
|
|
def self.readonly_tag_names(guardian = nil)
|
|
|
|
return [] if guardian&.is_staff?
|
|
|
|
|
|
|
|
query =
|
|
|
|
Tag.joins(tag_groups: :tag_group_permissions).where(
|
|
|
|
"tag_group_permissions.permission_type = ?",
|
|
|
|
TagGroupPermission.permission_types[:readonly],
|
|
|
|
)
|
|
|
|
|
|
|
|
query.pluck(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# explicit permissions to use these tags
|
|
|
|
def self.permitted_tag_names(guardian = nil)
|
2022-11-22 12:55:57 -06:00
|
|
|
query =
|
|
|
|
Tag.joins(tag_groups: :tag_group_permissions).where(
|
|
|
|
tag_group_permissions: {
|
|
|
|
group_id: permitted_group_ids(guardian),
|
|
|
|
permission_type: TagGroupPermission.permission_types[:full],
|
|
|
|
},
|
|
|
|
)
|
2020-10-14 12:15:54 -05:00
|
|
|
|
|
|
|
query.pluck(:name).uniq
|
2018-04-20 14:25:28 -05:00
|
|
|
end
|
|
|
|
|
2020-10-14 12:15:54 -05:00
|
|
|
# middle level of tag group restrictions
|
2018-04-20 14:25:28 -05:00
|
|
|
def self.staff_tag_names
|
2019-11-26 23:11:49 -06:00
|
|
|
tag_names = Discourse.cache.read(TAGS_STAFF_CACHE_KEY)
|
2019-03-12 03:23:36 -05:00
|
|
|
|
|
|
|
if !tag_names
|
2022-11-22 12:55:57 -06:00
|
|
|
tag_names =
|
|
|
|
Tag
|
|
|
|
.joins(tag_groups: :tag_group_permissions)
|
|
|
|
.where(
|
|
|
|
tag_group_permissions: {
|
|
|
|
group_id: Group::AUTO_GROUPS[:everyone],
|
|
|
|
permission_type: TagGroupPermission.permission_types[:readonly],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.pluck(:name)
|
2019-03-12 03:23:36 -05:00
|
|
|
Discourse.cache.write(TAGS_STAFF_CACHE_KEY, tag_names, expires_in: 1.hour)
|
|
|
|
end
|
|
|
|
|
|
|
|
tag_names
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!
|
|
|
|
Discourse.cache.delete(TAGS_STAFF_CACHE_KEY)
|
2016-05-30 15:37:06 -05:00
|
|
|
end
|
|
|
|
|
2016-04-25 14:55:15 -05:00
|
|
|
def self.clean_tag(tag)
|
2018-10-15 01:45:28 -05:00
|
|
|
tag = tag.dup
|
2018-10-05 04:23:52 -05:00
|
|
|
tag.downcase! if SiteSetting.force_lowercase_tags
|
2018-10-15 01:45:28 -05:00
|
|
|
tag.strip!
|
2020-12-22 09:27:37 -06:00
|
|
|
tag.gsub!(/[[:space:]]+/, "-")
|
|
|
|
tag.gsub!(/[^[:word:][:punct:]]+/, "")
|
2018-10-15 01:45:28 -05:00
|
|
|
tag.gsub!(TAGS_FILTER_REGEXP, "")
|
2023-11-22 17:57:12 -06:00
|
|
|
tag.squeeze!("-")
|
2018-10-15 01:45:28 -05:00
|
|
|
tag[0...SiteSetting.max_tag_length]
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
|
|
|
|
2016-10-12 14:44:26 -05:00
|
|
|
def self.tags_for_saving(tags_arg, guardian, opts = {})
|
|
|
|
return [] unless guardian.can_tag_topics? && tags_arg.present?
|
2016-04-25 14:55:15 -05:00
|
|
|
|
2018-10-05 04:23:52 -05:00
|
|
|
tag_names = Tag.where_name(tags_arg).pluck(:name)
|
2016-04-25 14:55:15 -05:00
|
|
|
|
2016-10-12 14:44:26 -05:00
|
|
|
if guardian.can_create_tag?
|
|
|
|
tag_names += (tags_arg - tag_names).map { |t| clean_tag(t) }
|
|
|
|
tag_names.delete_if { |t| t.blank? }
|
|
|
|
tag_names.uniq!
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
|
|
|
|
2019-11-14 14:10:51 -06:00
|
|
|
opts[:unlimited] ? tag_names : tag_names[0...SiteSetting.max_tags_per_topic]
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 15:32:19 -05:00
|
|
|
def self.add_or_create_tags_by_name(taggable, tag_names_arg, opts = {})
|
|
|
|
tag_names =
|
|
|
|
DiscourseTagging.tags_for_saving(tag_names_arg, Guardian.new(Discourse.system_user), opts) ||
|
|
|
|
[]
|
2016-06-06 13:18:15 -05:00
|
|
|
if taggable.tags.pluck(:name).sort != tag_names.sort
|
2018-10-05 04:23:52 -05:00
|
|
|
taggable.tags = Tag.where_name(tag_names).all
|
2019-12-04 12:33:51 -06:00
|
|
|
new_tag_names =
|
|
|
|
taggable.tags.size < tag_names.size ? tag_names - taggable.tags.map(&:name) : []
|
2023-04-18 12:01:11 -05:00
|
|
|
taggable.tags << Tag
|
|
|
|
.where(target_tag_id: taggable.tags.map(&:id))
|
|
|
|
.where.not(id: taggable.tags.map(&:id))
|
|
|
|
.all
|
2019-12-04 12:33:51 -06:00
|
|
|
new_tag_names.each { |name| taggable.tags << Tag.create(name: name) }
|
2016-06-06 13:18:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 12:33:51 -06:00
|
|
|
# Returns true if all were added successfully, or an Array of the
|
|
|
|
# tags that failed to be added, with errors on each Tag.
|
|
|
|
def self.add_or_create_synonyms_by_name(target_tag, synonym_names)
|
|
|
|
tag_names =
|
|
|
|
DiscourseTagging.tags_for_saving(synonym_names, Guardian.new(Discourse.system_user)) || []
|
2020-11-16 19:22:31 -06:00
|
|
|
tag_names -= [target_tag.name]
|
2019-12-04 12:33:51 -06:00
|
|
|
existing = Tag.where_name(tag_names).all
|
|
|
|
target_tag.synonyms << existing
|
|
|
|
(tag_names - target_tag.synonyms.map(&:name)).each do |name|
|
|
|
|
target_tag.synonyms << Tag.create(name: name)
|
|
|
|
end
|
|
|
|
successful = existing.select { |t| !t.errors.present? }
|
2020-07-21 10:32:01 -05:00
|
|
|
synonyms_ids = successful.map(&:id)
|
|
|
|
TopicTag.where(topic_id: target_tag.topics.with_deleted, tag_id: synonyms_ids).delete_all
|
2023-06-02 09:17:29 -05:00
|
|
|
TopicTag.joins(DB.sql_fragment(<<~SQL, synonyms_ids: synonyms_ids)).delete_all
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT MIN(id) AS id, topic_id
|
|
|
|
FROM topic_tags
|
|
|
|
WHERE tag_id IN (:synonyms_ids)
|
|
|
|
GROUP BY topic_id
|
|
|
|
) AS tt ON tt.id < topic_tags.id
|
|
|
|
AND tt.topic_id = topic_tags.topic_id
|
|
|
|
AND topic_tags.tag_id IN (:synonyms_ids)
|
|
|
|
SQL
|
2020-07-21 10:32:01 -05:00
|
|
|
TopicTag.where(tag_id: synonyms_ids).update_all(tag_id: target_tag.id)
|
2020-02-14 11:13:17 -06:00
|
|
|
Scheduler::Defer.later "Update tag topic counts" do
|
|
|
|
Tag.ensure_consistency!
|
|
|
|
end
|
2019-12-04 12:33:51 -06:00
|
|
|
(existing - successful).presence || true
|
|
|
|
end
|
|
|
|
|
2016-04-25 14:55:15 -05:00
|
|
|
def self.muted_tags(user)
|
|
|
|
return [] unless user
|
2016-08-04 10:54:39 -05:00
|
|
|
TagUser.lookup(user, :muted).joins(:tag).pluck("tags.name")
|
2016-04-25 14:55:15 -05:00
|
|
|
end
|
|
|
|
end
|