このコミットが含まれているのは:
@@ -9,30 +9,55 @@ class PostImportPreviewer
|
||||
end
|
||||
|
||||
def preview
|
||||
preview_rows(rows: @parsed[:rows])
|
||||
end
|
||||
|
||||
def preview_rows rows:, fetch_metadata: true, metadata_cache: { }
|
||||
urls = { }
|
||||
@parsed[:rows].map do |row|
|
||||
values = row[:values]
|
||||
attributes = FIELDS.to_h { |field| [field, mapped_value(field, values)] }
|
||||
url = values[@url_column].to_s.strip
|
||||
metadata, warnings = fetch_metadata(url)
|
||||
attributes = metadata.merge(attributes) { |_key, automatic, input| input.presence || automatic }
|
||||
rows.map do |row|
|
||||
row = row.symbolize_keys
|
||||
values = row[:values] || []
|
||||
attributes = row[:attributes]&.stringify_keys || FIELDS.to_h { |field| [field, mapped_value(field, values)] }
|
||||
provenance = row[:provenance]&.stringify_keys || mapped_provenance(attributes)
|
||||
url = row[:url].presence || values[@url_column].to_s.strip
|
||||
provenance['url'] ||= row[:url].present? ? 'manual' : 'mapped'
|
||||
attributes['url'] = url
|
||||
fetch_warnings = Array(row[:fetch_warnings]).dup
|
||||
should_fetch = fetch_metadata == true || fetch_metadata.to_i == row[:source_row].to_i
|
||||
metadata, fetch_warnings = metadata_for(url, should_fetch, metadata_cache, fetch_warnings)
|
||||
metadata.each do |field, value|
|
||||
next unless provenance[field] == 'automatic' || attributes[field].blank?
|
||||
|
||||
attributes[field] = value
|
||||
provenance[field] = 'automatic'
|
||||
end
|
||||
post = Post.new(url:)
|
||||
post.valid?
|
||||
normal_url = post.url
|
||||
errors = post.errors.to_hash.transform_values(&:dup)
|
||||
validation_warnings = []
|
||||
errors[:url] = ['URL が重複しています.'] if normal_url.present? && urls.key?(normal_url)
|
||||
urls[normal_url] = true if normal_url.present?
|
||||
if normal_url.present? && Post.exists?(url: normal_url)
|
||||
@existing == 'error' ? errors[:url] = ['既存投稿です.'] : warnings << '既存投稿のためスキップします.'
|
||||
@existing == 'error' ? errors[:url] = ['既存投稿です.'] : validation_warnings << '既存投稿のためスキップします.'
|
||||
end
|
||||
validate_preview_tags(attributes['tags'], errors)
|
||||
validate_basic_data(attributes, errors)
|
||||
validate_preview_tags(attributes['tags'], errors, validation_warnings)
|
||||
validate_parents(attributes['parent_post_ids'], errors)
|
||||
{ source_row: row[:source_row], url: normal_url || url, attributes:, warnings:, errors:,
|
||||
status: errors.present? ? 'error' : (warnings.present? ? 'warning' : 'ready'),
|
||||
attributes.delete('url')
|
||||
{ source_row: row[:source_row], url: normal_url || url, attributes:, provenance:,
|
||||
fetch_warnings:, validation_warnings:, warnings: fetch_warnings + validation_warnings, errors:,
|
||||
status: errors.present? ? 'error' : ((fetch_warnings + validation_warnings).present? ? 'warning' : 'ready'),
|
||||
existing: normal_url.present? && Post.exists?(url: normal_url) }
|
||||
end
|
||||
end
|
||||
|
||||
def metadata_for url, fetch_metadata, cache, previous_warnings
|
||||
return [{ }, previous_warnings] unless fetch_metadata
|
||||
|
||||
cache[url] ||= fetch_metadata(url)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mapped_value field, values
|
||||
@@ -43,33 +68,71 @@ class PostImportPreviewer
|
||||
Array(mapping['columns']).filter_map { |index| values[index.to_i].to_s.presence }.join(field == 'tags' ? ' ' : '')
|
||||
end
|
||||
|
||||
def mapped_provenance attributes
|
||||
attributes.to_h.to_h { |field, value|
|
||||
mapping = @mappings[field.to_s]
|
||||
origin =
|
||||
if value.present? && mapping&.[]('kind') == 'fixed'
|
||||
'fixed'
|
||||
elsif value.present?
|
||||
'mapped'
|
||||
else
|
||||
'automatic'
|
||||
end
|
||||
[field.to_s, origin]
|
||||
}
|
||||
end
|
||||
|
||||
def fetch_metadata url
|
||||
return [{ }, ['URL が空です.']] if url.blank?
|
||||
|
||||
data = PostMetadataFetcher.fetch(url)
|
||||
data = PostMetadataFetcher.fetch(url).stringify_keys
|
||||
warnings = []
|
||||
warnings << 'タイトルを取得できませんでした.' if data[:title].blank?
|
||||
warnings << 'サムネールを取得できませんでした.' if data[:thumbnail_base].blank?
|
||||
warnings << 'タイトルを取得できませんでした.' if data['title'].blank?
|
||||
warnings << 'サムネールを取得できませんでした.' if data['thumbnail_base'].blank?
|
||||
[data.compact, warnings]
|
||||
rescue Preview::UrlSafety::UnsafeUrl, Preview::HttpFetcher::FetchFailed, Preview::HttpFetcher::ResponseTooLarge => e
|
||||
[{ }, ["メタデータを取得できませんでした: #{ e.message }"]]
|
||||
end
|
||||
|
||||
def validate_preview_tags raw, errors
|
||||
def validate_preview_tags raw, errors, warnings
|
||||
names = raw.to_s.split
|
||||
return if names.empty?
|
||||
if names.any? { _1.downcase.start_with?('nico:') }
|
||||
errors[:tags] = ['ニコニコ・タグは直接指定できません.']
|
||||
return
|
||||
end
|
||||
canonical = names.map { TagName.canonicalise(_1.sub(/\[.*\]\z/, '')).first }
|
||||
if Tag.joins(:tag_name).where(tag_names: { name: canonical }, deprecated_at: nil).count < canonical.uniq.length
|
||||
errors[:tags] = ['存在しないタグ又は廃止済みタグがあります.']
|
||||
end
|
||||
parsed = names.map { |name| TagName.canonicalise(name.sub(/\[.*\]\z/, '')).first }
|
||||
existing = Tag.joins(:tag_name).where(tag_names: { name: parsed }).includes(:tag_name).to_a
|
||||
deprecated = existing.select(&:deprecated?).map(&:name)
|
||||
errors[:tags] = ["廃止済みタグがあります: #{ deprecated.join(' ') }"] if deprecated.present?
|
||||
known = existing.reject(&:deprecated?).map(&:name)
|
||||
new_tags = parsed.uniq - known
|
||||
warnings << "新規タグを作成します: #{ new_tags.join(' ') }" if new_tags.present?
|
||||
rescue Tag::SectionLiteralParseError
|
||||
errors[:tags] = ['タグ区間の記法が不正です.']
|
||||
end
|
||||
|
||||
def validate_basic_data attributes, errors
|
||||
post = Post.new(title: attributes['title'].presence,
|
||||
url: attributes['url'],
|
||||
thumbnail_base: attributes['thumbnail_base'].presence,
|
||||
original_created_from: attributes['original_created_from'].presence,
|
||||
original_created_before: attributes['original_created_before'].presence,
|
||||
video_ms: parse_duration(attributes['duration'], errors))
|
||||
post.valid?
|
||||
post.errors.each { |error| (errors[error.attribute] ||= []) << error.message }
|
||||
end
|
||||
|
||||
def parse_duration value, errors
|
||||
return nil if value.blank?
|
||||
|
||||
value.is_a?(Numeric) ? value.to_i : Tag.time_to_ms!(value.to_s, tag_name: '動画時間')
|
||||
rescue Tag::SectionLiteralParseError
|
||||
errors[:video_ms] = ['動画時間の記法が不正です.']
|
||||
nil
|
||||
end
|
||||
|
||||
def validate_parents raw, errors
|
||||
ids = raw.to_s.split.map { Integer(_1, exception: false) }
|
||||
return if ids.compact.empty? && raw.to_s.blank?
|
||||
|
||||
新しい課題から参照
ユーザをブロックする