From 46fd487fc3cd6a13455cb7da4adc170b849f2b26 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 31 Jan 2026 03:06:15 +0900 Subject: [PATCH] #246 --- backend/lib/tasks/sync_nico.rake | 13 ++++++++++--- backend/spec/tasks/nico_sync_spec.rb | 11 +++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 86bd941..591307a 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -47,11 +47,18 @@ namespace :nico do code = datum['code'] post = Post.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)") .first - unless post - title = datum['title'] + title = datum['title'] + original_created_at = datum['uploaded_at'] && Time.iso8601(datum['uploaded_at']) + original_created_from = original_created_at&.change(sec: 0) + original_created_before = original_created_from&.+(1.minute) + if post + attrs = { title:, original_created_from:, original_created_before: } + post.update!(attrs) if attrs.any? { |k, v| post.public_send(k) != v } + else url = "https://www.nicovideo.jp/watch/#{ code }" thumbnail_base = fetch_thumbnail.(url) rescue nil - post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil) + post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil, + original_created_from:, original_created_before:) if thumbnail_base.present? post.thumbnail.attach( io: URI.open(thumbnail_base), diff --git a/backend/spec/tasks/nico_sync_spec.rb b/backend/spec/tasks/nico_sync_spec.rb index e4cef17..3f773f4 100644 --- a/backend/spec/tasks/nico_sync_spec.rb +++ b/backend/spec/tasks/nico_sync_spec.rb @@ -13,7 +13,6 @@ RSpec.describe "nico:sync" do end def link_nico_to_tag!(nico_tag, tag) - # NicoTagRelation がある前提(君の model からそう見える) NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id) end @@ -35,7 +34,12 @@ RSpec.describe "nico:sync" do Tag.tagme # pythonの出力(AAA が付く) - stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["AAA"] }]) + stub_python([{ + 'code' => 'sm9', + 'title' => 't', + 'tags' => ['AAA'], + 'uploaded_at' => '2026-01-01T12:34:56+09:00', + 'deleted_at' => '2026-01-31T00:00:00+09:00' }]) # 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ allow(URI).to receive(:open).and_return(StringIO.new("")) @@ -49,6 +53,9 @@ RSpec.describe "nico:sync" do expect(active_tag_names).to include("nico:AAA") expect(active_tag_names).to include("spec_linked") + expect(post.original_created_from).to eq(Time.iso8601('2026-01-01T03:34:00Z')) + expect(post.original_created_before).to eq(Time.iso8601('2026-01-01T03:35:00Z')) + # 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids) expect(active_tag_names).to include("bot操作") end