2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-25 15:55:15 -04:00
|
|
|
module DiscourseTagging
|
|
|
|
|
|
|
|
TAGS_FIELD_NAME = "tags"
|
2016-10-14 15:29:47 -04:00
|
|
|
TAGS_FILTER_REGEXP = /[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/ # /?#[]@!$&'()*+,;=.%\`^|{}"<>
|
2019-03-12 04:23:36 -04:00
|
|
|
TAGS_STAFF_CACHE_KEY = "staff_tag_names"
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2019-04-26 14:39:39 -04:00
|
|
|
TAG_GROUP_TAG_IDS_SQL = <<-SQL
|
|
|
|
SELECT tag_id
|
|
|
|
FROM tag_group_memberships tgm
|
|
|
|
INNER JOIN tag_groups tg
|
|
|
|
ON tgm.tag_group_id = tg.id
|
|
|
|
SQL
|
|
|
|
|
2017-02-28 12:08:06 -05:00
|
|
|
def self.tag_topic_by_names(topic, guardian, tag_names_arg, append: false)
|
2018-02-21 10:52:56 -05:00
|
|
|
if guardian.can_tag?(topic)
|
2016-05-04 14:02:47 -04:00
|
|
|
tag_names = DiscourseTagging.tags_for_saving(tag_names_arg, guardian) || []
|
|
|
|
|
2018-02-13 15:46:25 -05:00
|
|
|
old_tag_names = topic.tags.pluck(:name) || []
|
2016-05-04 14:02:47 -04:00
|
|
|
new_tag_names = tag_names - old_tag_names
|
|
|
|
removed_tag_names = old_tag_names - tag_names
|
|
|
|
|
|
|
|
# Protect staff-only tags
|
|
|
|
unless guardian.is_staff?
|
2018-04-20 15:25:28 -04:00
|
|
|
all_staff_tags = DiscourseTagging.staff_tag_names
|
|
|
|
hidden_tags = DiscourseTagging.hidden_tag_names
|
|
|
|
|
|
|
|
staff_tags = new_tag_names & all_staff_tags
|
|
|
|
staff_tags += new_tag_names & hidden_tags
|
2016-05-04 14:02:47 -04:00
|
|
|
if staff_tags.present?
|
2019-04-30 02:58:18 -04:00
|
|
|
topic.errors.add(:base, I18n.t("tags.staff_tag_disallowed", tag: staff_tags.join(" ")))
|
2016-05-04 14:02:47 -04:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-04-20 15:25:28 -04:00
|
|
|
staff_tags = removed_tag_names & all_staff_tags
|
2016-05-04 14:02:47 -04:00
|
|
|
if staff_tags.present?
|
2019-04-30 02:58:18 -04:00
|
|
|
topic.errors.add(:base, I18n.t("tags.staff_tag_remove_disallowed", tag: staff_tags.join(" ")))
|
2016-05-04 14:02:47 -04:00
|
|
|
return false
|
|
|
|
end
|
2018-04-18 12:51:25 -04:00
|
|
|
|
|
|
|
tag_names += removed_tag_names & hidden_tags
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
2018-03-28 14:40:26 -04:00
|
|
|
category = topic.category
|
|
|
|
tag_names = tag_names + old_tag_names if append
|
|
|
|
|
|
|
|
if tag_names.present?
|
2016-12-09 01:24:12 -05:00
|
|
|
# guardian is explicitly nil cause we don't want to strip all
|
|
|
|
# staff tags that already passed validation
|
2018-03-13 16:34:28 -04:00
|
|
|
tags = filter_allowed_tags(
|
2018-10-05 05:23:52 -04:00
|
|
|
Tag.where_name(tag_names),
|
2018-03-13 16:34:28 -04:00
|
|
|
nil, # guardian
|
|
|
|
for_topic: true,
|
|
|
|
category: category,
|
|
|
|
selected_tags: tag_names
|
|
|
|
).to_a
|
2016-05-30 16:37:06 -04:00
|
|
|
|
2019-06-07 01:45:16 -04:00
|
|
|
if tags.size < tag_names.size && (category.nil? || category.allow_global_tags || (category.tags.count == 0 && category.tag_groups.count == 0))
|
2016-05-04 14:02:47 -04:00
|
|
|
tag_names.each do |name|
|
2018-10-05 05:23:52 -04:00
|
|
|
unless Tag.where_name(name).exists?
|
2016-05-30 16:37:06 -04:00
|
|
|
tags << Tag.create(name: name)
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-26 14:39:39 -04:00
|
|
|
# add missing mandatory parent tags
|
|
|
|
parent_tags = TagGroup.includes(:parent_tag).where("tag_groups.id IN (
|
|
|
|
SELECT tg.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 (?))", tags.map(&:id)).map(&:parent_tag)
|
|
|
|
|
|
|
|
parent_tags.reject { |t| tag_names.include?(t.name) }.each do |tag|
|
|
|
|
tags << tag
|
|
|
|
end
|
|
|
|
|
2018-04-14 13:50:43 -04:00
|
|
|
# validate minimum required tags for a category
|
|
|
|
if !guardian.is_staff? && category && category.minimum_required_tags > 0 && tags.length < category.minimum_required_tags
|
2019-04-30 02:58:18 -04:00
|
|
|
topic.errors.add(:base, I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
|
2018-04-14 13:50:43 -04:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
topic.tags = tags
|
|
|
|
else
|
2018-04-14 13:50:43 -04:00
|
|
|
# validate minimum required tags for a category
|
|
|
|
if !guardian.is_staff? && category && category.minimum_required_tags > 0
|
2019-04-30 02:58:18 -04:00
|
|
|
topic.errors.add(:base, I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
|
2018-04-14 13:50:43 -04:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
topic.tags = []
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
topic.tags_changed = true
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-05-30 16:37:06 -04:00
|
|
|
# Options:
|
|
|
|
# term: a search term to filter tags by name
|
|
|
|
# category: a Category to which the object being tagged belongs
|
2016-06-09 16:00:19 -04:00
|
|
|
# for_input: result is for an input field, so only show permitted tags
|
2018-03-13 16:34:28 -04:00
|
|
|
# for_topic: results are for tagging a topic
|
2016-06-09 16:00:19 -04:00
|
|
|
# selected_tags: an array of tag names that are in the current selection
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.filter_allowed_tags(query, guardian, opts = {})
|
2018-10-05 05:23:52 -04:00
|
|
|
selected_tag_ids = opts[:selected_tags] ? Tag.where_name(opts[:selected_tags]).pluck(:id) : []
|
2018-03-13 16:34:28 -04:00
|
|
|
|
2018-03-13 17:17:08 -04:00
|
|
|
if !opts[:for_topic] && !selected_tag_ids.empty?
|
2018-03-13 16:34:28 -04:00
|
|
|
query = query.where('tags.id NOT IN (?)', selected_tag_ids)
|
|
|
|
end
|
|
|
|
|
2016-05-30 16:37:06 -04:00
|
|
|
term = opts[:term]
|
|
|
|
if term.present?
|
2019-04-29 20:25:53 -04:00
|
|
|
term = term.gsub("_", "\\_")
|
|
|
|
clean_tag(term)
|
|
|
|
term.downcase!
|
2018-10-05 05:23:52 -04:00
|
|
|
query = query.where('lower(tags.name) like ?', "%#{term}%")
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2016-07-08 17:13:32 -04:00
|
|
|
# Filters for category-specific tags:
|
|
|
|
category = opts[:category]
|
|
|
|
|
|
|
|
if category && (category.tags.count > 0 || category.tag_groups.count > 0)
|
2019-04-04 16:35:31 -04:00
|
|
|
if category.allow_global_tags
|
|
|
|
# Select tags that:
|
|
|
|
# * are restricted to the given category
|
|
|
|
# * belong to no tag groups and aren't restricted to other categories
|
|
|
|
# * belong to tag groups that are not restricted to any categories
|
|
|
|
query = query.where(<<~SQL, category.tag_groups.pluck(:id), category.id)
|
|
|
|
tags.id IN (
|
|
|
|
SELECT t.id FROM tags t
|
|
|
|
LEFT JOIN category_tags ct ON t.id = ct.tag_id
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT xtgm.tag_id, xtgm.tag_group_id
|
|
|
|
FROM tag_group_memberships xtgm
|
|
|
|
INNER JOIN category_tag_groups ctg
|
|
|
|
ON xtgm.tag_group_id = ctg.tag_group_id
|
|
|
|
) AS tgm ON t.id = tgm.tag_id
|
|
|
|
WHERE (tgm.tag_group_id IS NULL AND ct.category_id IS NULL)
|
|
|
|
OR tgm.tag_group_id IN (?)
|
|
|
|
OR ct.category_id = ?
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
else
|
|
|
|
# Select only tags that are restricted to the given category
|
|
|
|
query = query.where(<<~SQL, category.id, category.tag_groups.pluck(:id))
|
|
|
|
tags.id IN (
|
|
|
|
SELECT tag_id FROM category_tags WHERE category_id = ?
|
2016-07-08 17:13:32 -04:00
|
|
|
UNION
|
2019-04-04 16:35:31 -04:00
|
|
|
SELECT tag_id FROM tag_group_memberships WHERE tag_group_id IN (?)
|
|
|
|
)
|
|
|
|
SQL
|
2016-07-08 17:13:32 -04:00
|
|
|
end
|
2018-03-13 16:34:28 -04:00
|
|
|
elsif opts[:for_input] || opts[:for_topic] || category
|
2016-07-08 17:13:32 -04:00
|
|
|
# exclude tags that are restricted to other categories
|
|
|
|
if CategoryTag.exists?
|
|
|
|
query = query.where("tags.id NOT IN (SELECT tag_id FROM category_tags)")
|
|
|
|
end
|
|
|
|
|
|
|
|
if CategoryTagGroup.exists?
|
|
|
|
tag_group_ids = CategoryTagGroup.pluck(:tag_group_id).uniq
|
|
|
|
query = query.where("tags.id NOT IN (SELECT tag_id FROM tag_group_memberships WHERE tag_group_id IN (?))", tag_group_ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-13 16:34:28 -04:00
|
|
|
if opts[:for_input] || opts[:for_topic]
|
2016-07-08 17:13:32 -04:00
|
|
|
unless guardian.nil? || guardian.is_staff?
|
2018-04-20 15:25:28 -04:00
|
|
|
all_staff_tags = DiscourseTagging.staff_tag_names
|
|
|
|
query = query.where('tags.name NOT IN (?)', all_staff_tags) if all_staff_tags.present?
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
2019-04-26 14:39:39 -04:00
|
|
|
end
|
2016-05-30 16:37:06 -04:00
|
|
|
|
2019-04-26 14:39:39 -04:00
|
|
|
if opts[:for_input]
|
2016-06-09 16:00:19 -04:00
|
|
|
# exclude tag groups that have a parent tag which is missing from selected_tags
|
|
|
|
|
|
|
|
if selected_tag_ids.empty?
|
2019-04-26 14:39:39 -04:00
|
|
|
sql = "tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE tg.parent_tag_id IS NOT NULL)"
|
2016-06-09 16:00:19 -04:00
|
|
|
query = query.where(sql)
|
|
|
|
else
|
2019-04-26 14:39:39 -04:00
|
|
|
exclude_group_ids = one_per_topic_group_ids(selected_tag_ids)
|
2016-06-09 16:00:19 -04:00
|
|
|
|
|
|
|
if exclude_group_ids.empty?
|
2019-04-26 14:39:39 -04:00
|
|
|
sql = "tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE tg.parent_tag_id NOT IN (?))"
|
2016-06-09 16:00:19 -04:00
|
|
|
query = query.where(sql, selected_tag_ids)
|
|
|
|
else
|
|
|
|
# It's possible that the selected tags violate some one-tag-per-group restrictions,
|
|
|
|
# so filter them out by picking one from each group.
|
|
|
|
limit_tag_ids = TagGroupMembership.select('distinct on (tag_group_id) tag_id')
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(tag_id: selected_tag_ids)
|
|
|
|
.where(tag_group_id: exclude_group_ids)
|
|
|
|
.map(&:tag_id)
|
2019-04-26 14:39:39 -04:00
|
|
|
sql = "(tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE (tg.parent_tag_id NOT IN (?) OR tg.id in (?))) OR tags.id IN (?))"
|
2016-06-09 16:00:19 -04:00
|
|
|
query = query.where(sql, selected_tag_ids, exclude_group_ids, limit_tag_ids)
|
2016-06-07 13:08:59 -04:00
|
|
|
end
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
2019-04-26 14:39:39 -04:00
|
|
|
elsif opts[:for_topic] && !selected_tag_ids.empty?
|
|
|
|
# One tag per group restriction
|
|
|
|
exclude_group_ids = one_per_topic_group_ids(selected_tag_ids)
|
|
|
|
|
|
|
|
unless exclude_group_ids.empty?
|
|
|
|
limit_tag_ids = TagGroupMembership.select('distinct on (tag_group_id) tag_id')
|
|
|
|
.where(tag_id: selected_tag_ids)
|
|
|
|
.where(tag_group_id: exclude_group_ids)
|
|
|
|
.map(&:tag_id)
|
|
|
|
sql = "(tags.id NOT IN (#{TAG_GROUP_TAG_IDS_SQL} WHERE (tg.id in (?))) OR tags.id IN (?))"
|
|
|
|
query = query.where(sql, exclude_group_ids, limit_tag_ids)
|
|
|
|
end
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2018-03-26 17:04:55 -04:00
|
|
|
if guardian.nil? || guardian.is_staff?
|
|
|
|
query
|
|
|
|
else
|
|
|
|
filter_visible(query, guardian)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-26 14:39:39 -04:00
|
|
|
def self.one_per_topic_group_ids(selected_tag_ids)
|
|
|
|
TagGroup.where(one_per_topic: true)
|
|
|
|
.joins(:tag_group_memberships)
|
|
|
|
.where('tag_group_memberships.tag_id in (?)', selected_tag_ids)
|
|
|
|
.pluck(:id)
|
|
|
|
end
|
|
|
|
|
2018-03-26 17:04:55 -04:00
|
|
|
def self.filter_visible(query, guardian = nil)
|
2018-04-20 15:25:28 -04:00
|
|
|
guardian&.is_staff? ? query : query.where("tags.id NOT IN (#{hidden_tags_query.select(:id).to_sql})")
|
2018-03-26 17:04:55 -04:00
|
|
|
end
|
|
|
|
|
2018-04-20 15:25:28 -04:00
|
|
|
def self.hidden_tag_names(guardian = nil)
|
2018-05-15 10:05:48 -04:00
|
|
|
guardian&.is_staff? ? [] : hidden_tags_query.pluck(:name)
|
2018-03-26 17:04:55 -04:00
|
|
|
end
|
|
|
|
|
2018-04-20 15:25:28 -04:00
|
|
|
def self.hidden_tags_query
|
|
|
|
Tag.joins(:tag_groups)
|
|
|
|
.where('tag_groups.id NOT IN (
|
|
|
|
SELECT tag_group_id
|
|
|
|
FROM tag_group_permissions
|
|
|
|
WHERE group_id = ?)',
|
|
|
|
Group::AUTO_GROUPS[:everyone]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.staff_tag_names
|
2019-03-12 04:23:36 -04:00
|
|
|
tag_names = Discourse.cache.read(TAGS_STAFF_CACHE_KEY, tag_names)
|
|
|
|
|
|
|
|
if !tag_names
|
|
|
|
tag_names = Tag.joins(tag_groups: :tag_group_permissions)
|
|
|
|
.where('tag_group_permissions.group_id = ? AND tag_group_permissions.permission_type = ?',
|
|
|
|
Group::AUTO_GROUPS[:everyone],
|
|
|
|
TagGroupPermission.permission_types[:readonly]
|
|
|
|
).pluck(:name)
|
|
|
|
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 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2016-04-25 15:55:15 -04:00
|
|
|
def self.clean_tag(tag)
|
2018-10-15 02:45:28 -04:00
|
|
|
tag = tag.dup
|
2018-10-05 05:23:52 -04:00
|
|
|
tag.downcase! if SiteSetting.force_lowercase_tags
|
2018-10-15 02:45:28 -04:00
|
|
|
tag.strip!
|
|
|
|
tag.gsub!(/\s+/, '-')
|
|
|
|
tag.squeeze!('-')
|
|
|
|
tag.gsub!(TAGS_FILTER_REGEXP, '')
|
|
|
|
tag[0...SiteSetting.max_tag_length]
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.tags_for_saving(tags_arg, guardian, opts = {})
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2016-10-12 15:44:26 -04:00
|
|
|
return [] unless guardian.can_tag_topics? && tags_arg.present?
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2018-10-05 05:23:52 -04:00
|
|
|
tag_names = Tag.where_name(tags_arg).pluck(:name)
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2016-10-12 15:44:26 -04: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 15:55:15 -04:00
|
|
|
end
|
|
|
|
|
2016-06-09 16:32:19 -04:00
|
|
|
return opts[:unlimited] ? tag_names : tag_names[0...SiteSetting.max_tags_per_topic]
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.add_or_create_tags_by_name(taggable, tag_names_arg, opts = {})
|
2016-06-09 16:32:19 -04:00
|
|
|
tag_names = DiscourseTagging.tags_for_saving(tag_names_arg, Guardian.new(Discourse.system_user), opts) || []
|
2016-06-06 14:18:15 -04:00
|
|
|
if taggable.tags.pluck(:name).sort != tag_names.sort
|
2018-10-05 05:23:52 -04:00
|
|
|
taggable.tags = Tag.where_name(tag_names).all
|
2016-06-06 14:18:15 -04:00
|
|
|
if taggable.tags.size < tag_names.size
|
|
|
|
new_tag_names = tag_names - taggable.tags.map(&:name)
|
|
|
|
new_tag_names.each do |name|
|
|
|
|
taggable.tags << Tag.create(name: name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-25 15:55:15 -04:00
|
|
|
def self.muted_tags(user)
|
|
|
|
return [] unless user
|
2016-08-04 11:54:39 -04:00
|
|
|
TagUser.lookup(user, :muted).joins(:tag).pluck('tags.name')
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
end
|