このコミットが含まれているのは:
2026-07-11 23:21:59 +09:00
コミット 440d3d38be
5個のファイルの変更63行の追加27行の削除
+11 -2
ファイルの表示
@@ -41,7 +41,8 @@ class PostImportsController < ApplicationController
end end
def create def create
result = PostImportRunner.new(actor: current_user, rows: params[:rows].to_a).run result = PostImportRunner.new(actor: current_user, rows: params[:rows].to_a,
existing: params[:existing]).run
render json: result, status: result[:created].positive? ? :created : :ok render json: result, status: result[:created].positive? ? :created : :ok
rescue ArgumentError => e rescue ArgumentError => e
render_bad_request(e.message) render_bad_request(e.message)
@@ -79,7 +80,7 @@ class PostImportsController < ApplicationController
rows.map do |row| rows.map do |row|
value = ActionController::Parameters.new(row).permit(:source_row, :sourceRow, :url, value = ActionController::Parameters.new(row).permit(:source_row, :sourceRow, :url,
attributes: {}, provenance: {}, attributes: {}, provenance: {}, tag_sources: {},
fetch_warnings: [], validation_warnings: []) fetch_warnings: [], validation_warnings: [])
normalised = value.to_h.deep_transform_keys { _1.to_s.underscore } normalised = value.to_h.deep_transform_keys { _1.to_s.underscore }
attributes = normalised.fetch('attributes', { }) attributes = normalised.fetch('attributes', { })
@@ -90,7 +91,15 @@ class PostImportsController < ApplicationController
(provenance.keys - (allowed + ['url'])).empty? (provenance.keys - (allowed + ['url'])).empty?
raise ArgumentError, '行の形式が不正です.' raise ArgumentError, '行の形式が不正です.'
end end
unless provenance.values.all? { _1.in?(['automatic', 'mapped', 'fixed', 'manual']) }
raise ArgumentError, '値の由来が不正です.'
end
raise ArgumentError, 'セルが大きすぎます.' if [normalised['url'], *attributes.values].any? { _1.to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES } raise ArgumentError, 'セルが大きすぎます.' if [normalised['url'], *attributes.values].any? { _1.to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES }
if normalised['tag_sources'].present? &&
(!normalised['tag_sources'].is_a?(Hash) ||
(normalised['tag_sources'].keys - ['automatic', 'mapped', 'fixed', 'manual']).present?)
raise ArgumentError, 'タグ由来の形式が不正です.'
end
normalised.symbolize_keys normalised.symbolize_keys
end end
+17 -5
ファイルの表示
@@ -19,6 +19,8 @@ class PostImportPreviewer
values = row[:values] || [] values = row[:values] || []
attributes = row[:attributes]&.stringify_keys || FIELDS.to_h { |field| [field, mapped_value(field, values)] } attributes = row[:attributes]&.stringify_keys || FIELDS.to_h { |field| [field, mapped_value(field, values)] }
provenance = row[:provenance]&.stringify_keys || mapped_provenance(attributes) provenance = row[:provenance]&.stringify_keys || mapped_provenance(attributes)
tag_sources = row[:tag_sources]&.stringify_keys || initial_tag_sources(attributes, provenance)
tag_sources['manual'] = attributes['tags'].to_s if provenance['tags'] == 'manual'
url = row[:url].presence || values[@url_column].to_s.strip url = row[:url].presence || values[@url_column].to_s.strip
provenance['url'] ||= row[:url].present? ? 'manual' : 'mapped' provenance['url'] ||= row[:url].present? ? 'manual' : 'mapped'
attributes['url'] = url attributes['url'] = url
@@ -27,9 +29,8 @@ class PostImportPreviewer
metadata, fetch_warnings = metadata_for(url, should_fetch, metadata_cache, fetch_warnings) metadata, fetch_warnings = metadata_for(url, should_fetch, metadata_cache, fetch_warnings)
metadata.each do |field, value| metadata.each do |field, value|
if field == 'tags' && provenance[field] != 'manual' if field == 'tags' && provenance[field] != 'manual'
previous_origin = provenance[field] tag_sources['automatic'] = value.to_s
attributes[field] = [attributes[field], value].compact.join(' ').split.uniq.join(' ') attributes[field] = merged_tags(tag_sources)
provenance[field] = previous_origin == 'automatic' ? 'automatic' : previous_origin
next next
end end
next unless provenance[field] == 'automatic' || attributes[field].blank? next unless provenance[field] == 'automatic' || attributes[field].blank?
@@ -48,10 +49,11 @@ class PostImportPreviewer
@existing == 'error' ? errors[:url] = ['既存投稿です.'] : validation_warnings << '既存投稿のためスキップします.' @existing == 'error' ? errors[:url] = ['既存投稿です.'] : validation_warnings << '既存投稿のためスキップします.'
end end
validate_basic_data(attributes, errors) validate_basic_data(attributes, errors)
validate_preview_tags(attributes['tags'], errors, validation_warnings) validate_preview_tags(merged_tags(tag_sources), errors, validation_warnings)
validate_parents(attributes['parent_post_ids'], errors) validate_parents(attributes['parent_post_ids'], errors)
attributes.delete('url') attributes.delete('url')
{ source_row: row[:source_row], url: normal_url || url, attributes:, provenance:, attributes['tags'] = merged_tags(tag_sources)
{ source_row: row[:source_row], url: normal_url || url, attributes:, provenance:, tag_sources:,
fetch_warnings:, validation_warnings:, warnings: fetch_warnings + validation_warnings, errors:, fetch_warnings:, validation_warnings:, warnings: fetch_warnings + validation_warnings, errors:,
status: errors.present? ? 'error' : ((fetch_warnings + validation_warnings).present? ? 'warning' : 'ready'), status: errors.present? ? 'error' : ((fetch_warnings + validation_warnings).present? ? 'warning' : 'ready'),
existing: normal_url.present? && Post.exists?(url: normal_url) } existing: normal_url.present? && Post.exists?(url: normal_url) }
@@ -89,6 +91,16 @@ class PostImportPreviewer
} }
end end
def initial_tag_sources attributes, provenance
origin = provenance['tags'] == 'fixed' ? 'fixed' : 'mapped'
{ 'automatic' => '', 'mapped' => origin == 'mapped' ? attributes['tags'].to_s : '',
'fixed' => origin == 'fixed' ? attributes['tags'].to_s : '', 'manual' => '' }
end
def merged_tags sources
%w[automatic mapped fixed manual].flat_map { sources[_1].to_s.split }.uniq.join(' ')
end
def fetch_metadata url def fetch_metadata url
return [{ }, ['URL が空です.']] if url.blank? return [{ }, ['URL が空です.']] if url.blank?
+7 -4
ファイルの表示
@@ -1,9 +1,10 @@
class PostImportRunner class PostImportRunner
MAX_ROWS = PostImportSourceParser::MAX_ROWS 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 ATTRIBUTE_KEYS = %w[title thumbnail_base original_created_from original_created_before duration tags parent_post_ids video_ms].freeze
def initialize actor:, rows: def initialize actor:, rows:, existing: 'skip'
@actor = actor @actor = actor
@rows = rows @rows = rows
@existing = existing == 'error' ? 'error' : 'skip'
end end
def run def run
@@ -22,12 +23,14 @@ class PostImportRunner
raise ArgumentError, '取込項目が大きすぎます.' if attributes.values.any? { _1.to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES } raise ArgumentError, '取込項目が大きすぎます.' if attributes.values.any? { _1.to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES }
raise ArgumentError, 'URL が長すぎます.' if row['url'].to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES raise ArgumentError, 'URL が長すぎます.' if row['url'].to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES
preview = PostImportPreviewer.new(parsed: { columns: [], rows: [] }, url_column: 0, preview = PostImportPreviewer.new(parsed: { columns: [], rows: [] }, url_column: 0,
mappings: {}, existing: 'error').preview_rows( mappings: {}, existing: @existing).preview_rows(
rows: [{ source_row: row['source_row'], url: row['url'], rows: [{ source_row: row['source_row'], url: row['url'],
attributes:, provenance: row['provenance'] }], attributes:, provenance: row['provenance'],
tag_sources: row['tag_sources'] }],
fetch_metadata: false).first fetch_metadata: false).first
return { source_row: row['source_row'], status: 'failed', errors: preview[:errors] } if preview[:errors].present? return { source_row: row['source_row'], status: 'failed', errors: preview[:errors] } if preview[:errors].present?
return row.slice('source_row').merge(status: 'skipped') if row['status'] == 'skipped' || preview[:existing] return row.slice('source_row').merge(status: 'skipped') if @existing == 'skip' && preview[:existing]
attributes['tags'] = preview[:attributes]['tags']
attributes['url'] = row['url'] attributes['url'] = row['url']
post = PostCreator.new(actor: @actor, attributes:).create! post = PostCreator.new(actor: @actor, attributes:).create!
{ source_row: row['source_row'], status: 'created', post: PostRepr.base(post) } { source_row: row['source_row'], status: 'created', post: PostRepr.base(post) }
+11 -8
ファイルの表示
@@ -29,14 +29,17 @@ class PostMetadataFetcher
raw = value.to_s.strip raw = value.to_s.strip
from = Time.zone.parse(raw) from = Time.zone.parse(raw)
before = zone = '(?:Z|[+-]\d{2}:?\d{2})?'
case raw before = case raw
when /\A\d{4}\z/ then from + 1.year when /\A\d{4}\z/ then from + 1.year
when /\A\d{4}-\d{2}\z/ then from + 1.month when /\A\d{4}-\d{2}\z/ then from + 1.month
when /\A\d{4}-\d{2}-\d{2}\z/ then from + 1.day when /\A\d{4}-\d{2}-\d{2}\z/ then from + 1.day
when /:\d{2}(?:Z|[+-]\d{2}:?\d{2})?\z/ then from + 1.second when /\A\d{4}-\d{2}-\d{2}T\d{2}#{ zone }\z/ then from + 1.hour
else from + 1.minute when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}#{ zone }\z/ then from + 1.minute
end when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}#{ zone }\z/ then from + 1.second
when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+#{ zone }\z/ then from + 0.001
else return nil
end
[from, before] [from, before]
rescue ArgumentError, TypeError rescue ArgumentError, TypeError
nil nil
+17 -8
ファイルの表示
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { Helmet } from 'react-helmet-async' import { Helmet } from 'react-helmet-async'
import FieldError from '@/components/common/FieldError' import FieldError from '@/components/common/FieldError'
@@ -28,8 +28,10 @@ type ImportRow = {
warnings: string[] warnings: string[]
errors: Record<string, string[]> errors: Record<string, string[]>
provenance: Record<string, 'automatic' | 'mapped' | 'fixed' | 'manual'> provenance: Record<string, 'automatic' | 'mapped' | 'fixed' | 'manual'>
tagSources?: Record<'automatic' | 'mapped' | 'fixed' | 'manual', string>
status: 'ready' | 'warning' | 'error' status: 'ready' | 'warning' | 'error'
existing: boolean } existing: boolean
importStatus?: 'pending' | 'created' | 'skipped' | 'failed' }
type ParsedImport = { columns: Column[], rows: { sourceRow: number, values: string[] }[] } type ParsedImport = { columns: Column[], rows: { sourceRow: number, values: string[] }[] }
type Phase = 'source' | 'mapping' | 'preview' | 'result' type Phase = 'source' | 'mapping' | 'preview' | 'result'
@@ -172,18 +174,22 @@ const PostImportPage: FC<Props> = ({ user }) => {
} }
const submit = async () => { const submit = async () => {
const targets = rows.filter (row => row.status !== 'error' && !(row.existing && existing === 'skip')) const targets = rows.filter (row => (row as ImportRow & { importStatus?: string }).importStatus !== 'created')
if (targets.length === 0) if (targets.length === 0)
return return
setLoading (true) setLoading (true)
try try
{ {
const result = await apiPost<{ created: number, skipped: number, failed: number, const result = await apiPost<{ created: number, skipped: number, failed: number,
rows: { sourceRow: number, status: string, post?: { id: number }, errors?: Record<string, string[]> }[] }> ('/posts/import', { rows: targets }) rows: { sourceRow: number, status: string, post?: { id: number }, errors?: Record<string, string[]> }[] }> ('/posts/import', { rows: targets, existing })
toast ({ title: `登録完了: ${result.created}`, toast ({ title: `登録完了: ${result.created}`,
description: `スキップ ${result.skipped} 件、失敗 ${result.failed}` }) description: `スキップ ${result.skipped} 件、失敗 ${result.failed}` })
setPhase ('result') setPhase ('result')
setResults (result.rows) setResults (result.rows)
setRows (current => current.map (row => {
const resultRow = result.rows.find (_1 => _1.sourceRow === row.sourceRow)
return resultRow ? { ...row, importStatus: resultRow.status } : row
}))
} }
catch catch
{ {
@@ -304,14 +310,17 @@ const RowCard = ({ row, index, update, updateURL }: { row: ImportRow, index: num
const EditableValue = ({ label, value, origin, onCommit }: { label: string, value: string, origin: string | undefined, onCommit: (value: string) => void }) => { const EditableValue = ({ label, value, origin, onCommit }: { label: string, value: string, origin: string | undefined, onCommit: (value: string) => void }) => {
const [editing, setEditing] = useState (false) const [editing, setEditing] = useState (false)
const [draft, setDraft] = useState (value) const [draft, setDraft] = useState (value)
const finished = useRef (false)
useEffect (() => { if (!editing) setDraft (value) }, [editing, value])
const originLabel = { automatic: '自動取得', mapped: '入力データ', fixed: '固定値', manual: '手修正' }[origin ?? 'automatic'] const originLabel = { automatic: '自動取得', mapped: '入力データ', fixed: '固定値', manual: '手修正' }[origin ?? 'automatic']
const commit = () => { setEditing (false); if (draft !== value) onCommit (draft) } const begin = () => { finished.current = false; setDraft (value); setEditing (true) }
const cancel = () => { setDraft (value); setEditing (false) } const commit = () => { if (finished.current) return; finished.current = true; setEditing (false); if (draft !== value) onCommit (draft) }
const cancel = () => { if (finished.current) return; finished.current = true; setDraft (value); setEditing (false) }
return <div className={origin === 'manual' ? 'rounded bg-amber-100 p-1 dark:bg-amber-900' : 'p-1'}> return <div className={origin === 'manual' ? 'rounded bg-amber-100 p-1 dark:bg-amber-900' : 'p-1'}>
<span className="text-xs">{label}{originLabel}</span> <span className="text-xs">{label}{originLabel}</span>
{editing ? <input autoFocus value={draft} onChange={ev => setDraft (ev.target.value)} onBlur={commit} {editing ? <input autoFocus value={draft} onChange={ev => setDraft (ev.target.value)} onBlur={commit}
onKeyDown={ev => { if (ev.key === 'Escape') cancel (); if (ev.key === 'Enter') commit () } } className={inputClass (false)}/> onKeyDown={ev => { if (ev.key === 'Escape') cancel (); if (ev.key === 'Enter') { ev.preventDefault (); commit () } } } className={inputClass (false)}/>
: <button type="button" className="block w-full text-left" onDoubleClick={() => setEditing (true)} onClick={() => setEditing (true)}>{value || '(未指定)'} </button>} : <button type="button" className="block w-full text-left" onDoubleClick={begin} onClick={begin}>{value || '(未指定)'} </button>}
</div> </div>
} }