From 2a40dd999b575815b868eb0c4e05f7fc05ed87b4 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Mon, 22 Jun 2026 06:29:17 +0900 Subject: [PATCH] #378 --- backend/app/controllers/posts_controller.rb | 23 ++++++++++++++------- backend/app/models/tag.rb | 16 +++++++++++++- backend/app/representations/post_repr.rb | 2 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index db39065..48cf816 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -148,10 +148,10 @@ class PostsController < ApplicationController ApplicationRecord.transaction do post.save! - tags = Tag.normalise_tags!(tag_names) + tags = Tag.normalise_tags!(tag_names, deny_deprecated: true) TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user) - tags = Tag.expand_parent_tags(tags) + tags = Tag.expand_parent_tags(tags).reject(&:deprecated?) sync_post_tags!(post, tags) sync_parent_posts!(post, parent_post_ids) @@ -165,6 +165,8 @@ class PostsController < ApplicationController render json: PostRepr.base(post), status: :created rescue Tag::NicoTagNormalisationError render_validation_error fields: { tags: 'ニコニコ・タグは直接指定できません.' } + rescue Tag::DeprecatedTagNormalisationError + render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags rescue ArgumentError => e render_validation_error fields: { parent_post_ids: [e.message] } rescue ActiveRecord::RecordInvalid => e @@ -255,6 +257,8 @@ class PostsController < ApplicationController render json:, status: :ok rescue Tag::NicoTagNormalisationError render_validation_error fields: { tags: ['ニコニコ・タグは直接指定できません.'] } + rescue Tag::DeprecatedTagNormalisationError + render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags rescue ArgumentError => e render_validation_error fields: { parent_post_ids: [e.message] } rescue ActiveRecord::RecordInvalid => e @@ -378,7 +382,7 @@ class PostsController < ApplicationController end def build_tag_tree_for tags - tags = tags.to_a + tags = tags.reject(&:deprecated?).to_a tag_ids = tags.map(&:id) implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids) @@ -501,7 +505,8 @@ class PostsController < ApplicationController end def editable_tag_names_from_post post - post.tags.not_nico.joins(:tag_name).order('tag_names.name').pluck('tag_names.name') + post.tags.not_nico.where(deprecated_at: nil) + .joins(:tag_name).order('tag_names.name').pluck('tag_names.name') end def post_incoming_snapshot title:, original_created_from:, original_created_before:, @@ -533,9 +538,10 @@ class PostsController < ApplicationController end def incoming_tag_names_for_snapshot raw_tag_names - tags = Tag.normalise_tags!(raw_tag_names, with_tagme: false) + tags = Tag.normalise_tags!(raw_tag_names, with_tagme: false, + deny_deprecated: true) - Tag.expand_parent_tags(tags).map(&:name).uniq.sort + Tag.expand_parent_tags(tags).reject(&:deprecated?).map(&:name).uniq.sort end def post_conflict_json post:, base_version_no:, base_snapshot:, @@ -622,13 +628,14 @@ class PostsController < ApplicationController original_created_from: snapshot[:original_created_from], original_created_before: snapshot[:original_created_before]) - editable_tags = Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false) + editable_tags = Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false, + deny_deprecated: true) TagVersioning.record_tag_snapshots!(editable_tags, created_by_user: current_user) readonly_tags = post.tags.nico.to_a tags = readonly_tags + editable_tags - tags = Tag.expand_parent_tags(tags) + tags = Tag.expand_parent_tags(tags).reject(&:deprecated?) sync_post_tags!(post, tags) sync_parent_posts!(post, snapshot[:parent_post_ids]) diff --git a/backend/app/models/tag.rb b/backend/app/models/tag.rb index 5d78c75..8e2002e 100644 --- a/backend/app/models/tag.rb +++ b/backend/app/models/tag.rb @@ -8,6 +8,15 @@ class Tag < ApplicationRecord ; end + class DeprecatedTagNormalisationError < ArgumentError + attr_reader :tag_names + + def initialize tag_names + @tag_names = Array(tag_names) + super('deprecated tags are not allowed') + end + end + has_many :post_tags, inverse_of: :tag has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag' @@ -95,7 +104,8 @@ class Tag < ApplicationRecord def self.normalise_tags! tag_names, with_tagme: true, with_no_deerjikist: true, - deny_nico: true + deny_nico: true, + deny_deprecated: false if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') } raise NicoTagNormalisationError end @@ -104,6 +114,10 @@ class Tag < ApplicationRecord pf, cat = CATEGORY_PREFIXES.find { |p, _| name.downcase.start_with?(p) } || ['', nil] name = TagName.canonicalise(name.sub(/\A#{ pf }/i, '')).first find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag| + if deny_deprecated && tag.deprecated? + raise DeprecatedTagNormalisationError, [tag.name] + end + tag.update!(category: cat) if cat && tag.category != cat end end diff --git a/backend/app/representations/post_repr.rb b/backend/app/representations/post_repr.rb index e4f8198..1c62f1e 100644 --- a/backend/app/representations/post_repr.rb +++ b/backend/app/representations/post_repr.rb @@ -53,7 +53,7 @@ module PostRepr end def tag_json tags - tags.map { |tag| TagRepr.inline(tag) } + tags.reject(&:deprecated?).map { |tag| TagRepr.inline(tag) } end def thumbnail_url post