#248 ニコニコ同期でオリジナルの投稿日時連携(#246)

Merged
みてるぞ merged 1 commits from feature/246 into main 1 week ago
  1. +10
    -3
      backend/lib/tasks/sync_nico.rake
  2. +9
    -2
      backend/spec/tasks/nico_sync_spec.rb

+ 10
- 3
backend/lib/tasks/sync_nico.rake View File

@@ -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),


+ 9
- 2
backend/spec/tasks/nico_sync_spec.rb View File

@@ -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("<html></html>"))
@@ -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


Loading…
Cancel
Save