From 440d3d38be24ed9d00b59d58f9212de51deb5c5e Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 11 Jul 2026 23:21:59 +0900 Subject: [PATCH] #399 --- .../controllers/post_imports_controller.rb | 13 ++++++++-- backend/app/services/post_import_previewer.rb | 22 ++++++++++++---- backend/app/services/post_import_runner.rb | 11 +++++--- backend/app/services/post_metadata_fetcher.rb | 19 ++++++++------ frontend/src/pages/posts/PostImportPage.tsx | 25 +++++++++++++------ 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/backend/app/controllers/post_imports_controller.rb b/backend/app/controllers/post_imports_controller.rb index 78a11b3..174450f 100644 --- a/backend/app/controllers/post_imports_controller.rb +++ b/backend/app/controllers/post_imports_controller.rb @@ -41,7 +41,8 @@ class PostImportsController < ApplicationController end 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 rescue ArgumentError => e render_bad_request(e.message) @@ -79,7 +80,7 @@ class PostImportsController < ApplicationController rows.map do |row| value = ActionController::Parameters.new(row).permit(:source_row, :sourceRow, :url, - attributes: {}, provenance: {}, + attributes: {}, provenance: {}, tag_sources: {}, fetch_warnings: [], validation_warnings: []) normalised = value.to_h.deep_transform_keys { _1.to_s.underscore } attributes = normalised.fetch('attributes', { }) @@ -90,7 +91,15 @@ class PostImportsController < ApplicationController (provenance.keys - (allowed + ['url'])).empty? raise ArgumentError, '行の形式が不正です.' 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 } + 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 end diff --git a/backend/app/services/post_import_previewer.rb b/backend/app/services/post_import_previewer.rb index af2e467..fe649cc 100644 --- a/backend/app/services/post_import_previewer.rb +++ b/backend/app/services/post_import_previewer.rb @@ -19,6 +19,8 @@ class PostImportPreviewer 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) + 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 provenance['url'] ||= row[:url].present? ? 'manual' : 'mapped' attributes['url'] = url @@ -27,9 +29,8 @@ class PostImportPreviewer metadata, fetch_warnings = metadata_for(url, should_fetch, metadata_cache, fetch_warnings) metadata.each do |field, value| if field == 'tags' && provenance[field] != 'manual' - previous_origin = provenance[field] - attributes[field] = [attributes[field], value].compact.join(' ').split.uniq.join(' ') - provenance[field] = previous_origin == 'automatic' ? 'automatic' : previous_origin + tag_sources['automatic'] = value.to_s + attributes[field] = merged_tags(tag_sources) next end next unless provenance[field] == 'automatic' || attributes[field].blank? @@ -48,10 +49,11 @@ class PostImportPreviewer @existing == 'error' ? errors[:url] = ['既存投稿です.'] : validation_warnings << '既存投稿のためスキップします.' end 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) 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:, status: errors.present? ? 'error' : ((fetch_warnings + validation_warnings).present? ? 'warning' : 'ready'), existing: normal_url.present? && Post.exists?(url: normal_url) } @@ -89,6 +91,16 @@ class PostImportPreviewer } 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 return [{ }, ['URL が空です.']] if url.blank? diff --git a/backend/app/services/post_import_runner.rb b/backend/app/services/post_import_runner.rb index 49e03a8..2627e8a 100644 --- a/backend/app/services/post_import_runner.rb +++ b/backend/app/services/post_import_runner.rb @@ -1,9 +1,10 @@ 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: + def initialize actor:, rows:, existing: 'skip' @actor = actor @rows = rows + @existing = existing == 'error' ? 'error' : 'skip' end def run @@ -22,12 +23,14 @@ class PostImportRunner 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 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'], - attributes:, provenance: row['provenance'] }], + attributes:, provenance: row['provenance'], + tag_sources: row['tag_sources'] }], fetch_metadata: false).first 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'] post = PostCreator.new(actor: @actor, attributes:).create! { source_row: row['source_row'], status: 'created', post: PostRepr.base(post) } diff --git a/backend/app/services/post_metadata_fetcher.rb b/backend/app/services/post_metadata_fetcher.rb index bafc936..9b37b37 100644 --- a/backend/app/services/post_metadata_fetcher.rb +++ b/backend/app/services/post_metadata_fetcher.rb @@ -29,14 +29,17 @@ class PostMetadataFetcher raw = value.to_s.strip from = Time.zone.parse(raw) - before = - case raw - 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}-\d{2}\z/ then from + 1.day - when /:\d{2}(?:Z|[+-]\d{2}:?\d{2})?\z/ then from + 1.second - else from + 1.minute - end + zone = '(?:Z|[+-]\d{2}:?\d{2})?' + before = case raw + 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}-\d{2}\z/ then from + 1.day + when /\A\d{4}-\d{2}-\d{2}T\d{2}#{ zone }\z/ then from + 1.hour + when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}#{ zone }\z/ then from + 1.minute + 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] rescue ArgumentError, TypeError nil diff --git a/frontend/src/pages/posts/PostImportPage.tsx b/frontend/src/pages/posts/PostImportPage.tsx index c747a73..c25a5d1 100644 --- a/frontend/src/pages/posts/PostImportPage.tsx +++ b/frontend/src/pages/posts/PostImportPage.tsx @@ -1,4 +1,4 @@ -import { useRef, useState } from 'react' +import { useEffect, useRef, useState } from 'react' import { Helmet } from 'react-helmet-async' import FieldError from '@/components/common/FieldError' @@ -28,8 +28,10 @@ type ImportRow = { warnings: string[] errors: Record provenance: Record + tagSources?: Record<'automatic' | 'mapped' | 'fixed' | 'manual', string> status: 'ready' | 'warning' | 'error' - existing: boolean } + existing: boolean + importStatus?: 'pending' | 'created' | 'skipped' | 'failed' } type ParsedImport = { columns: Column[], rows: { sourceRow: number, values: string[] }[] } type Phase = 'source' | 'mapping' | 'preview' | 'result' @@ -172,18 +174,22 @@ const PostImportPage: FC = ({ user }) => { } 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) return setLoading (true) try { const result = await apiPost<{ created: number, skipped: number, failed: number, - rows: { sourceRow: number, status: string, post?: { id: number }, errors?: Record }[] }> ('/posts/import', { rows: targets }) + rows: { sourceRow: number, status: string, post?: { id: number }, errors?: Record }[] }> ('/posts/import', { rows: targets, existing }) toast ({ title: `登録完了: ${result.created} 件`, description: `スキップ ${result.skipped} 件、失敗 ${result.failed} 件` }) setPhase ('result') 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 { @@ -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 [editing, setEditing] = useState (false) 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 commit = () => { setEditing (false); if (draft !== value) onCommit (draft) } - const cancel = () => { setDraft (value); setEditing (false) } + const begin = () => { finished.current = false; setDraft (value); setEditing (true) } + 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
{label}・{originLabel} {editing ? 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)}/> + : }
}