Merge branch 'main' into feature/323

This commit is contained in:
2026-04-23 12:48:55 +09:00
11 changed files with 642 additions and 49 deletions
+85 -5
View File
@@ -66,7 +66,7 @@ class TagsController < ApplicationController
.offset(offset)
.to_a
render json: { tags: TagRepr.base(tags), count: q.size }
render json: { tags: TagRepr.many(tags), count: q.size }
end
def with_depth
@@ -209,6 +209,52 @@ class TagsController < ApplicationController
render json: build_tag_children(tag)
end
def update_all
return head :unauthorized unless current_user
return head :forbidden unless current_user.gte_member?
tag = Tag.find_by(id: params[:id])
return head :not_found unless tag
name = params[:name].to_s.strip
category = params[:category].to_s.strip
return head :unprocessable_entity if name.blank? || category.blank?
if name != tag.name &&
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico])
return render json: { error: 'システム・タグの名称は変更できません.' },
status: :unprocessable_entity
end
if tag.nico? || category == 'nico'
return render json: { error: 'ニコタグは変更できません.' },
status: :unprocessable_entity
end
alias_names = params[:aliases].to_s.split.uniq
parent_names = params[:parent_tags].to_s.split.uniq
ApplicationRecord.transaction do
TagVersioning.ensure_snapshot!(tag, created_by_user: current_user)
old_name = tag.name
tag.update!(category:)
tag.tag_name.update!(name:)
alias_names << old_name if name != old_name
alias_names.delete(name)
update_aliases!(tag, alias_names)
update_parent_tags!(tag, parent_names)
tag.reload
record_tag_version!(tag, event_type: :update, created_by_user: current_user)
end
render json: TagRepr.base(tag.reload)
end
def update
return head :unauthorized unless current_user
return head :forbidden unless current_user.gte_member?
@@ -218,8 +264,8 @@ class TagsController < ApplicationController
tag = Tag.find(params[:id])
if category.present? && tag.nico? != (category == 'nico')
return render json: { error: 'ニコタグのカテゴリ変更できません.' },
if tag.nico? || (category.present? && category == 'nico')
return render json: { error: 'ニコタグ変更できません.' },
status: :unprocessable_entity
end
@@ -237,7 +283,7 @@ class TagsController < ApplicationController
private
def build_tag_children(tag)
def build_tag_children tag
material = tag.materials.first
file = nil
content_type = nil
@@ -251,11 +297,45 @@ class TagsController < ApplicationController
material: material.as_json&.merge(file:, content_type:))
end
def record_tag_version!(tag, event_type:, created_by_user:)
def record_tag_version! tag, event_type:, created_by_user:
if tag.nico?
NicoTagVersionRecorder.record!(tag:, event_type:, created_by_user:)
else
TagVersionRecorder.record!(tag:, event_type:, created_by_user:)
end
end
def update_aliases! tag, alias_names
current_aliases = tag.tag_name.aliases.to_a
current_aliases.each do |alias_tag_name|
next if alias_names.include?(alias_tag_name.name)
alias_tag_name.update!(canonical: nil)
end
alias_names.each do |alias_name|
alias_tag_name = TagName.find_undiscard_or_create_by!(name: alias_name)
alias_tag_name.update!(canonical: tag.tag_name)
end
end
def update_parent_tags! tag, parent_names
parent_tags = Tag.normalise_tags(parent_names, with_tagme: false,
with_no_deerjikist: false,
deny_nico: true)
old_parent_tags = tag.parents.to_a
TagVersioning.record_tag_snapshots!((old_parent_tags + parent_tags).uniq,
created_by_user: current_user)
tag.reversed_tag_implications.destroy_all
parent_tags.each do |parent_tag|
next if parent_tag == tag
TagImplication.create!(tag:, parent_tag:)
end
end
end
+3 -4
View File
@@ -8,10 +8,9 @@ module TagRepr
module_function
def base tag
tag.as_json(BASE)
tag.as_json(BASE).merge(aliases: tag.snapshot_aliases,
parents: tag.parents.map { _1.as_json(BASE) })
end
def many tags
tags.map { |t| base(t) }
end
def many(tags) = tags.map { |t| base(t) }
end