105 行
3.5 KiB
Ruby
105 行
3.5 KiB
Ruby
class PostImportRunner
|
|
def initialize actor:, rows:
|
|
@actor = actor
|
|
@rows = rows
|
|
end
|
|
|
|
def run
|
|
normalised_rows = PostImportRowNormaliser.normalise!(@rows)
|
|
previews = PostImportPreviewer.new.preview_rows(rows: normalised_rows,
|
|
fetch_metadata: false)
|
|
preview_map = previews.index_by { _1[:source_row] }
|
|
|
|
results = normalised_rows.map do |row|
|
|
run_row(row, preview_map.fetch(row['source_row']))
|
|
end
|
|
|
|
{ created: results.count { _1[:status] == 'created' },
|
|
skipped: results.count { _1[:status] == 'skipped' },
|
|
failed: results.count { _1[:status] == 'failed' },
|
|
rows: results }
|
|
end
|
|
|
|
private
|
|
|
|
def run_row row, preview
|
|
attributes = row.fetch('attributes', { }).transform_keys { _1.to_s.underscore }
|
|
return { source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: preview[:validation_errors],
|
|
recoverable: true } if preview[:validation_errors].present?
|
|
if preview[:skip_reason] == 'existing'
|
|
return { source_row: row['source_row'],
|
|
status: 'skipped',
|
|
existing_post_id: preview[:existing_post_id] }
|
|
end
|
|
|
|
attributes['tags'] = preview[:attributes]['tags']
|
|
attributes['url'] = row['url']
|
|
post = PostCreator.new(actor: @actor, attributes:).create!
|
|
{ source_row: row['source_row'], status: 'created', post: PostRepr.base(post) }
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
existing_post = existing_post_for_race(row, e.record)
|
|
if existing_post
|
|
return { source_row: row['source_row'],
|
|
status: 'skipped',
|
|
existing_post_id: existing_post.id }
|
|
end
|
|
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: e.record.errors.to_hash,
|
|
recoverable: true }
|
|
rescue ActiveRecord::RecordNotUnique => e
|
|
raise unless url_record_not_unique?(e)
|
|
|
|
existing_post = existing_post_for_race(row)
|
|
raise unless existing_post
|
|
|
|
{ source_row: row['source_row'],
|
|
status: 'skipped',
|
|
existing_post_id: existing_post.id }
|
|
rescue Tag::NicoTagNormalisationError
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: { tags: ['ニコニコ・タグは直接指定できません.'] },
|
|
recoverable: true }
|
|
rescue Tag::DeprecatedTagNormalisationError
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: { tags: ['廃止済みタグは付与できません.'] },
|
|
recoverable: true }
|
|
rescue PostCreator::VideoMsParseError
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: { duration: ['動画時間の記法が不正です.'] },
|
|
recoverable: true }
|
|
rescue ArgumentError
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: { base: ['入力値が不正です.'] },
|
|
recoverable: true }
|
|
rescue StandardError => e
|
|
Rails.logger.error("post_import_runner_failure #{ { error: e.class.name,
|
|
message: e.message }.to_json }")
|
|
{ source_row: row['source_row'],
|
|
status: 'failed',
|
|
errors: { base: ['登録中にエラーが発生しました.'] } }
|
|
end
|
|
|
|
def existing_post_for_race row, record = nil
|
|
if record && !(record.errors.of_kind?(:url, :taken))
|
|
return nil
|
|
end
|
|
|
|
normal_url = PostUrlNormaliser.normalise(row['url'])
|
|
return nil if normal_url.blank?
|
|
|
|
Post.find_by(url: normal_url)
|
|
end
|
|
|
|
def url_record_not_unique? error
|
|
error.message.include?('index_posts_on_url')
|
|
end
|
|
end
|