This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
class Tag < ApplicationRecord
|
||||
class NicoTagNormalisationError < StandardError
|
||||
class NicoTagNormalisationError < ArgumentError
|
||||
;
|
||||
end
|
||||
|
||||
@@ -67,12 +67,14 @@ class Tag < ApplicationRecord
|
||||
@bot ||= find_or_create_by_tag_name!('bot操作', category: 'meta')
|
||||
end
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true
|
||||
tags = tag_names.map do |name|
|
||||
raise NicoTagNormalisationError if name.start_with?('nico:')
|
||||
def self.normalise_tags tag_names, with_tagme: true, deny_nico: true
|
||||
if deny_nico && tag_names.any? { |n| n.start_with?('nico:') }
|
||||
raise NicoTagNormalisationError
|
||||
end
|
||||
|
||||
tags = tag_names.map do |name|
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name.delete_prefix!(pf)
|
||||
name = name.delete_prefix(pf)
|
||||
find_or_create_by_tag_name!(name, category: (cat || 'general')).tap do |tag|
|
||||
if cat && tag.category != cat
|
||||
tag.update!(category: cat)
|
||||
@@ -107,11 +109,13 @@ class Tag < ApplicationRecord
|
||||
(result + tags).uniq { |t| t.id }
|
||||
end
|
||||
|
||||
def self.find_or_create_by_tag_name!(name, category:)
|
||||
def self.find_or_create_by_tag_name! name, category:
|
||||
tn = TagName.find_or_create_by!(name: name.to_s.strip)
|
||||
Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
|
||||
t.category = category
|
||||
end
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
retry
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user