2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-25 15:55:15 -04:00
|
|
|
module DiscourseTagging
|
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
TAGS_FIELD_NAME ||= "tags"
|
|
|
|
TAGS_FILTER_REGEXP ||= /[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/ # /?#[]@!$&'()*+,;=.%\`^|{}"<>
|
|
|
|
TAGS_STAFF_CACHE_KEY ||= "staff_tag_names"
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
TAG_GROUP_TAG_IDS_SQL ||= <<-SQL
|
2019-04-26 14:39:39 -04:00
|
|
|
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(
|
|
|
|
nil, # guardian
|
|
|
|
for_topic: true,
|
|
|
|
category: category,
|
2019-11-12 14:28:44 -05:00
|
|
|
selected_tags: tag_names,
|
|
|
|
only_tag_names: tag_names
|
|
|
|
)
|
|
|
|
|
|
|
|
tags = Tag.where(id: tags.map(&:id)).all.to_a if tags.size > 0
|
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
|
2019-10-16 14:27:30 -04:00
|
|
|
tag_ids = tags.map(&:id)
|
|
|
|
|
|
|
|
parent_tags_map = DB.query("
|
|
|
|
SELECT tgm.tag_id, tg.parent_tag_id
|
2019-04-26 14:39:39 -04:00
|
|
|
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
|
2019-10-16 14:27:30 -04:00
|
|
|
AND tgm.tag_id IN (?)
|
|
|
|
", tag_ids).inject({}) do |h, v|
|
|
|
|
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 14:39:39 -04:00
|
|
|
|
2019-10-16 14:27:30 -04:00
|
|
|
unless missing_parent_tag_ids.empty?
|
|
|
|
tags = tags + Tag.where(id: missing_parent_tag_ids).all
|
2019-04-26 14:39:39 -04:00
|
|
|
end
|
|
|
|
|
2019-10-30 14:49:00 -04: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 13:50:43 -04:00
|
|
|
|
2019-07-25 12:46:16 -04: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 14:02:47 -04:00
|
|
|
topic.tags = tags
|
|
|
|
else
|
2019-10-30 14:49:00 -04: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 13:50:43 -04:00
|
|
|
|
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
|
|
|
|
|
2019-10-30 14:49:00 -04:00
|
|
|
def self.validate_min_required_tags_for_category(guardian, topic, category, tags = [])
|
|
|
|
if !guardian.is_staff? &&
|
|
|
|
category &&
|
|
|
|
category.minimum_required_tags > 0 &&
|
|
|
|
tags.length < category.minimum_required_tags
|
|
|
|
|
|
|
|
topic.errors.add(:base, I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.validate_required_tags_from_group(guardian, topic, category, tags = [])
|
|
|
|
if !guardian.is_staff? &&
|
|
|
|
category &&
|
|
|
|
category.required_tag_group &&
|
|
|
|
(tags.length < category.min_tags_from_required_group ||
|
|
|
|
category.required_tag_group.tags.where("tags.id in (?)", tags.map(&:id)).count < category.min_tags_from_required_group)
|
|
|
|
|
|
|
|
topic.errors.add(:base,
|
|
|
|
I18n.t(
|
|
|
|
"tags.required_tags_from_group",
|
|
|
|
count: category.min_tags_from_required_group,
|
|
|
|
tag_group_name: category.required_tag_group.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
TAG_GROUP_RESTRICTIONS_SQL ||= <<~SQL
|
|
|
|
tag_group_restrictions AS (
|
|
|
|
SELECT t.name as tag_name, t.id as tag_id, tgm.id as tgm_id, tg.id as tag_group_id, tg.parent_tag_id as parent_tag_id,
|
|
|
|
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
|
|
|
|
|
|
|
|
CATEGORY_RESTRICTIONS_SQL ||= <<~SQL
|
|
|
|
category_restrictions AS (
|
|
|
|
SELECT t.name as tag_name, t.id as tag_id, ct.id as ct_id, ct.category_id as category_id
|
|
|
|
FROM tags t
|
|
|
|
INNER JOIN category_tags ct ON t.id = ct.tag_id /*and_name_like*/
|
|
|
|
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT t.name as tag_name, t.id as tag_id, ctg.id as ctg_id, ctg.category_id as category_id
|
|
|
|
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
|
|
|
|
|
|
|
|
PERMITTED_TAGS_SQL ||= <<~SQL
|
|
|
|
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
|
|
|
|
ON tg.id = tgp.tag_group_id
|
|
|
|
AND tgp.group_id = #{Group::AUTO_GROUPS[:everyone]}
|
|
|
|
AND tgp.permission_type = #{TagGroupPermission.permission_types[:full]}
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2016-05-30 16:37:06 -04:00
|
|
|
# Options:
|
|
|
|
# term: a search term to filter tags by name
|
2019-11-12 14:28:44 -05:00
|
|
|
# order: order by for the query
|
|
|
|
# limit: max number of results
|
2016-05-30 16:37:06 -04:00
|
|
|
# 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
|
2019-11-12 14:28:44 -05:00
|
|
|
# only_tag_names: limit results to tags with these names
|
|
|
|
def self.filter_allowed_tags(guardian, opts = {})
|
2018-10-05 05:23:52 -04:00
|
|
|
selected_tag_ids = opts[:selected_tags] ? Tag.where_name(opts[:selected_tags]).pluck(:id) : []
|
2019-11-12 14:28:44 -05: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 = {}
|
|
|
|
|
|
|
|
unless selected_tag_ids.empty?
|
|
|
|
builder_params[:selected_tag_ids] = selected_tag_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
sql = +"WITH #{TAG_GROUP_RESTRICTIONS_SQL}, #{CATEGORY_RESTRICTIONS_SQL}"
|
|
|
|
if (opts[:for_input] || opts[:for_topic]) && filter_for_non_staff
|
|
|
|
sql << ", #{PERMITTED_TAGS_SQL} "
|
|
|
|
end
|
|
|
|
|
|
|
|
outer_join = category.nil? || category.allow_global_tags || !category_has_restricted_tags
|
|
|
|
|
|
|
|
sql << <<~SQL
|
|
|
|
SELECT t.id, t.name, t.topic_count, t.pm_topic_count,
|
|
|
|
tgr.tgm_id as tgm_id, tgr.tag_group_id as tag_group_id, tgr.parent_tag_id as parent_tag_id,
|
|
|
|
tgr.one_per_topic as one_per_topic
|
|
|
|
FROM tags t
|
|
|
|
INNER JOIN tag_group_restrictions tgr ON tgr.tag_id = t.id
|
|
|
|
#{outer_join ? "LEFT OUTER" : "INNER"}
|
|
|
|
JOIN category_restrictions cr ON t.id = cr.tag_id
|
|
|
|
/*where*/
|
|
|
|
/*order_by*/
|
|
|
|
/*limit*/
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder = DB.build(sql)
|
|
|
|
|
|
|
|
builder.limit(opts[:limit]) if opts[:limit]
|
|
|
|
builder.order_by(opts[:order]) if opts[:order]
|
2018-03-13 16:34:28 -04:00
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
if !opts[:for_topic] && builder_params[:selected_tag_ids]
|
|
|
|
builder.where("id NOT IN (:selected_tag_ids)")
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:only_tag_names]
|
|
|
|
builder.where("LOWER(name) IN (?)", opts[:only_tag_names].map(&:downcase))
|
|
|
|
end
|
|
|
|
|
|
|
|
# parent tag requirements
|
|
|
|
if opts[:for_input]
|
|
|
|
builder.where(
|
|
|
|
builder_params[:selected_tag_ids] ?
|
|
|
|
"tgm_id IS NULL OR parent_tag_id IS NULL OR parent_tag_id IN (:selected_tag_ids)" :
|
|
|
|
"tgm_id IS NULL OR parent_tag_id IS NULL"
|
|
|
|
)
|
|
|
|
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 16:34:28 -04:00
|
|
|
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!
|
2019-11-12 14:28:44 -05:00
|
|
|
builder.where("LOWER(name) LIKE :term")
|
|
|
|
builder_params[:term] = "%#{term}%"
|
|
|
|
sql.gsub!("/*and_name_like*/", "AND LOWER(t.name) LIKE :term")
|
|
|
|
else
|
|
|
|
sql.gsub!("/*and_name_like*/", "")
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
if opts[:for_input] && filter_for_non_staff && category&.required_tag_group
|
2019-11-04 16:51:18 -05:00
|
|
|
required_tag_ids = category.required_tag_group.tags.pluck(:id)
|
|
|
|
if (required_tag_ids & selected_tag_ids).size < category.min_tags_from_required_group
|
2019-11-12 14:28:44 -05:00
|
|
|
builder.where("id IN (?)", required_tag_ids)
|
2016-07-08 17:13:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
if filter_for_non_staff
|
|
|
|
# remove hidden tags
|
|
|
|
builder.where(<<~SQL, Group::AUTO_GROUPS[:everyone])
|
|
|
|
id NOT IN (
|
|
|
|
SELECT tag_id
|
|
|
|
FROM tag_group_memberships tgm
|
|
|
|
WHERE tag_group_id NOT IN (SELECT tag_group_id FROM tag_group_permissions WHERE group_id = ?)
|
|
|
|
)
|
|
|
|
SQL
|
2019-04-26 14:39:39 -04:00
|
|
|
end
|
2016-05-30 16:37:06 -04:00
|
|
|
|
2019-11-12 14:28:44 -05: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
|
|
|
|
|
|
|
|
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)",
|
|
|
|
one_tag_per_group_ids
|
|
|
|
)
|
2019-04-26 14:39:39 -04:00
|
|
|
end
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2019-11-12 14:28:44 -05:00
|
|
|
result = builder.query(builder_params).uniq { |t| t.id }
|
2019-04-26 14:39:39 -04:00
|
|
|
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
|