このコミットが含まれているのは:
@@ -1,11 +1,15 @@
|
||||
class PostImportRunner
|
||||
MAX_ROWS = PostImportSourceParser::MAX_ROWS
|
||||
ATTRIBUTE_KEYS = %w[title thumbnail_base original_created_from original_created_before duration tags parent_post_ids video_ms].freeze
|
||||
def initialize actor:, rows:
|
||||
@actor = actor
|
||||
@rows = rows
|
||||
end
|
||||
|
||||
def run
|
||||
results = @rows.map { |row| run_row(row.to_h.stringify_keys) }
|
||||
raise ArgumentError, "取込件数は #{ MAX_ROWS } 件までです." if @rows.length > MAX_ROWS
|
||||
|
||||
results = @rows.map { |row| run_row(row.to_h.deep_transform_keys { _1.to_s.underscore }) }
|
||||
{ created: results.count { _1[:status] == 'created' }, skipped: results.count { _1[:status] == 'skipped' },
|
||||
failed: results.count { _1[:status] == 'failed' }, rows: results }
|
||||
end
|
||||
@@ -15,10 +19,20 @@ class PostImportRunner
|
||||
def run_row row
|
||||
return row.slice('source_row').merge(status: 'skipped') if row['status'] == 'skipped' || row['existing']
|
||||
attributes = row.fetch('attributes', { }).to_h.transform_keys { _1.to_s.underscore }
|
||||
raise ArgumentError, '取込項目が不正です.' unless (attributes.keys - ATTRIBUTE_KEYS).empty?
|
||||
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
|
||||
{ source_row: row['source_row'], status: 'failed', errors: e.record.errors.to_hash }
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
{ source_row: row['source_row'], status: 'failed', errors: { tags: ['ニコニコ・タグは直接指定できません.'] } }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
{ source_row: row['source_row'], status: 'failed', errors: { tags: ['廃止済みタグは付与できません.'] } }
|
||||
rescue ArgumentError, PostCreator::VideoMsParseError
|
||||
{ source_row: row['source_row'], status: 'failed', errors: { base: ['入力値が不正です.'] } }
|
||||
rescue StandardError => e
|
||||
{ source_row: row['source_row'], status: 'failed', errors: { base: [e.message] } }
|
||||
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
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする