このコミットが含まれているのは:
2026-09-24 07:01:36 +09:00
コミット 1de00191ea
11個のファイルの変更108行の追加22行の削除
+3 -2
ファイルの表示
@@ -10,8 +10,9 @@ class TagNameSanitisationRule < ApplicationRecord
validate :source_pattern_must_be_regexp
class << self
def sanitise(name) =
rules.reduce(name.dup) { |name, (pattern, replacement)| name.gsub(pattern, replacement) }
def sanitise(name)
rules.reduce(name.dup) { |name, (pattern, replacement)| name.gsub(pattern, replacement) }
end
def apply!
TagName.find_each do |tn|
+2 -1
ファイルの表示
@@ -143,7 +143,8 @@ namespace :nico do
desired_tag_ids = kept_tag_ids.to_a
datum['tags'].each do |raw|
tag = ExternalTag.find_or_create_by!(platform: :nico, name: raw)
name = TagNameSanitisationRule.sanitise(raw)
tag = ExternalTag.find_or_create_by!(platform: :nico, name:)
unless tag.nico_tag_versions.exists?
NicoTagVersionRecorder.record!(external_tag: tag,
+62 -3
ファイルの表示
@@ -15,6 +15,11 @@ RSpec.describe 'nico:sync' do
NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
end
def create_nico_sanitisation_rules!
TagNameSanitisationRule.create!(priority: 20, source_pattern: '\\?', replacement: '_')
TagNameSanitisationRule.create!(priority: 40, source_pattren: '_$', replacement: '')
end
it '既存 post を見つけて、nico tag と linked tag を追加し、差分が出たら bot を付ける' do
# 既存 post(正規表現で拾われるURL)
post = Post.create!(
@@ -435,17 +440,24 @@ RSpec.describe 'nico:sync' do
expect(post.tags.map(&:name)).not_to include('bot操作')
end
it '外部タグが新規記載されたとき,その時点の連携タグを記載する' do
it 'サニタイズ後の既存外部タグを再利用し、その連携タグを記載する' do
post = create_nico_sync_post!
create_nico_sanitisation_rules!
external_tag = create_external_tag!('AAA')
linked_tag = create_tag!('spec_linked', category: :general)
link_nico_to_tag!(external_tag, linked_tag)
run_nico_sync_with_tags!(['AAA'])
expect {
run_nico_sync_with_tags!(['AAA?'])
}.not_to change(ExternalTag, :count)
expect(post.reload.external_tags).to include(external_tag)
post.reload
expect(post.external_tags).to contain_exactly(external_tag)
expect(post.tags).to include(linked_tag)
expect(ExternalTag.exists?(platform: :nico, name: 'AAA?')).to be(false)
end
it '外部タグに差分がない場合,内外マッピングが変はっても連携タグを再評価しない' do
@@ -520,4 +532,51 @@ RSpec.describe 'nico:sync' do
# 再記載時点の内外マッピングが新たに適用される.
expect(post.tags).to include(new_linked_tag)
end
it '外部タグだけの変更では bot を付けず、投稿履歴を記録する' do
post = create_nico_sync_post!
PostVersionRecorder.record!(post:, event_type: :create, created_by_user: nil)
create_nico_sanitisation_rules!
expect {
run_nico_sync_with_tags!(['AAA?', 'AAA?'])
}.to change(PostVersion, :count).by(1)
.and change(ExternalTag, :count).by(1)
.and change(PostExternalTag, :count).by(1)
.and change(NicoTagVersion, :count).by(1)
.and change(TagName, :count).by(0)
external = post.reload.external_tags.sole
expect(external).to have_attributes(
platform: 'nico',
name: 'AAA')
expect(ExternalTag.exists?(platform: :nico, name: 'AAA?')).to be(false)
expect(post.tags.map(&:name)).not_to include('bot操作')
expect(post.post_versions.order(:version_no).last.tags_json)
.to include('external_tag_id' => external.id)
expect {
run_nico_sync_with_tags!(['AAA?'])
}.to change(PostVersion, :count).by(0)
.and change(NicoTagVersion, :count).by(0)
expect {
run_nico_sync_with_tags!([])
}.to change(PostVersion, :count).by(1)
expect(post.reload.external_tags).to be_empty
expect(post.tags.map(&:name)).not_to include('bot操作')
end
it 'nico: prefix を含む従来の規則で外部タグ名をサニタイズする' do
post = create_nico_sync_post!
run_nico_sync_with_tags!(['foo:_bar'])
expect(post.reload.external_tags.sole)
.to have_attributes(platform: 'nico', name: 'foo:_bar')
end
end