Browse Source

#125

pull/236/head
みてるぞ 2 weeks ago
parent
commit
6a02aa1d7b
1 changed files with 10 additions and 6 deletions
  1. +10
    -6
      backend/app/models/tag.rb

+ 10
- 6
backend/app/models/tag.rb View File

@@ -1,5 +1,5 @@
class Tag < ApplicationRecord class Tag < ApplicationRecord
class NicoTagNormalisationError < StandardError
class NicoTagNormalisationError < ArgumentError
; ;
end end


@@ -67,12 +67,14 @@ class Tag < ApplicationRecord
@bot ||= find_or_create_by_tag_name!('bot操作', category: 'meta') @bot ||= find_or_create_by_tag_name!('bot操作', category: 'meta')
end 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] 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| find_or_create_by_tag_name!(name, category: (cat || 'general')).tap do |tag|
if cat && tag.category != cat if cat && tag.category != cat
tag.update!(category: cat) tag.update!(category: cat)
@@ -107,11 +109,13 @@ class Tag < ApplicationRecord
(result + tags).uniq { |t| t.id } (result + tags).uniq { |t| t.id }
end 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) tn = TagName.find_or_create_by!(name: name.to_s.strip)
Tag.find_or_create_by!(tag_name_id: tn.id) do |t| Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
t.category = category t.category = category
end end
rescue ActiveRecord::RecordNotUnique
retry
end end


private private


Loading…
Cancel
Save