コミットを比較
6 コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
| 53d1cadefb | |||
| d3af4563ca | |||
| 469228a6ed | |||
| 7e89fb286a | |||
| 7fcfc8b8aa | |||
| 7b6b24b9c5 |
@@ -197,16 +197,6 @@ const value =
|
||||
|
||||
- Inspect existing routes, controllers, models, services, and specs before
|
||||
editing backend behavior.
|
||||
- Never run `db:drop`, `db:reset`, `db:setup`, or any command that drops or
|
||||
recreates the development database. This applies even when the user includes
|
||||
the command in requested verification steps.
|
||||
- Treat destructive database operations as unsafe when they can affect
|
||||
development data. Ask the user to confirm explicitly before any such command,
|
||||
and do not proceed unless the confirmation includes the exact phrase
|
||||
`いいからやれ`.
|
||||
- Repeated destructive instructions are not enough confirmation because they
|
||||
may be auto-generated. Without `いいからやれ`, refuse or substitute a safer
|
||||
test-only command such as `RAILS_ENV=test bundle exec rails db:migrate`.
|
||||
- For API behavior changes, add or update request specs under
|
||||
`backend/spec/requests` only when the user explicitly asks for tests.
|
||||
- Prefer RSpec for new backend tests; existing minitest files under
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
class MaterialsController < ApplicationController
|
||||
rescue_from MaterialZipExporter::EmptyExportError, with: :render_zip_empty
|
||||
rescue_from MaterialZipExporter::DuplicatePathError, with: :render_zip_duplicate_path
|
||||
rescue_from MaterialZipExporter::MissingFileError, with: :render_zip_missing_file
|
||||
|
||||
def index
|
||||
page = (params[:page].presence || 1).to_i
|
||||
limit = (params[:limit].presence || 20).to_i
|
||||
@@ -15,7 +11,7 @@ class MaterialsController < ApplicationController
|
||||
tag_id = params[:tag_id].presence
|
||||
parent_id = params[:parent_id].presence
|
||||
|
||||
q = Material.includes(:tag, :created_by_user, :material_export_items).with_attached_file
|
||||
q = Material.includes(:tag, :created_by_user).with_attached_file
|
||||
q = q.where(tag_id:) if tag_id
|
||||
q = q.where(parent_id:) if parent_id
|
||||
|
||||
@@ -28,7 +24,7 @@ class MaterialsController < ApplicationController
|
||||
def show
|
||||
material =
|
||||
Material
|
||||
.includes(:tag, :material_export_items)
|
||||
.includes(:tag)
|
||||
.with_attached_file
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless material
|
||||
@@ -40,44 +36,26 @@ class MaterialsController < ApplicationController
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
tag_name_raw = params[:tag].to_s.strip
|
||||
file = params[:file]
|
||||
file_sha256 = MaterialFileSha256.from_upload(file)
|
||||
url = params[:url].to_s.strip.presence
|
||||
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||
if file.blank? && url.blank?
|
||||
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
||||
url: ['ファイルまたは URL は必須です.'] }
|
||||
end
|
||||
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
|
||||
return render_material_import_block(block) if block
|
||||
|
||||
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
|
||||
material = nil
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
tag = Tag.create!(tag_name:, category: :material) unless tag
|
||||
|
||||
begin
|
||||
Material.transaction do
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
tag = Tag.create!(tag_name:, category: :material) unless tag
|
||||
material = Material.new(tag:, url:,
|
||||
created_by_user: current_user,
|
||||
updated_by_user: current_user)
|
||||
material.file.attach(file)
|
||||
|
||||
material = Material.new(tag:, url:,
|
||||
created_by_user: current_user,
|
||||
updated_by_user: current_user)
|
||||
material.file.attach(uploaded_blob) if uploaded_blob
|
||||
material.save!
|
||||
upsert_export_paths!(material)
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: current_user)
|
||||
end
|
||||
rescue StandardError
|
||||
uploaded_blob&.purge_later
|
||||
raise
|
||||
end
|
||||
|
||||
if material
|
||||
if material.save
|
||||
render json: MaterialRepr.base(material, host: request.base_url), status: :created
|
||||
else
|
||||
render_validation_error material
|
||||
@@ -93,43 +71,29 @@ class MaterialsController < ApplicationController
|
||||
|
||||
tag_name_raw = params[:tag].to_s.strip
|
||||
file = params[:file]
|
||||
file_sha256 = MaterialFileSha256.from_upload(file)
|
||||
url = params.key?(:url) ? params[:url].to_s.strip.presence : material.url
|
||||
url = params[:url].to_s.strip.presence
|
||||
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||
if file.blank? && url.blank? && !material.file.attached?
|
||||
if file.blank? && url.blank?
|
||||
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
||||
url: ['ファイルまたは URL は必須です.'] }
|
||||
end
|
||||
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
|
||||
return render_material_import_block(block) if block
|
||||
|
||||
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
tag = Tag.create!(tag_name:, category: :material) unless tag
|
||||
|
||||
begin
|
||||
Material.transaction do
|
||||
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
tag = Tag.create!(tag_name:, category: :material) unless tag
|
||||
|
||||
material.assign_attributes(tag:, url:, updated_by_user: current_user)
|
||||
if uploaded_blob
|
||||
material.file.attach(uploaded_blob)
|
||||
clear_file_suppression!(material)
|
||||
elsif params.key?(:url) && url.present? && file.blank?
|
||||
material.file.detach
|
||||
end
|
||||
material.save!
|
||||
upsert_export_paths!(material)
|
||||
MaterialVersionRecorder.record!(material:, event_type: :update,
|
||||
created_by_user: current_user)
|
||||
end
|
||||
rescue StandardError
|
||||
uploaded_blob&.purge_later
|
||||
raise
|
||||
material.update!(tag:, url:, updated_by_user: current_user)
|
||||
if file
|
||||
material.file.attach(file)
|
||||
else
|
||||
material.file.purge
|
||||
end
|
||||
|
||||
render json: MaterialRepr.base(material, host: request.base_url)
|
||||
if material.save
|
||||
render json: MaterialRepr.base(material, host: request.base_url)
|
||||
else
|
||||
render_validation_error material
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@@ -139,146 +103,7 @@ class MaterialsController < ApplicationController
|
||||
material = Material.find_by(id: params[:id])
|
||||
return head :not_found unless material
|
||||
|
||||
Material.transaction do
|
||||
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
||||
material.discard!
|
||||
MaterialVersionRecorder.record!(material:, event_type: :discard,
|
||||
created_by_user: current_user)
|
||||
end
|
||||
material.discard
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def download
|
||||
zip = MaterialZipExporter.new(profile: params[:profile],
|
||||
tag_id: params[:tag_id]).export
|
||||
profile = params[:profile].presence || 'legacy_drive'
|
||||
send_data zip,
|
||||
type: 'application/zip',
|
||||
disposition: 'attachment',
|
||||
filename: "btrc-materials-#{ profile }.zip"
|
||||
end
|
||||
|
||||
def suppress_file
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.admin?
|
||||
|
||||
material = Material.with_attached_file.find_by(id: params[:id])
|
||||
return head :not_found unless material
|
||||
|
||||
reason = params[:reason].to_s.strip.presence
|
||||
return render_unprocessable_entity('理由は必須です.', field: :reason) unless reason
|
||||
purge = bool?(:purge)
|
||||
file_snapshot = purge_material_file_snapshot(material) if purge
|
||||
attachment = purge && material.file.attached? ? material.file.attachment : nil
|
||||
|
||||
Material.transaction do
|
||||
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
||||
material.update!(file_suppressed_at: Time.current,
|
||||
file_suppressed_by_user: current_user,
|
||||
file_suppression_reason: reason,
|
||||
updated_by_user: current_user)
|
||||
MaterialVersionRecorder.record!(material:, event_type: :suppress,
|
||||
created_by_user: current_user,
|
||||
file_snapshot:)
|
||||
end
|
||||
# Enqueue failure raises here after the suppress metadata has been committed.
|
||||
# In that case the file remains suppressed in UI/ZIP and purge can be retried.
|
||||
attachment&.purge_later
|
||||
material.reload if purge
|
||||
|
||||
render json: MaterialRepr.base(material, host: request.base_url)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def upsert_export_paths! material
|
||||
raw = params[:export_paths]
|
||||
return if raw.blank?
|
||||
|
||||
export_paths = raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw.to_h
|
||||
export_paths.each do |profile, export_path|
|
||||
profile = profile.to_s
|
||||
export_path = export_path.to_s.strip
|
||||
item = material.material_export_items.find_or_initialize_by(profile:)
|
||||
|
||||
if export_path.blank?
|
||||
item.destroy! if item.persisted?
|
||||
next
|
||||
end
|
||||
|
||||
item.export_path = export_path
|
||||
item.enabled = true
|
||||
item.created_by_user ||= current_user
|
||||
item.save!
|
||||
end
|
||||
end
|
||||
|
||||
def render_zip_empty
|
||||
render_unprocessable_entity('ZIP export 対象の素材がありません.')
|
||||
end
|
||||
|
||||
def render_zip_duplicate_path error
|
||||
render_unprocessable_entity("ZIP export path が重複してゐます: #{ error.message }")
|
||||
end
|
||||
|
||||
def render_zip_missing_file error
|
||||
missing_files = error.missing_files.map do |missing_file|
|
||||
{ material_id: missing_file.material_id,
|
||||
export_path: missing_file.export_path,
|
||||
blob_id: missing_file.blob_id,
|
||||
filename: missing_file.filename }
|
||||
end
|
||||
|
||||
render json: { type: 'validation_error',
|
||||
message: 'ZIP export に必要な素材ファイルが欠損しています.',
|
||||
errors: { },
|
||||
base_errors: ['ZIP export に必要な素材ファイルが欠損しています.'],
|
||||
missing_files: },
|
||||
status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def build_uploaded_material_blob! file, file_sha256
|
||||
return nil unless file
|
||||
|
||||
file.tempfile.rewind
|
||||
|
||||
blob = ActiveStorage::Blob.create_and_upload!(
|
||||
io: file.tempfile,
|
||||
filename: file.original_filename,
|
||||
content_type: file.content_type,
|
||||
)
|
||||
|
||||
if file_sha256.present?
|
||||
blob.metadata['sha256'] = file_sha256
|
||||
blob.save! if blob.changed?
|
||||
end
|
||||
|
||||
blob
|
||||
ensure
|
||||
file.tempfile.rewind if file&.tempfile
|
||||
end
|
||||
|
||||
def clear_file_suppression! material
|
||||
material.file_suppressed_at = nil
|
||||
material.file_suppressed_by_user = nil
|
||||
material.file_suppression_reason = nil
|
||||
end
|
||||
|
||||
def purge_material_file_snapshot material
|
||||
return nil unless material.file.attached?
|
||||
|
||||
blob = material.file.blob
|
||||
|
||||
{ file_blob_id: blob.id,
|
||||
file_filename: blob.filename.to_s,
|
||||
file_content_type: blob.content_type,
|
||||
file_byte_size: blob.byte_size,
|
||||
file_checksum: blob.checksum,
|
||||
file_sha256: blob.metadata['sha256'] ||
|
||||
MaterialFileSha256.from_blob(blob, allow_download: true) }
|
||||
end
|
||||
|
||||
def render_material_import_block block
|
||||
render_validation_error fields: { file: ["抑止された素材です: #{ block.reason }"] }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,6 +25,7 @@ class PostVersionsController < ApplicationController
|
||||
SQL
|
||||
.select('post_versions.*', 'prev.title AS prev_title', 'prev.url AS prev_url',
|
||||
'prev.thumbnail_base AS prev_thumbnail_base', 'prev.tags AS prev_tags',
|
||||
'prev.video_ms AS prev_video_ms',
|
||||
'prev.original_created_from AS prev_original_created_from',
|
||||
'prev.original_created_before AS prev_original_created_before')
|
||||
q = q.where('post_versions.post_id = ?', post_id) if post_id
|
||||
@@ -74,6 +75,10 @@ class PostVersionsController < ApplicationController
|
||||
current: row.thumbnail_base,
|
||||
prev: row.attributes['prev_thumbnail_base']
|
||||
},
|
||||
video_ms: {
|
||||
current: row.video_ms,
|
||||
prev: row.attributes['prev_video_ms']
|
||||
},
|
||||
tags: build_version_tags(cur_tags, prev_tags),
|
||||
original_created_from: {
|
||||
current: row.original_created_from&.iso8601,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
class PostsController < ApplicationController
|
||||
Event = Struct.new(:post, :tag, :user, :change_type, :timestamp, keyword_init: true)
|
||||
|
||||
class VideoMsParseError < ArgumentError
|
||||
;
|
||||
end
|
||||
|
||||
def index
|
||||
url = params[:url].presence
|
||||
title = params[:title].presence
|
||||
@@ -45,7 +49,9 @@ class PostsController < ApplicationController
|
||||
.joins("LEFT JOIN (#{ pt_max_sql }) pt_max ON pt_max.post_id = posts.id")
|
||||
.reselect('posts.*', Arel.sql("#{ updated_at_all_sql } AS updated_at_all"))
|
||||
.preload(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
active_post_tags: [:sections,
|
||||
{ tag: [:deerjikists, :materials,
|
||||
{ tag_name: :wiki_page }] }])
|
||||
.with_attached_thumbnail
|
||||
|
||||
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
||||
@@ -97,7 +103,9 @@ class PostsController < ApplicationController
|
||||
|
||||
def random
|
||||
post = filtered_posts.preload(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
active_post_tags: [:sections,
|
||||
{ tag: [:deerjikists, :materials,
|
||||
{ tag_name: :wiki_page }] }])
|
||||
.with_attached_thumbnail
|
||||
.order('RAND()')
|
||||
.first
|
||||
@@ -110,7 +118,9 @@ class PostsController < ApplicationController
|
||||
post =
|
||||
Post
|
||||
.includes(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
active_post_tags: [:sections,
|
||||
{ tag: [:deerjikists, :materials,
|
||||
{ tag_name: :wiki_page }] }])
|
||||
.with_attached_thumbnail
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless post
|
||||
@@ -125,7 +135,7 @@ class PostsController < ApplicationController
|
||||
child_posts:,
|
||||
sibling_posts:,
|
||||
related:)
|
||||
.merge(tags: build_tag_tree_for(post.tags))
|
||||
.merge(tags: build_tag_tree_for(post))
|
||||
end
|
||||
|
||||
def create
|
||||
@@ -148,11 +158,15 @@ class PostsController < ApplicationController
|
||||
ApplicationRecord.transaction do
|
||||
post.save!
|
||||
|
||||
tags = Tag.normalise_tags!(tag_names, deny_deprecated: true)
|
||||
Tag.normalise_tags!(tag_names, deny_deprecated: true, with_sections: true) =>
|
||||
{ tags:, sections: }
|
||||
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user)
|
||||
|
||||
tags = Tag.expand_parent_tags(tags).reject(&:deprecated?)
|
||||
sync_post_tags!(post, tags)
|
||||
post.video_ms = normalise_video_ms(tags)
|
||||
validate_video_sections!(post.video_ms, sections)
|
||||
post.save!
|
||||
sync_post_tags!(post, tags, sections)
|
||||
|
||||
sync_parent_posts!(post, parent_post_ids)
|
||||
|
||||
@@ -167,6 +181,10 @@ class PostsController < ApplicationController
|
||||
render_validation_error fields: { tags: 'ニコニコ・タグは直接指定できません.' }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags
|
||||
rescue Tag::SectionLiteralParseError
|
||||
render_validation_error fields: { tags: ['タグ区間の記法が不正です.'] }
|
||||
rescue VideoMsParseError
|
||||
render_validation_error fields: { video_ms: ['動画時間の記法が不正です.'] }
|
||||
rescue ArgumentError => e
|
||||
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
@@ -223,6 +241,8 @@ class PostsController < ApplicationController
|
||||
original_created_from:,
|
||||
original_created_before:,
|
||||
tag_names:,
|
||||
video_ms_param: params[:video_ms],
|
||||
duration_param: params[:duration],
|
||||
parent_post_ids:)
|
||||
|
||||
snapshot_to_apply =
|
||||
@@ -253,12 +273,16 @@ class PostsController < ApplicationController
|
||||
|
||||
post.reload
|
||||
json = PostRepr.base(post, current_user)
|
||||
json['tags'] = build_tag_tree_for(post.tags)
|
||||
json['tags'] = build_tag_tree_for(post)
|
||||
render json:, status: :ok
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
render_validation_error fields: { tags: ['ニコニコ・タグは直接指定できません.'] }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags
|
||||
rescue Tag::SectionLiteralParseError
|
||||
render_validation_error fields: { tags: ['タグ区間の記法が不正です.'] }
|
||||
rescue VideoMsParseError
|
||||
render_validation_error fields: { video_ms: ['動画時間の記法が不正です.'] }
|
||||
rescue ArgumentError => e
|
||||
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
@@ -357,7 +381,7 @@ class PostsController < ApplicationController
|
||||
def tagged_post_ids_for(name) =
|
||||
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)
|
||||
|
||||
def sync_post_tags! post, desired_tags
|
||||
def sync_post_tags! post, desired_tags, sections
|
||||
desired_tags.each do |t|
|
||||
t.save! if t.new_record?
|
||||
end
|
||||
@@ -376,13 +400,21 @@ class PostsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
PostTagSection.where(post_id: post.id).destroy_all
|
||||
sections.each do |tag_id, ranges|
|
||||
ranges.each do |begin_ms, end_ms|
|
||||
PostTagSection.create!(post_id: post.id, tag_id:, begin_ms:, end_ms:)
|
||||
end
|
||||
end
|
||||
|
||||
PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
|
||||
pt.discard_by!(current_user)
|
||||
end
|
||||
end
|
||||
|
||||
def build_tag_tree_for tags
|
||||
tags = tags.reject(&:deprecated?).to_a
|
||||
def build_tag_tree_for post
|
||||
post_tags = post.active_post_tags.reject { |post_tag| post_tag.tag.deprecated? }
|
||||
tags = post_tags.map(&:tag)
|
||||
tag_ids = tags.map(&:id)
|
||||
|
||||
implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
|
||||
@@ -397,6 +429,9 @@ class PostsController < ApplicationController
|
||||
root_ids = tag_ids - child_ids
|
||||
|
||||
tags_by_id = tags.index_by(&:id)
|
||||
sections_by_tag_id = post_tags.to_h { |post_tag|
|
||||
[post_tag.tag_id, post_tag.sections.as_json(only: [:begin_ms, :end_ms])]
|
||||
}
|
||||
|
||||
memo = { }
|
||||
|
||||
@@ -404,8 +439,10 @@ class PostsController < ApplicationController
|
||||
tag = tags_by_id[tag_id]
|
||||
return nil unless tag
|
||||
|
||||
sections = sections_by_tag_id.fetch(tag_id, [])
|
||||
|
||||
if path.include?(tag_id)
|
||||
return TagRepr.inline(tag).merge(children: [])
|
||||
return TagRepr.inline(tag).merge(children: [], sections:)
|
||||
end
|
||||
|
||||
if memo.key?(tag_id)
|
||||
@@ -417,7 +454,7 @@ class PostsController < ApplicationController
|
||||
|
||||
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
||||
|
||||
memo[tag_id] = TagRepr.inline(tag).merge(children:)
|
||||
memo[tag_id] = TagRepr.inline(tag).merge(children:, sections:)
|
||||
end
|
||||
|
||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||
@@ -442,7 +479,7 @@ class PostsController < ApplicationController
|
||||
|
||||
params[:parent_post_ids].to_s.split.map { |token|
|
||||
id = Integer(token, exception: false)
|
||||
raise ArgumentError, "親投稿 Id. が不正です: #{ token }" if id.nil? || id <= 0
|
||||
raise ArgumentError, "親投稿 Id. が不正です: #{ token }" if !(id) || id <= 0
|
||||
|
||||
id
|
||||
}.uniq
|
||||
@@ -486,6 +523,7 @@ class PostsController < ApplicationController
|
||||
|
||||
def post_snapshot_from_version version
|
||||
{ title: version.title,
|
||||
video_ms: version.respond_to?(:video_ms) ? version.video_ms : nil,
|
||||
original_created_from: snapshot_time(version.original_created_from),
|
||||
original_created_before: snapshot_time(version.original_created_before),
|
||||
tag_names: editable_tag_names_from_version(version),
|
||||
@@ -498,6 +536,7 @@ class PostsController < ApplicationController
|
||||
|
||||
def post_snapshot_from_record post
|
||||
{ title: post.title,
|
||||
video_ms: post.video_ms,
|
||||
original_created_from: snapshot_time(post.original_created_from),
|
||||
original_created_before: snapshot_time(post.original_created_before),
|
||||
tag_names: editable_tag_names_from_post(post),
|
||||
@@ -505,16 +544,41 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def editable_tag_names_from_post post
|
||||
post.tags.not_nico.where(deprecated_at: nil)
|
||||
.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
|
||||
post
|
||||
.post_tags
|
||||
.kept
|
||||
.joins(tag: :tag_name)
|
||||
.merge(Tag.not_nico)
|
||||
.merge(Tag.where(deprecated_at: nil))
|
||||
.includes(:sections, tag: :tag_name)
|
||||
.order('tag_names.name')
|
||||
.map do |post_tag|
|
||||
name = post_tag.tag.tag_name.name
|
||||
sections = post_tag.sections.sort_by(&:begin_ms)
|
||||
|
||||
next name if sections.empty?
|
||||
|
||||
"#{ name }#{ sections.map { Post.section_literal(_1) }.join }"
|
||||
end
|
||||
end
|
||||
|
||||
def post_incoming_snapshot title:, original_created_from:, original_created_before:,
|
||||
tag_names:, parent_post_ids:
|
||||
tag_names:, video_ms_param:, duration_param:, parent_post_ids:
|
||||
Tag.normalise_tags!(tag_names, with_tagme: false, deny_deprecated: true,
|
||||
with_sections: true) =>
|
||||
{ tags:, sections: }
|
||||
|
||||
tags = Tag.expand_parent_tags(tags).reject(&:deprecated?)
|
||||
video_ms = normalise_video_ms(tags, video_ms_param:, duration_param:)
|
||||
validate_video_sections!(video_ms, sections)
|
||||
|
||||
{ title:,
|
||||
video_ms:,
|
||||
original_created_from: snapshot_time(original_created_from),
|
||||
original_created_before: snapshot_time(original_created_before),
|
||||
tag_names: incoming_tag_names_for_snapshot(tag_names),
|
||||
tag_names: tags.uniq(&:id).map { |tag|
|
||||
"#{ tag.name }#{ sections[tag.id].to_a.map { section_literal(_1) }.join }"
|
||||
}.sort,
|
||||
parent_post_ids: parent_post_ids.sort }
|
||||
end
|
||||
|
||||
@@ -537,11 +601,8 @@ class PostsController < ApplicationController
|
||||
value.to_s
|
||||
end
|
||||
|
||||
def incoming_tag_names_for_snapshot raw_tag_names
|
||||
tags = Tag.normalise_tags!(raw_tag_names, with_tagme: false,
|
||||
deny_deprecated: true)
|
||||
|
||||
Tag.expand_parent_tags(tags).reject(&:deprecated?).map(&:name).uniq.sort
|
||||
def section_literal section
|
||||
"[#{ Post.ms_to_time(section[0]) }-#{ section[1] ? Post.ms_to_time(section[1]) : '' }]"
|
||||
end
|
||||
|
||||
def post_conflict_json post:, base_version_no:, base_snapshot:,
|
||||
@@ -562,6 +623,8 @@ class PostsController < ApplicationController
|
||||
def post_snapshot_changes base_snapshot, current_snapshot, incoming_snapshot
|
||||
[scalar_snapshot_change(:title, 'タイトル',
|
||||
base_snapshot, current_snapshot, incoming_snapshot),
|
||||
scalar_snapshot_change(:video_ms, '動画時間',
|
||||
base_snapshot, current_snapshot, incoming_snapshot),
|
||||
scalar_snapshot_change(:original_created_from, 'オリジナルの作成日時(以降)',
|
||||
base_snapshot, current_snapshot, incoming_snapshot),
|
||||
scalar_snapshot_change(:original_created_before, 'オリジナルの作成日時(より前)',
|
||||
@@ -625,11 +688,14 @@ class PostsController < ApplicationController
|
||||
PostVersionRecorder.ensure_snapshot!(post, created_by_user: current_user)
|
||||
|
||||
post.update!(title: snapshot[:title],
|
||||
video_ms: snapshot[:video_ms],
|
||||
original_created_from: snapshot[:original_created_from],
|
||||
original_created_before: snapshot[:original_created_before])
|
||||
|
||||
editable_tags = Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false,
|
||||
deny_deprecated: true)
|
||||
Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false,
|
||||
deny_deprecated: true,
|
||||
with_sections: true) =>
|
||||
{ tags: editable_tags, sections: }
|
||||
TagVersioning.record_tag_snapshots!(editable_tags, created_by_user: current_user)
|
||||
|
||||
readonly_tags = post.tags.nico.to_a
|
||||
@@ -637,14 +703,17 @@ class PostsController < ApplicationController
|
||||
tags = readonly_tags + editable_tags
|
||||
tags = Tag.expand_parent_tags(tags).reject(&:deprecated?)
|
||||
|
||||
sync_post_tags!(post, tags)
|
||||
post.video_ms = tags.any? { _1.id == Tag.video.id } ? snapshot[:video_ms] : nil
|
||||
validate_video_sections!(post.video_ms, sections)
|
||||
post.save!
|
||||
sync_post_tags!(post, tags, sections)
|
||||
sync_parent_posts!(post, snapshot[:parent_post_ids])
|
||||
|
||||
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user)
|
||||
end
|
||||
|
||||
def merge_post_snapshots base_snapshot, current_snapshot, incoming_snapshot
|
||||
[:title, :original_created_from, :original_created_before].map {
|
||||
[:title, :video_ms, :original_created_from, :original_created_before].map {
|
||||
[_1, merge_scalar_snapshot_value(base_snapshot[_1],
|
||||
current_snapshot[_1],
|
||||
incoming_snapshot[_1])]
|
||||
@@ -688,4 +757,44 @@ class PostsController < ApplicationController
|
||||
render_validation_error record
|
||||
end
|
||||
end
|
||||
|
||||
def normalise_video_ms tags, video_ms_param: params[:video_ms], duration_param: params[:duration]
|
||||
return nil unless tags.any? { _1.id == Tag.video.id }
|
||||
|
||||
if video_ms_param.present?
|
||||
video_ms = Integer(video_ms_param, exception: false)
|
||||
raise VideoMsParseError unless video_ms&.positive?
|
||||
|
||||
return video_ms
|
||||
end
|
||||
|
||||
return nil if duration_param.blank?
|
||||
|
||||
video_ms = Tag.time_to_ms!(duration_param.to_s, tag_name: '動画時間')
|
||||
raise VideoMsParseError unless video_ms.positive?
|
||||
|
||||
video_ms
|
||||
rescue Tag::SectionLiteralParseError
|
||||
raise VideoMsParseError
|
||||
end
|
||||
|
||||
def validate_video_sections! video_ms, sections
|
||||
return unless video_ms
|
||||
|
||||
sections.each_value do |ranges|
|
||||
ranges.each do |begin_ms, end_ms|
|
||||
if begin_ms >= video_ms
|
||||
post = Post.new
|
||||
post.errors.add :video_ms, 'タグ区間の開始が動画時間以上です.'
|
||||
raise ActiveRecord::RecordInvalid, post
|
||||
end
|
||||
|
||||
if end_ms && end_ms > video_ms
|
||||
post = Post.new
|
||||
post.errors.add :video_ms, 'タグ区間の終端が動画時間を超えてゐます.'
|
||||
raise ActiveRecord::RecordInvalid, post
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -437,10 +437,16 @@ class TagsController < ApplicationController
|
||||
|
||||
def build_tag_children tag
|
||||
material = tag.materials.first
|
||||
file = nil
|
||||
content_type = nil
|
||||
if material&.file&.attached?
|
||||
file = rails_storage_proxy_url(material.file, only_path: false)
|
||||
content_type = material.file.blob.content_type
|
||||
end
|
||||
|
||||
TagRepr.base(tag).merge(
|
||||
children: tag.children.sort_by { _1.name }.map { build_tag_children(_1) },
|
||||
material: material && MaterialRepr.base(material, host: request.base_url))
|
||||
material: material.as_json&.merge(file:, content_type:))
|
||||
end
|
||||
|
||||
def record_tag_version! tag, event_type:, created_by_user:, name_changed: false, wiki_page: nil
|
||||
|
||||
@@ -9,10 +9,6 @@ class Material < ApplicationRecord
|
||||
belongs_to :tag, optional: true
|
||||
belongs_to :created_by_user, class_name: 'User', optional: true
|
||||
belongs_to :updated_by_user, class_name: 'User', optional: true
|
||||
belongs_to :file_suppressed_by_user, class_name: 'User', optional: true
|
||||
|
||||
has_many :material_versions, dependent: :destroy
|
||||
has_many :material_export_items, dependent: :destroy
|
||||
|
||||
has_one_attached :file, dependent: :purge
|
||||
|
||||
@@ -22,18 +18,11 @@ class Material < ApplicationRecord
|
||||
validate :tag_must_be_material_category
|
||||
|
||||
def content_type
|
||||
return nil if file_suppressed?
|
||||
return nil unless file&.attached?
|
||||
|
||||
file.blob.content_type
|
||||
end
|
||||
|
||||
def file_suppressed? = file_suppressed_at.present?
|
||||
|
||||
def snapshot_export_paths
|
||||
material_export_items.order(:profile).pluck(:profile, :export_path).to_h
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def file_must_be_attached
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
class MaterialExportItem < ApplicationRecord
|
||||
VALID_PROFILES = ['legacy_drive'].freeze
|
||||
|
||||
belongs_to :material
|
||||
belongs_to :created_by_user, class_name: 'User', optional: true
|
||||
|
||||
validates :profile, presence: true, inclusion: { in: VALID_PROFILES }
|
||||
validates :export_path, presence: true, uniqueness: { scope: :profile }
|
||||
validates :material_id, uniqueness: { scope: :profile }
|
||||
validate :export_path_must_be_relative_safe_path
|
||||
|
||||
scope :enabled, -> { where(enabled: true) }
|
||||
|
||||
private
|
||||
|
||||
def export_path_must_be_relative_safe_path
|
||||
return if export_path.blank?
|
||||
|
||||
if export_path.start_with?('/')
|
||||
errors.add(:export_path, '絶対パスは使へません.')
|
||||
end
|
||||
|
||||
if export_path.match?(/\A[A-Za-z]:\//)
|
||||
errors.add(:export_path, '絶対パスは使へません.')
|
||||
end
|
||||
|
||||
if export_path.include?('\\')
|
||||
errors.add(:export_path, '/ 区切りで指定してください.')
|
||||
end
|
||||
|
||||
if export_path.include?("\0")
|
||||
errors.add(:export_path, 'NUL は使へません.')
|
||||
end
|
||||
|
||||
parts = export_path.split('/')
|
||||
if export_path.include?('//')
|
||||
errors.add(:export_path, '空の path segment は使へません.')
|
||||
end
|
||||
|
||||
if parts.any? { |part| part.in?(['.', '..']) }
|
||||
errors.add(:export_path, '.. は使へません.')
|
||||
end
|
||||
|
||||
if export_path.end_with?('/')
|
||||
errors.add(:export_path, 'directory path は使へません.')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,29 +0,0 @@
|
||||
class MaterialImportBlock < ApplicationRecord
|
||||
MATCH_KINDS = ['sha256', 'exact_path', 'path_prefix', 'manual'].freeze
|
||||
REASONS = [
|
||||
'copyright_high_risk',
|
||||
'copyright_takedown',
|
||||
'adult_or_sensitive',
|
||||
'personal_information',
|
||||
'malware_or_dangerous_file',
|
||||
'duplicate_or_low_quality',
|
||||
'source_owner_request',
|
||||
'other'
|
||||
].freeze
|
||||
|
||||
belongs_to :created_by_user, class_name: 'User', optional: true
|
||||
|
||||
validates :match_kind, presence: true, inclusion: { in: MATCH_KINDS }
|
||||
validates :reason, presence: true, inclusion: { in: REASONS }
|
||||
validates :sha256, length: { is: 64 }, allow_blank: true
|
||||
validate :match_value_must_be_present
|
||||
|
||||
private
|
||||
|
||||
def match_value_must_be_present
|
||||
return if match_kind == 'manual'
|
||||
return if sha256.present? || external_path_pattern.present?
|
||||
|
||||
errors.add(:base, 'sha256 または external_path_pattern は必須です.')
|
||||
end
|
||||
end
|
||||
@@ -1,18 +0,0 @@
|
||||
class MaterialVersion < ApplicationRecord
|
||||
EVENT_TYPE_MAP = { create: 'create',
|
||||
update: 'update',
|
||||
discard: 'discard',
|
||||
restore: 'restore',
|
||||
suppress: 'suppress' }.freeze
|
||||
|
||||
include VersionRecord
|
||||
|
||||
belongs_to :material
|
||||
belongs_to :tag, optional: true
|
||||
belongs_to :parent, class_name: 'Material', optional: true
|
||||
belongs_to :updated_by_user, class_name: 'User', optional: true
|
||||
|
||||
def export_paths_hash
|
||||
export_paths_json || {}
|
||||
end
|
||||
end
|
||||
@@ -46,6 +46,7 @@ class Post < ApplicationRecord
|
||||
before_validation :normalise_url
|
||||
|
||||
validates :url, presence: true, uniqueness: true
|
||||
validates :video_ms, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
|
||||
|
||||
validate :validate_original_created_range
|
||||
validate :url_must_be_http_url
|
||||
@@ -69,7 +70,42 @@ class Post < ApplicationRecord
|
||||
super(options).merge(thumbnail: nil)
|
||||
end
|
||||
|
||||
def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
|
||||
def snapshot_tag_names
|
||||
post_tags
|
||||
.kept
|
||||
.joins(tag: :tag_name)
|
||||
.includes(:sections, tag: :tag_name)
|
||||
.order('tag_names.name')
|
||||
.map do |post_tag|
|
||||
name = post_tag.tag.tag_name.name
|
||||
sections = post_tag.sections.sort_by(&:begin_ms)
|
||||
|
||||
next name if sections.empty?
|
||||
|
||||
"#{ name }#{ sections.map { Post.section_literal(_1) }.join }"
|
||||
end
|
||||
end
|
||||
|
||||
def self.section_literal section
|
||||
"[#{ Post.ms_to_time(section.begin_ms) }-#{ section.end_ms ? Post.ms_to_time(section.end_ms) : '' }]"
|
||||
end
|
||||
|
||||
def self.ms_to_time ms
|
||||
total_s = ms / 1_000
|
||||
s = total_s % 60
|
||||
min = (total_s / 60) % 60
|
||||
h = total_s / 3_600
|
||||
remainder_ms = ms % 1_000
|
||||
|
||||
base =
|
||||
if h.positive?
|
||||
'%d:%02d:%02d' % [h, min, s]
|
||||
else
|
||||
'%d:%02d' % [min, s]
|
||||
end
|
||||
|
||||
remainder_ms.positive? ? "#{ base }.#{ remainder_ms.to_s.rjust(3, '0') }" : base
|
||||
end
|
||||
|
||||
def snapshot_parent_post_ids = parents.order(:id).pluck(:id)
|
||||
|
||||
|
||||
@@ -10,6 +10,12 @@ class PostTag < ApplicationRecord
|
||||
belongs_to :created_user, class_name: 'User', optional: true
|
||||
belongs_to :deleted_user, class_name: 'User', optional: true
|
||||
|
||||
has_many :sections, -> { order(:begin_ms) }, class_name: 'PostTagSection',
|
||||
foreign_key: [:post_id, :tag_id],
|
||||
primary_key: [:post_id, :tag_id],
|
||||
dependent: :delete_all,
|
||||
inverse_of: :post_tag
|
||||
|
||||
validates :post_id, presence: true
|
||||
validates :tag_id, presence: true
|
||||
validates :post_id, uniqueness: {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
class PostTagSection < ApplicationRecord
|
||||
self.primary_key = :post_id, :tag_id, :begin_ms
|
||||
|
||||
belongs_to :post
|
||||
belongs_to :tag
|
||||
|
||||
belongs_to :post_tag, -> { kept }, foreign_key: [:post_id, :tag_id],
|
||||
primary_key: [:post_id, :tag_id],
|
||||
inverse_of: :sections,
|
||||
optional: true
|
||||
|
||||
validates :post_id, presence: true
|
||||
validates :tag_id, presence: true
|
||||
|
||||
validates :begin_ms, presence: true,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
|
||||
validates :end_ms, numericality: { only_integer: true, greater_than: :begin_ms },
|
||||
allow_nil: true
|
||||
end
|
||||
@@ -5,6 +5,7 @@ class PostVersion < ApplicationRecord
|
||||
belongs_to :parent, class_name: 'Post', optional: true
|
||||
|
||||
validates :url, presence: true
|
||||
validates :video_ms, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
|
||||
|
||||
validate :validate_original_created_range
|
||||
|
||||
|
||||
+112
-3
@@ -17,6 +17,16 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
class SectionLiteralParseError < ArgumentError
|
||||
attr_reader :tag_name, :literal
|
||||
|
||||
def initialize tag_name, literal
|
||||
@tag_name = tag_name
|
||||
@literal = literal
|
||||
super("invalid section literal for tag #{ tag_name }: #{ literal }")
|
||||
end
|
||||
end
|
||||
|
||||
has_many :post_tags, inverse_of: :tag
|
||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
@@ -105,26 +115,60 @@ class Tag < ApplicationRecord
|
||||
def self.normalise_tags! tag_names, with_tagme: true,
|
||||
with_no_deerjikist: true,
|
||||
deny_nico: true,
|
||||
deny_deprecated: false
|
||||
deny_deprecated: false,
|
||||
with_sections: false
|
||||
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') }
|
||||
raise NicoTagNormalisationError
|
||||
end
|
||||
|
||||
sections = { }
|
||||
tags = tag_names.map do |name|
|
||||
raw_name = name
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.downcase.start_with?(p) } || ['', nil]
|
||||
name = TagName.canonicalise(name.sub(/\A#{ pf }/i, '')).first
|
||||
|
||||
name = name.sub(/\A#{ pf }/i, '')
|
||||
|
||||
sections_by_tag = []
|
||||
while (match = name.match(/\A(\S*?)\[([^\[\]\s]*)-([^\[\]\s]*)\](\S*)\z/))
|
||||
name = "#{ match[1] }#{ match[4] }"
|
||||
next if match[2].empty? && match[3].empty?
|
||||
|
||||
sections_by_tag << normalise_section_range!(
|
||||
begin_raw: match[2],
|
||||
end_raw: match[3],
|
||||
tag_name: name)
|
||||
end
|
||||
|
||||
if name.include?('[') || name.include?(']')
|
||||
raise SectionLiteralParseError.new(raw_name, raw_name)
|
||||
end
|
||||
|
||||
name = TagName.canonicalise(name).first
|
||||
|
||||
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag|
|
||||
if deny_deprecated && tag.deprecated?
|
||||
raise DeprecatedTagNormalisationError, [tag.name]
|
||||
end
|
||||
|
||||
tag.update!(category: cat) if cat && tag.category != cat
|
||||
next if sections_by_tag.empty?
|
||||
|
||||
sections[tag.id] ||= []
|
||||
sections[tag.id].concat(sections_by_tag)
|
||||
sections[tag.id] = merge_section_ranges(sections[tag.id])
|
||||
sections.delete(tag.id) if sections[tag.id] == [[0, nil]]
|
||||
end
|
||||
end
|
||||
|
||||
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme)
|
||||
tags << Tag.no_deerjikist if with_no_deerjikist && tags.all? { |t| !(t.deerjikist?) }
|
||||
tags.uniq(&:id)
|
||||
tags.uniq!(&:id)
|
||||
|
||||
if with_sections
|
||||
{ tags:, sections: }
|
||||
else
|
||||
tags
|
||||
end
|
||||
end
|
||||
|
||||
def self.expand_parent_tags tags
|
||||
@@ -150,6 +194,45 @@ class Tag < ApplicationRecord
|
||||
(result + tags).uniq { |t| t.id }
|
||||
end
|
||||
|
||||
def self.normalise_section_range! begin_raw:, end_raw:, tag_name:
|
||||
begin_ms = begin_raw.empty? ? 0 : time_to_ms!(begin_raw, tag_name:)
|
||||
end_ms = end_raw.empty? ? nil : time_to_ms!(end_raw, tag_name:)
|
||||
|
||||
if end_ms
|
||||
begin_ms, end_ms = end_ms, begin_ms if begin_ms > end_ms
|
||||
end_ms = begin_ms + 1 if begin_ms == end_ms
|
||||
end
|
||||
|
||||
[begin_ms, end_ms]
|
||||
end
|
||||
|
||||
def self.merge_section_ranges ranges
|
||||
sorted_ranges = ranges.sort_by { |begin_ms, end_ms| [begin_ms, end_ms || Float::INFINITY] }
|
||||
merged = []
|
||||
|
||||
sorted_ranges.each do |begin_ms, end_ms|
|
||||
if merged.empty?
|
||||
merged << [begin_ms, end_ms]
|
||||
next
|
||||
end
|
||||
|
||||
last_begin_ms, last_end_ms = merged[-1]
|
||||
if last_end_ms.nil? || begin_ms <= last_end_ms
|
||||
merged[-1] = [last_begin_ms, merge_section_end(last_end_ms, end_ms)]
|
||||
else
|
||||
merged << [begin_ms, end_ms]
|
||||
end
|
||||
end
|
||||
|
||||
merged
|
||||
end
|
||||
|
||||
def self.merge_section_end left_end_ms, right_end_ms
|
||||
return nil if left_end_ms.nil? || right_end_ms.nil?
|
||||
|
||||
[left_end_ms, right_end_ms].max
|
||||
end
|
||||
|
||||
def self.find_or_create_by_tag_name! name, category:
|
||||
tn = TagName.find_undiscard_or_create_by!(name: name.to_s.strip)
|
||||
tn = tn.canonical if tn.canonical_id?
|
||||
@@ -246,6 +329,32 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.time_to_ms! str, tag_name:
|
||||
match =
|
||||
case str
|
||||
when /\A(?<seconds>\d+)(?:\.(?<ms>\d{1,3}))?\z/
|
||||
{ hours: nil, minutes: nil, seconds: Regexp.last_match[:seconds],
|
||||
ms: Regexp.last_match[:ms] }
|
||||
when /\A(?<minutes>\d+):(?<seconds>[0-5]?\d)(?:\.(?<ms>\d{1,3}))?\z/
|
||||
{ hours: nil, minutes: Regexp.last_match[:minutes],
|
||||
seconds: Regexp.last_match[:seconds],
|
||||
ms: Regexp.last_match[:ms] }
|
||||
when /\A(?<hours>\d+):(?<minutes>[0-5]?\d):(?<seconds>[0-5]?\d)(?:\.(?<ms>\d{1,3}))?\z/
|
||||
{ hours: Regexp.last_match[:hours],
|
||||
minutes: Regexp.last_match[:minutes],
|
||||
seconds: Regexp.last_match[:seconds],
|
||||
ms: Regexp.last_match[:ms] }
|
||||
end
|
||||
|
||||
raise SectionLiteralParseError.new(tag_name, str) unless match
|
||||
|
||||
total_s = match[:seconds].to_i
|
||||
total_s += match[:minutes].to_i * 60 if match[:minutes]
|
||||
total_s += match[:hours].to_i * 3_600 if match[:hours]
|
||||
|
||||
total_s * 1_000 + match[:ms].to_s.ljust(3, '0')[0, 3].to_i
|
||||
end
|
||||
|
||||
def nico_tags_cannot_be_deprecated
|
||||
if nico? && deprecated_at.present?
|
||||
errors.add :deprecated_at, 'ニコタグは廃止できません.'
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
module VersionRecord
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
DEFAULT_EVENT_TYPE_MAP = { create: 'create',
|
||||
update: 'update',
|
||||
discard: 'discard',
|
||||
restore: 'restore' }.freeze
|
||||
|
||||
def readonly? = persisted?
|
||||
|
||||
included do
|
||||
event_type_map = if const_defined?(:EVENT_TYPE_MAP, false)
|
||||
const_get(:EVENT_TYPE_MAP)
|
||||
else
|
||||
DEFAULT_EVENT_TYPE_MAP
|
||||
end
|
||||
|
||||
belongs_to :created_by_user, class_name: 'User', optional: true
|
||||
|
||||
enum :event_type, event_type_map, prefix: true, validate: true
|
||||
enum :event_type, { create: 'create',
|
||||
update: 'update',
|
||||
discard: 'discard',
|
||||
restore: 'restore' }, prefix: true, validate: true
|
||||
|
||||
validates :version_no, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
||||
validates :event_type, presence: true
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
|
||||
module MaterialRepr
|
||||
BASE = { only: [:id, :url, :version_no, :file_suppressed_at,
|
||||
:file_suppression_reason, :created_at, :updated_at],
|
||||
BASE = { only: [:id, :url, :created_at, :updated_at],
|
||||
methods: [:content_type],
|
||||
include: { tag: TagRepr::BASE,
|
||||
created_by_user: UserRepr::BASE,
|
||||
@@ -13,30 +12,13 @@ module MaterialRepr
|
||||
|
||||
def base material, host:
|
||||
material.as_json(BASE).merge(
|
||||
file: if material.file.attached? && !material.file_suppressed?
|
||||
file: if material.file.attached?
|
||||
Rails.application.routes.url_helpers.rails_storage_proxy_url(
|
||||
material.file, host:)
|
||||
end,
|
||||
export_paths: export_paths(material),
|
||||
export_items: export_items(material))
|
||||
end)
|
||||
end
|
||||
|
||||
def many materials, host:
|
||||
materials.map { |m| base(m, host:) }
|
||||
end
|
||||
|
||||
def export_paths material
|
||||
material.material_export_items.each_with_object({ }) do |item, hash|
|
||||
hash[item.profile] = item.enabled ? item.export_path : ''
|
||||
end
|
||||
end
|
||||
|
||||
def export_items material
|
||||
material.material_export_items.map do |item|
|
||||
{ id: item.id,
|
||||
profile: item.profile,
|
||||
export_path: item.export_path,
|
||||
enabled: item.enabled }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ module PostRepr
|
||||
:url,
|
||||
:title,
|
||||
:thumbnail_base,
|
||||
:video_ms,
|
||||
:original_created_from,
|
||||
:original_created_before,
|
||||
:created_at,
|
||||
@@ -18,7 +19,7 @@ module PostRepr
|
||||
|
||||
def base post, current_user = nil
|
||||
json = common(post)
|
||||
json['tags'] = tag_json(post.tags)
|
||||
json['tags'] = tag_json(post)
|
||||
json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user)
|
||||
json['viewed'] = current_user ? current_user.viewed?(post) : false
|
||||
json
|
||||
@@ -52,8 +53,16 @@ module PostRepr
|
||||
.merge('thumbnail' => thumbnail_url(post))
|
||||
end
|
||||
|
||||
def tag_json tags
|
||||
tags.reject(&:deprecated?).map { |tag| TagRepr.inline(tag) }
|
||||
def tag_json post
|
||||
post
|
||||
.active_post_tags
|
||||
.reject { _1.tag.deprecated? }
|
||||
.sort_by { _1.tag.name }
|
||||
.map { |post_tag|
|
||||
TagRepr.inline(post_tag.tag).merge(
|
||||
'children' => [],
|
||||
'sections' => post_tag.sections.as_json(only: [:begin_ms, :end_ms]))
|
||||
}
|
||||
end
|
||||
|
||||
def thumbnail_url post
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
require 'digest'
|
||||
|
||||
class MaterialFileSha256
|
||||
def self.from_blob blob, allow_download: false
|
||||
sha256 = blob.metadata['sha256']
|
||||
return sha256 if sha256.present?
|
||||
return nil unless allow_download
|
||||
|
||||
begin
|
||||
blob.open do |file|
|
||||
sha256 = Digest::SHA256.file(file.path).hexdigest
|
||||
blob.metadata['sha256'] = sha256
|
||||
blob.save! if blob.changed?
|
||||
sha256
|
||||
end
|
||||
rescue ActiveStorage::FileNotFoundError, ArgumentError => error
|
||||
Rails.logger.warn(
|
||||
"MaterialFileSha256.from_blob failed for blob_id=#{blob.id}: " \
|
||||
"#{error.class}: #{error.message}",
|
||||
)
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.from_upload upload
|
||||
tempfile = upload&.tempfile
|
||||
return nil unless tempfile
|
||||
|
||||
tempfile.rewind
|
||||
Digest::SHA256.file(tempfile.path).hexdigest.tap do
|
||||
tempfile.rewind
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
class MaterialImportBlockMatcher
|
||||
def self.match_for_sha256 sha256
|
||||
return nil if sha256.blank?
|
||||
|
||||
MaterialImportBlock.find_by(match_kind: 'sha256', sha256:)
|
||||
end
|
||||
end
|
||||
@@ -1,70 +0,0 @@
|
||||
class MaterialVersionRecorder < VersionRecorder
|
||||
EVENT_TYPES = ['create', 'update', 'discard', 'restore', 'suppress'].freeze
|
||||
|
||||
def self.record! material:, event_type:, created_by_user:, file_snapshot: nil
|
||||
new(material:, event_type:, created_by_user:, file_snapshot:).record!
|
||||
end
|
||||
|
||||
def initialize material:, event_type:, created_by_user:, file_snapshot: nil
|
||||
@file_snapshot = file_snapshot
|
||||
|
||||
super(record: material, event_type:, created_by_user:)
|
||||
end
|
||||
|
||||
def self.ensure_snapshot! material, created_by_user:
|
||||
return if material.material_versions.exists?
|
||||
|
||||
record!(material:, event_type: :create,
|
||||
created_by_user: material.created_by_user || created_by_user)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def version_class = MaterialVersion
|
||||
def version_association = :material_versions
|
||||
def record_key = :material
|
||||
|
||||
def snapshot_attributes
|
||||
blob = @record.file.attached? ? @record.file.blob : nil
|
||||
file_snapshot = build_file_snapshot(blob)
|
||||
|
||||
{ url: @record.url,
|
||||
parent: @record.parent,
|
||||
tag: @record.tag,
|
||||
tag_name: @record.tag&.name,
|
||||
tag_category: @record.tag&.category,
|
||||
export_paths_json: @record.snapshot_export_paths,
|
||||
discarded_at: @record.discarded_at,
|
||||
file_blob_id: file_snapshot[:file_blob_id],
|
||||
file_filename: file_snapshot[:file_filename],
|
||||
file_content_type: file_snapshot[:file_content_type],
|
||||
file_byte_size: file_snapshot[:file_byte_size],
|
||||
file_checksum: file_snapshot[:file_checksum],
|
||||
file_sha256: file_snapshot[:file_sha256],
|
||||
file_suppressed_at: @record.file_suppressed_at,
|
||||
file_suppression_reason: @record.file_suppression_reason }
|
||||
end
|
||||
|
||||
def build_file_snapshot blob
|
||||
return @file_snapshot if @file_snapshot
|
||||
return empty_file_snapshot unless blob
|
||||
|
||||
{ file_blob_id: blob.id,
|
||||
file_filename: blob.filename.to_s,
|
||||
file_content_type: blob.content_type,
|
||||
file_byte_size: blob.byte_size,
|
||||
file_checksum: blob.checksum,
|
||||
file_sha256: blob.metadata['sha256'] }
|
||||
end
|
||||
|
||||
def empty_file_snapshot
|
||||
{ file_blob_id: nil,
|
||||
file_filename: nil,
|
||||
file_content_type: nil,
|
||||
file_byte_size: nil,
|
||||
file_checksum: nil,
|
||||
file_sha256: nil }
|
||||
end
|
||||
|
||||
def event_types = self.class::EVENT_TYPES
|
||||
end
|
||||
@@ -1,149 +0,0 @@
|
||||
require 'stringio'
|
||||
require 'zlib'
|
||||
|
||||
# Initial implementation keeps every file payload and the final ZIP in memory.
|
||||
# Keep this service boundary stable so job/cached export paths can replace it later.
|
||||
class MaterialZipExporter
|
||||
Entry = Struct.new(:path, :data, :mtime, keyword_init: true)
|
||||
MissingFile = Struct.new(:material_id, :export_path, :blob_id, :filename, keyword_init: true)
|
||||
|
||||
class EmptyExportError < StandardError; end
|
||||
class DuplicatePathError < StandardError; end
|
||||
class MissingFileError < StandardError
|
||||
attr_reader :missing_files
|
||||
|
||||
def initialize missing_files
|
||||
@missing_files = missing_files
|
||||
|
||||
super("Missing files: #{missing_files.map(&:export_path).join(', ')}")
|
||||
end
|
||||
end
|
||||
|
||||
def initialize profile: 'legacy_drive', tag_id: nil
|
||||
@profile = profile.presence || 'legacy_drive'
|
||||
@tag_id = tag_id.presence
|
||||
end
|
||||
|
||||
def export
|
||||
entries = build_entries
|
||||
raise EmptyExportError if entries.empty?
|
||||
|
||||
ZipWriter.write(entries)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_entries
|
||||
rows = MaterialExportItem
|
||||
.enabled
|
||||
.includes(material: { file_attachment: :blob })
|
||||
.joins(:material)
|
||||
.merge(Material.kept)
|
||||
.where(profile: @profile)
|
||||
.where(materials: { file_suppressed_at: nil })
|
||||
.order(:export_path)
|
||||
|
||||
rows = rows.where(materials: { tag_id: @tag_id }) if @tag_id
|
||||
|
||||
missing_files = []
|
||||
|
||||
entries = rows.filter_map do |item|
|
||||
material = item.material
|
||||
next unless material.file.attached?
|
||||
|
||||
data = download_blob(item, missing_files)
|
||||
next unless data
|
||||
|
||||
Entry.new(path: item.export_path,
|
||||
data:,
|
||||
mtime: material.updated_at || Time.current)
|
||||
end
|
||||
|
||||
raise MissingFileError.new(missing_files) if missing_files.any?
|
||||
|
||||
paths = entries.map(&:path)
|
||||
duplicated = paths.find { |path| paths.count(path) > 1 }
|
||||
raise DuplicatePathError, duplicated if duplicated
|
||||
|
||||
entries
|
||||
end
|
||||
|
||||
def download_blob item, missing_files
|
||||
blob = item.material.file.blob
|
||||
|
||||
blob.download
|
||||
rescue ActiveStorage::FileNotFoundError
|
||||
missing_files << MissingFile.new(
|
||||
material_id: item.material_id,
|
||||
export_path: item.export_path,
|
||||
blob_id: blob.id,
|
||||
filename: blob.filename.to_s,
|
||||
)
|
||||
nil
|
||||
end
|
||||
|
||||
class ZipWriter
|
||||
VERSION_NEEDED = 20
|
||||
GP_FLAG = 0x0800
|
||||
COMPRESSION_STORE = 0
|
||||
|
||||
def self.write entries
|
||||
new(entries).write
|
||||
end
|
||||
|
||||
def initialize entries
|
||||
@entries = entries
|
||||
@central_directory = []
|
||||
end
|
||||
|
||||
def write
|
||||
io = StringIO.new(''.b)
|
||||
|
||||
@entries.each do |entry|
|
||||
write_entry(io, entry)
|
||||
end
|
||||
|
||||
central_start = io.pos
|
||||
@central_directory.each { |header| io.write(header) }
|
||||
central_size = io.pos - central_start
|
||||
|
||||
io.write([0x06054b50, 0, 0, @entries.size, @entries.size,
|
||||
central_size, central_start, 0].pack('VvvvvVVv'))
|
||||
io.string
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write_entry io, entry
|
||||
path = entry.path.b
|
||||
data = entry.data.b
|
||||
crc32 = Zlib.crc32(data)
|
||||
dos_time, dos_date = dos_timestamp(entry.mtime)
|
||||
offset = io.pos
|
||||
|
||||
local_header = [0x04034b50, VERSION_NEEDED, GP_FLAG, COMPRESSION_STORE,
|
||||
dos_time, dos_date, crc32, data.bytesize, data.bytesize,
|
||||
path.bytesize, 0].pack('VvvvvvVVVvv')
|
||||
|
||||
io.write(local_header)
|
||||
io.write(path)
|
||||
io.write(data)
|
||||
|
||||
@central_directory << central_header(path:, crc32:, size: data.bytesize,
|
||||
dos_time:, dos_date:, offset:)
|
||||
end
|
||||
|
||||
def central_header path:, crc32:, size:, dos_time:, dos_date:, offset:
|
||||
[0x02014b50, VERSION_NEEDED, VERSION_NEEDED, GP_FLAG, COMPRESSION_STORE,
|
||||
dos_time, dos_date, crc32, size, size, path.bytesize, 0, 0, 0, 0, 0,
|
||||
offset].pack('VvvvvvvVVVvvvvvVV') + path
|
||||
end
|
||||
|
||||
def dos_timestamp time
|
||||
local = time.to_time
|
||||
dos_time = (local.hour << 11) | (local.min << 5) | (local.sec / 2)
|
||||
dos_date = ((local.year - 1980) << 9) | (local.month << 5) | local.day
|
||||
[dos_time, dos_date]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -23,6 +23,7 @@ class PostVersionRecorder < VersionRecorder
|
||||
{ title: @record.title,
|
||||
url: @record.url,
|
||||
thumbnail_base: @record.thumbnail_base,
|
||||
video_ms: @record.video_ms,
|
||||
tags: @record.snapshot_tag_names.join(' '),
|
||||
parent_post_ids: @record.snapshot_parent_post_ids.join(' '),
|
||||
original_created_from: @record.original_created_from,
|
||||
|
||||
@@ -73,7 +73,7 @@ class VersionRecorder
|
||||
end
|
||||
|
||||
def validate_event_type!
|
||||
return if event_types.include?(@event_type)
|
||||
return if EVENT_TYPES.include?(@event_type)
|
||||
|
||||
raise ArgumentError, "Invalid event_type: #{ @event_type }"
|
||||
end
|
||||
@@ -84,5 +84,4 @@ class VersionRecorder
|
||||
def snapshot_attributes = raise NotImplementedError
|
||||
|
||||
def record_class = @record.class
|
||||
def event_types = self.class::EVENT_TYPES
|
||||
end
|
||||
|
||||
@@ -113,10 +113,5 @@ Rails.application.routes.draw do
|
||||
resources :skip_events, controller: :theatre_skip_events, only: [:index]
|
||||
end
|
||||
|
||||
get 'materials/download.zip', to: 'materials#download'
|
||||
resources :materials, only: [:index, :show, :create, :update, :destroy] do
|
||||
member do
|
||||
patch :suppress_file
|
||||
end
|
||||
end
|
||||
resources :materials, only: [:index, :show, :create, :update, :destroy]
|
||||
end
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class AddVideoMsToPosts < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :posts, :video_ms, :integer
|
||||
add_index :posts, [:video_ms, :id], name: 'idx_posts_video_ms_id'
|
||||
|
||||
add_check_constraint :posts, 'video_ms IS NULL OR video_ms > 0',
|
||||
name: 'chk_posts_video_ms_positive'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
class CreatePostTagSections < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :post_tag_sections, primary_key: [:post_id, :tag_id, :begin_ms] do |t|
|
||||
t.references :post, null: false, foreign_key: true, index: false
|
||||
t.references :tag, null: false, foreign_key: true, index: false
|
||||
t.integer :begin_ms, null: false
|
||||
t.integer :end_ms, null: true
|
||||
t.timestamps
|
||||
|
||||
t.index [:post_id, :begin_ms], name: 'idx_post_tag_sections_post_id_begin_ms'
|
||||
|
||||
t.check_constraint 'begin_ms >= 0',
|
||||
name: 'chk_post_tag_sections_begin_ms_natural'
|
||||
|
||||
t.check_constraint 'end_ms IS NULL OR begin_ms < end_ms',
|
||||
name: 'chk_post_tag_sections_end_ms_after_begin_ms'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
class AddVideoMsToPostVersions < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :post_versions, :video_ms, :integer
|
||||
add_index :post_versions, [:video_ms, :post_id], name: 'idx_post_versions_video_ms_post_id'
|
||||
|
||||
add_check_constraint :post_versions, 'video_ms IS NULL OR video_ms > 0',
|
||||
name: 'chk_post_versions_video_ms_positive'
|
||||
end
|
||||
end
|
||||
@@ -1,98 +0,0 @@
|
||||
class EnhanceMaterialManagement < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
change_table :materials, bulk: true do |t|
|
||||
t.integer :version_no, null: false, default: 1
|
||||
t.datetime :file_suppressed_at
|
||||
t.references :file_suppressed_by_user, foreign_key: { to_table: :users }
|
||||
t.string :file_suppression_reason
|
||||
end
|
||||
|
||||
change_table :material_versions, bulk: true do |t|
|
||||
t.string :event_type
|
||||
t.string :tag_name
|
||||
t.string :tag_category
|
||||
t.json :export_paths_json
|
||||
t.bigint :file_blob_id
|
||||
t.string :file_filename
|
||||
t.string :file_content_type
|
||||
t.bigint :file_byte_size
|
||||
t.string :file_checksum
|
||||
t.string :file_sha256
|
||||
t.datetime :file_suppressed_at
|
||||
t.string :file_suppression_reason
|
||||
end
|
||||
|
||||
execute <<~SQL.squish
|
||||
UPDATE material_versions
|
||||
SET event_type = CASE
|
||||
WHEN version_no = 1 THEN 'create'
|
||||
ELSE 'update'
|
||||
END
|
||||
WHERE event_type IS NULL
|
||||
SQL
|
||||
|
||||
change_column_null :material_versions, :event_type, false
|
||||
add_index :material_versions, :file_blob_id
|
||||
|
||||
add_check_constraint :material_versions,
|
||||
"event_type IN ('create', 'update', 'discard', 'restore', 'suppress')",
|
||||
name: 'material_versions_event_type_valid'
|
||||
|
||||
create_table :material_export_items do |t|
|
||||
t.references :material, null: false, foreign_key: true
|
||||
t.string :profile, null: false, default: 'legacy_drive'
|
||||
t.string :export_path, null: false
|
||||
t.boolean :enabled, null: false, default: true
|
||||
t.references :created_by_user, foreign_key: { to_table: :users }
|
||||
t.timestamps
|
||||
|
||||
t.index [:profile, :export_path], unique: true
|
||||
t.index [:material_id, :profile], unique: true
|
||||
end
|
||||
|
||||
create_table :material_import_blocks do |t|
|
||||
t.string :match_kind, null: false
|
||||
t.string :sha256
|
||||
t.string :external_path_pattern
|
||||
t.string :reason, null: false
|
||||
t.text :note
|
||||
t.references :created_by_user, foreign_key: { to_table: :users }
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
execute <<~SQL.squish
|
||||
UPDATE materials
|
||||
SET version_no = COALESCE(
|
||||
(SELECT MAX(material_versions.version_no)
|
||||
FROM material_versions
|
||||
WHERE material_versions.material_id = materials.id),
|
||||
1)
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :material_import_blocks
|
||||
drop_table :material_export_items
|
||||
|
||||
remove_check_constraint :material_versions, name: 'material_versions_event_type_valid'
|
||||
|
||||
remove_index :material_versions, :file_blob_id
|
||||
remove_column :material_versions, :event_type
|
||||
remove_column :material_versions, :tag_name
|
||||
remove_column :material_versions, :tag_category
|
||||
remove_column :material_versions, :export_paths_json
|
||||
remove_column :material_versions, :file_blob_id
|
||||
remove_column :material_versions, :file_filename
|
||||
remove_column :material_versions, :file_content_type
|
||||
remove_column :material_versions, :file_byte_size
|
||||
remove_column :material_versions, :file_checksum
|
||||
remove_column :material_versions, :file_sha256
|
||||
remove_column :material_versions, :file_suppressed_at
|
||||
remove_column :material_versions, :file_suppression_reason
|
||||
|
||||
remove_reference :materials, :file_suppressed_by_user, foreign_key: { to_table: :users }
|
||||
remove_column :materials, :version_no
|
||||
remove_column :materials, :file_suppressed_at
|
||||
remove_column :materials, :file_suppression_reason
|
||||
end
|
||||
end
|
||||
生成ファイル
+22
-50
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_06_22_020000) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -130,32 +130,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.index ["ip_address"], name: "index_ip_addresses_on_ip_address", unique: true
|
||||
end
|
||||
|
||||
create_table "material_export_items", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "material_id", null: false
|
||||
t.string "profile", default: "legacy_drive", null: false
|
||||
t.string "export_path", null: false
|
||||
t.boolean "enabled", default: true, null: false
|
||||
t.bigint "created_by_user_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["created_by_user_id"], name: "index_material_export_items_on_created_by_user_id"
|
||||
t.index ["material_id", "profile"], name: "index_material_export_items_on_material_id_and_profile", unique: true
|
||||
t.index ["material_id"], name: "index_material_export_items_on_material_id"
|
||||
t.index ["profile", "export_path"], name: "index_material_export_items_on_profile_and_export_path", unique: true
|
||||
end
|
||||
|
||||
create_table "material_import_blocks", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "match_kind", null: false
|
||||
t.string "sha256"
|
||||
t.string "external_path_pattern"
|
||||
t.string "reason", null: false
|
||||
t.text "note"
|
||||
t.bigint "created_by_user_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["created_by_user_id"], name: "index_material_import_blocks_on_created_by_user_id"
|
||||
end
|
||||
|
||||
create_table "material_versions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "material_id", null: false
|
||||
t.integer "version_no", null: false
|
||||
@@ -167,28 +141,14 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "discarded_at"
|
||||
t.string "event_type", null: false
|
||||
t.string "tag_name"
|
||||
t.string "tag_category"
|
||||
t.json "export_paths_json"
|
||||
t.bigint "file_blob_id"
|
||||
t.string "file_filename"
|
||||
t.string "file_content_type"
|
||||
t.bigint "file_byte_size"
|
||||
t.string "file_checksum"
|
||||
t.string "file_sha256"
|
||||
t.datetime "file_suppressed_at"
|
||||
t.string "file_suppression_reason"
|
||||
t.index ["created_by_user_id"], name: "index_material_versions_on_created_by_user_id"
|
||||
t.index ["discarded_at"], name: "index_material_versions_on_discarded_at"
|
||||
t.index ["file_blob_id"], name: "index_material_versions_on_file_blob_id"
|
||||
t.index ["material_id", "version_no"], name: "index_material_versions_on_material_id_and_version_no", unique: true
|
||||
t.index ["material_id"], name: "index_material_versions_on_material_id"
|
||||
t.index ["parent_id"], name: "index_material_versions_on_parent_id"
|
||||
t.index ["tag_id"], name: "index_material_versions_on_tag_id"
|
||||
t.index ["updated_by_user_id"], name: "index_material_versions_on_updated_by_user_id"
|
||||
t.index ["url"], name: "index_material_versions_on_url"
|
||||
t.check_constraint "`event_type` in (_utf8mb4'create',_utf8mb4'update',_utf8mb4'discard',_utf8mb4'restore',_utf8mb4'suppress')", name: "material_versions_event_type_valid"
|
||||
end
|
||||
|
||||
create_table "materials", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
@@ -201,14 +161,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "discarded_at"
|
||||
t.virtual "active_url", type: :string, as: "if((`discarded_at` is null),`url`,NULL)"
|
||||
t.integer "version_no", default: 1, null: false
|
||||
t.datetime "file_suppressed_at"
|
||||
t.bigint "file_suppressed_by_user_id"
|
||||
t.string "file_suppression_reason"
|
||||
t.index ["active_url"], name: "index_materials_on_active_url", unique: true
|
||||
t.index ["created_by_user_id"], name: "index_materials_on_created_by_user_id"
|
||||
t.index ["discarded_at"], name: "index_materials_on_discarded_at"
|
||||
t.index ["file_suppressed_by_user_id"], name: "index_materials_on_file_suppressed_by_user_id"
|
||||
t.index ["parent_id"], name: "index_materials_on_parent_id"
|
||||
t.index ["tag_id"], name: "index_materials_on_tag_id"
|
||||
t.index ["updated_by_user_id"], name: "index_materials_on_updated_by_user_id"
|
||||
@@ -255,6 +210,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.index ["target_post_id"], name: "index_post_similarities_on_target_post_id"
|
||||
end
|
||||
|
||||
create_table "post_tag_sections", primary_key: ["post_id", "tag_id", "begin_ms"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "post_id", null: false
|
||||
t.bigint "tag_id", null: false
|
||||
t.integer "begin_ms", null: false
|
||||
t.integer "end_ms"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["post_id", "begin_ms"], name: "idx_post_tag_sections_post_id_begin_ms"
|
||||
t.index ["tag_id"], name: "fk_rails_8be3847903"
|
||||
t.check_constraint "(`end_ms` is null) or (`begin_ms` < `end_ms`)", name: "chk_post_tag_sections_end_ms_after_begin_ms"
|
||||
t.check_constraint "`begin_ms` >= 0", name: "chk_post_tag_sections_begin_ms_natural"
|
||||
end
|
||||
|
||||
create_table "post_tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "post_id", null: false
|
||||
t.bigint "tag_id", null: false
|
||||
@@ -288,9 +256,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.datetime "original_created_before"
|
||||
t.datetime "created_at", null: false
|
||||
t.bigint "created_by_user_id"
|
||||
t.integer "video_ms"
|
||||
t.index ["created_by_user_id"], name: "index_post_versions_on_created_by_user_id"
|
||||
t.index ["post_id", "version_no"], name: "index_post_versions_on_post_id_and_version_no", unique: true
|
||||
t.index ["post_id"], name: "index_post_versions_on_post_id"
|
||||
t.index ["video_ms", "post_id"], name: "idx_post_versions_video_ms_post_id"
|
||||
t.check_constraint "(`video_ms` is null) or (`video_ms` > 0)", name: "chk_post_versions_video_ms_positive"
|
||||
t.check_constraint "`event_type` in (_utf8mb4'create',_utf8mb4'update',_utf8mb4'discard',_utf8mb4'restore')", name: "post_versions_event_type_valid"
|
||||
t.check_constraint "`version_no` > 0", name: "post_versions_version_no_positive"
|
||||
end
|
||||
@@ -305,8 +276,11 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
t.datetime "original_created_before"
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "version_no", null: false
|
||||
t.integer "video_ms"
|
||||
t.index ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id"
|
||||
t.index ["url"], name: "index_posts_on_url", unique: true
|
||||
t.index ["video_ms", "id"], name: "idx_posts_video_ms_id"
|
||||
t.check_constraint "(`video_ms` is null) or (`video_ms` > 0)", name: "chk_posts_video_ms_positive"
|
||||
t.check_constraint "`version_no` > 0", name: "chk_posts_version_no_positive"
|
||||
end
|
||||
|
||||
@@ -612,9 +586,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
add_foreign_key "gekanator_question_suggestions", "users"
|
||||
add_foreign_key "gekanator_questions", "gekanator_question_suggestions"
|
||||
add_foreign_key "gekanator_questions", "users", column: "created_by_id"
|
||||
add_foreign_key "material_export_items", "materials"
|
||||
add_foreign_key "material_export_items", "users", column: "created_by_user_id"
|
||||
add_foreign_key "material_import_blocks", "users", column: "created_by_user_id"
|
||||
add_foreign_key "material_versions", "materials"
|
||||
add_foreign_key "material_versions", "materials", column: "parent_id"
|
||||
add_foreign_key "material_versions", "tags"
|
||||
@@ -623,7 +594,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
add_foreign_key "materials", "materials", column: "parent_id"
|
||||
add_foreign_key "materials", "tags"
|
||||
add_foreign_key "materials", "users", column: "created_by_user_id"
|
||||
add_foreign_key "materials", "users", column: "file_suppressed_by_user_id"
|
||||
add_foreign_key "materials", "users", column: "updated_by_user_id"
|
||||
add_foreign_key "nico_tag_relations", "tags"
|
||||
add_foreign_key "nico_tag_relations", "tags", column: "nico_tag_id"
|
||||
@@ -633,6 +603,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_06_23_000000) do
|
||||
add_foreign_key "post_implications", "posts", column: "parent_post_id"
|
||||
add_foreign_key "post_similarities", "posts"
|
||||
add_foreign_key "post_similarities", "posts", column: "target_post_id"
|
||||
add_foreign_key "post_tag_sections", "posts"
|
||||
add_foreign_key "post_tag_sections", "tags"
|
||||
add_foreign_key "post_tags", "posts"
|
||||
add_foreign_key "post_tags", "tags"
|
||||
add_foreign_key "post_tags", "users", column: "created_user_id"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :post_tag_section do
|
||||
association :post
|
||||
association :tag
|
||||
begin_ms { 1_000 }
|
||||
end_ms { 2_000 }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
FactoryBot.define do
|
||||
factory :post_tag do
|
||||
association :post
|
||||
association :tag
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :post do
|
||||
sequence(:url) { |n| "https://example.com/factory-post-#{ n }" }
|
||||
title { 'factory post' }
|
||||
thumbnail_base { nil }
|
||||
uploaded_user { nil }
|
||||
end
|
||||
end
|
||||
@@ -1,57 +0,0 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MaterialExportItem, type: :model do
|
||||
let(:user) { create(:user, :member) }
|
||||
let(:tag) { Tag.create!(tag_name: TagName.create!(name: 'export_item'), category: :material) }
|
||||
let(:material) do
|
||||
Material.create!(tag:, url: 'https://example.com/material',
|
||||
created_by_user: user, updated_by_user: user)
|
||||
end
|
||||
|
||||
it 'rejects blank export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive', export_path: '')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
|
||||
it 'rejects absolute export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive',
|
||||
export_path: '/素材/a.png')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
|
||||
it 'rejects parent traversal export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive',
|
||||
export_path: '素材/../a.png')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
|
||||
it 'rejects double slash export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive',
|
||||
export_path: '素材//a.png')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
|
||||
it 'rejects dot segment export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive',
|
||||
export_path: './素材/a.png')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
|
||||
it 'rejects trailing slash export_path' do
|
||||
item = described_class.new(material:, profile: 'legacy_drive',
|
||||
export_path: '素材/a/')
|
||||
|
||||
expect(item).not_to be_valid
|
||||
expect(item.errors[:export_path]).to be_present
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
RSpec.describe PostTag, type: :model do
|
||||
describe '#sections' do
|
||||
it 'loads sections by post_id and tag_id' do
|
||||
post_tag = create(:post_tag)
|
||||
section = create(:post_tag_section,
|
||||
post: post_tag.post,
|
||||
tag: post_tag.tag,
|
||||
begin_ms: 1000,
|
||||
end_ms: 2000)
|
||||
|
||||
expect(post_tag.sections).to contain_exactly(section)
|
||||
end
|
||||
|
||||
it 'does not load sections for another tag on the same post' do
|
||||
post = create(:post)
|
||||
tag = create(:tag)
|
||||
other_tag = create(:tag)
|
||||
|
||||
post_tag = create(:post_tag, post:, tag:)
|
||||
create(:post_tag_section,
|
||||
post:,
|
||||
tag: other_tag,
|
||||
begin_ms: 1000,
|
||||
end_ms: 2000)
|
||||
|
||||
expect(post_tag.sections).to be_empty
|
||||
end
|
||||
|
||||
it 'allows open-ended sections' do
|
||||
post_tag = create(:post_tag)
|
||||
section = create(:post_tag_section,
|
||||
post: post_tag.post,
|
||||
tag: post_tag.tag,
|
||||
begin_ms: 1000,
|
||||
end_ms: nil)
|
||||
|
||||
expect(section).to be_valid
|
||||
expect(post_tag.sections).to contain_exactly(section)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -19,6 +19,95 @@ RSpec.describe Tag, type: :model do
|
||||
expect(error.tag_names).to eq([deprecated_tag.name])
|
||||
}
|
||||
end
|
||||
|
||||
it 'rejects invalid section literals instead of treating them as zero' do
|
||||
expect {
|
||||
described_class.normalise_tags!(
|
||||
['normalise_invalid_section[1:aa-2:00]'],
|
||||
with_sections: true
|
||||
)
|
||||
}.to raise_error(Tag::SectionLiteralParseError)
|
||||
end
|
||||
|
||||
it 'parses open-ended section literals' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[1:00-]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[60_000, nil]])
|
||||
end
|
||||
|
||||
it 'parses omitted begin as zero' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[-1:00]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[0, 60_000]])
|
||||
end
|
||||
|
||||
it 'treats fully open section literals as plain tags' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[-]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections)[tag.id]).to be_nil
|
||||
end
|
||||
|
||||
it 'treats [0:00-] as a plain tag' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[0:00-]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections)[tag.id]).to be_nil
|
||||
end
|
||||
|
||||
it 'expands zero-width sections to one millisecond' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[1:00-1:00]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[60_000, 60_001]])
|
||||
end
|
||||
|
||||
it 'swaps reversed section boundaries' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[2:00-1:00]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[60_000, 120_000]])
|
||||
end
|
||||
|
||||
it 'merges open-ended sections over later bounded sections' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[1:00-][2:00-3:00]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[60_000, nil]])
|
||||
end
|
||||
|
||||
it 'merges adjacent bounded and open-ended sections' do
|
||||
result = described_class.normalise_tags!(
|
||||
['伊地知ニジカ[1:00-3:00][3:00-]'],
|
||||
with_sections: true
|
||||
)
|
||||
|
||||
tag = result.fetch(:tags).find { _1.name == '伊地知ニジカ' }
|
||||
expect(result.fetch(:sections).fetch(tag.id)).to eq([[60_000, nil]])
|
||||
end
|
||||
end
|
||||
|
||||
describe '.expand_parent_tags' do
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Materials API', type: :request do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
let!(:member_user) { create(:user, :member) }
|
||||
let!(:admin_user) { create(:user, :admin) }
|
||||
let!(:guest_user) { create(:user) }
|
||||
|
||||
def dummy_upload(filename: 'dummy.png', type: 'image/png', body: 'dummy')
|
||||
@@ -16,29 +13,22 @@ RSpec.describe 'Materials API', type: :request do
|
||||
end
|
||||
|
||||
def build_material(tag:, user:, parent: nil, file: dummy_upload, url: nil)
|
||||
Material.new(tag:, parent:, url:,
|
||||
created_by_user: user,
|
||||
updated_by_user: user).tap do |material|
|
||||
Material.new(tag:, parent:, url:, created_by_user: user, updated_by_user: user).tap do |material|
|
||||
material.file.attach(file) if file
|
||||
material.save!
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /materials' do
|
||||
let!(:tag_a) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_index_a'), category: :material)
|
||||
end
|
||||
let!(:tag_b) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_index_b'), category: :material)
|
||||
end
|
||||
let!(:tag_a) { Tag.create!(tag_name: TagName.create!(name: 'material_index_a'), category: :material) }
|
||||
let!(:tag_b) { Tag.create!(tag_name: TagName.create!(name: 'material_index_b'), category: :material) }
|
||||
|
||||
let!(:material_a) do
|
||||
build_material(tag: tag_a, user: member_user, file: dummy_upload(filename: 'a.png'))
|
||||
end
|
||||
|
||||
let!(:material_b) do
|
||||
build_material(tag: tag_b, user: member_user, parent: material_a,
|
||||
file: dummy_upload(filename: 'b.png'))
|
||||
build_material(tag: tag_b, user: member_user, parent: material_a, file: dummy_upload(filename: 'b.png'))
|
||||
end
|
||||
|
||||
before do
|
||||
@@ -107,9 +97,7 @@ RSpec.describe 'Materials API', type: :request do
|
||||
end
|
||||
|
||||
describe 'GET /materials/:id' do
|
||||
let!(:tag) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_show'), category: :material)
|
||||
end
|
||||
let!(:tag) { Tag.create!(tag_name: TagName.create!(name: 'material_show'), category: :material) }
|
||||
let!(:material) do
|
||||
build_material(tag:, user: member_user, file: dummy_upload(filename: 'show.png'))
|
||||
end
|
||||
@@ -150,22 +138,9 @@ RSpec.describe 'Materials API', type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in but not member' do
|
||||
context 'when logged in' do
|
||||
before { sign_in_as(guest_user) }
|
||||
|
||||
it 'returns 403' do
|
||||
post '/materials', params: {
|
||||
tag: 'material_create_guest_forbidden',
|
||||
file: dummy_upload
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when member' do
|
||||
before { sign_in_as(member_user) }
|
||||
|
||||
it 'returns 422 when tag is blank' do
|
||||
post '/materials', params: { tag: ' ', file: dummy_upload }
|
||||
|
||||
@@ -187,49 +162,24 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect do
|
||||
post '/materials', params: {
|
||||
tag: 'material_create_new',
|
||||
file: dummy_upload(filename: 'created.png'),
|
||||
export_paths: { legacy_drive: '伊地知ニジカ/created.png' }
|
||||
file: dummy_upload(filename: 'created.png')
|
||||
}
|
||||
end.to change(Material, :count).by(1)
|
||||
.and change(Tag, :count).by(1)
|
||||
.and change(TagName, :count).by(1)
|
||||
.and change(MaterialVersion, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
material = Material.order(:id).last
|
||||
expect(material.tag.name).to eq('material_create_new')
|
||||
expect(material.tag.category).to eq('material')
|
||||
expect(material.created_by_user).to eq(member_user)
|
||||
expect(material.updated_by_user).to eq(member_user)
|
||||
expect(material.created_by_user).to eq(guest_user)
|
||||
expect(material.updated_by_user).to eq(guest_user)
|
||||
expect(material.file.attached?).to be(true)
|
||||
expect(material.version_no).to eq(1)
|
||||
expect(material.material_versions.first.event_type).to eq('create')
|
||||
expect(material.material_export_items.first.export_path).to eq('伊地知ニジカ/created.png')
|
||||
expect(material.material_versions.first.export_paths_json).to eq(
|
||||
'legacy_drive' => '伊地知ニジカ/created.png'
|
||||
)
|
||||
|
||||
expect(json['id']).to eq(material.id)
|
||||
expect(json.dig('tag', 'name')).to eq('material_create_new')
|
||||
expect(json['content_type']).to eq('image/png')
|
||||
expect(json.dig('export_paths', 'legacy_drive')).to eq('伊地知ニジカ/created.png')
|
||||
end
|
||||
|
||||
it 'snapshots attached file metadata and sha256' do
|
||||
post '/materials', params: {
|
||||
tag: 'material_create_file_version',
|
||||
file: dummy_upload(filename: 'created.png', body: 'sha-body')
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
version = Material.order(:id).last.material_versions.first
|
||||
expect(version.file_blob_id).to be_present
|
||||
expect(version.file_filename).to eq('created.png')
|
||||
expect(version.file_content_type).to eq('image/png')
|
||||
expect(version.file_byte_size).to eq('sha-body'.bytesize)
|
||||
expect(version.file_sha256).to eq(Digest::SHA256.hexdigest('sha-body'))
|
||||
end
|
||||
|
||||
it 'returns 422 when the existing tag is not material/character' do
|
||||
@@ -269,33 +219,11 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json['url']).to eq('https://example.com/material-source')
|
||||
end
|
||||
|
||||
it 'rejects sha256-blocked file upload' do
|
||||
sha256 = Digest::SHA256.hexdigest('blocked-body')
|
||||
MaterialImportBlock.create!(match_kind: 'sha256',
|
||||
sha256:,
|
||||
reason: 'copyright_high_risk',
|
||||
created_by_user: admin_user)
|
||||
|
||||
expect do
|
||||
post '/materials', params: {
|
||||
tag: 'material_blocked_create',
|
||||
file: dummy_upload(filename: 'blocked.png', body: 'blocked-body')
|
||||
}
|
||||
end.not_to change(Material, :count)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'file' => ['抑止された素材です: copyright_high_risk']
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /materials/:id' do
|
||||
let!(:tag) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_update_old'), category: :material)
|
||||
end
|
||||
let!(:tag) { Tag.create!(tag_name: TagName.create!(name: 'material_update_old'), category: :material) }
|
||||
let!(:material) do
|
||||
build_material(tag:, user: member_user, file: dummy_upload(filename: 'old.png'))
|
||||
end
|
||||
@@ -349,26 +277,25 @@ RSpec.describe 'Materials API', type: :request do
|
||||
'tag' => ['タグは必須です.'])
|
||||
end
|
||||
|
||||
it 'keeps the existing file when file and url are omitted' do
|
||||
it 'returns 422 when both file and url are blank' do
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_no_payload'
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(material.reload.file.attached?).to be(true)
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'file' => ['ファイルまたは URL は必須です.'],
|
||||
'url' => ['ファイルまたは URL は必須です.'])
|
||||
end
|
||||
|
||||
it 'updates tag, url, file, and updated_by_user' do
|
||||
old_blob_id = material.file.blob.id
|
||||
|
||||
expect do
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_new',
|
||||
url: 'https://example.com/updated-source',
|
||||
file: dummy_upload(filename: 'updated.jpg', type: 'image/jpeg'),
|
||||
export_paths: { legacy_drive: '伊地知ニジカ/updated.jpg' }
|
||||
}
|
||||
end.to change(MaterialVersion, :count).by(2)
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_new',
|
||||
url: 'https://example.com/updated-source',
|
||||
file: dummy_upload(filename: 'updated.jpg', type: 'image/jpeg')
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
@@ -379,15 +306,8 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect(material.updated_by_user).to eq(member_user)
|
||||
expect(material.file.attached?).to be(true)
|
||||
expect(material.file.blob.id).not_to eq(old_blob_id)
|
||||
expect(ActiveStorage::Blob.where(id: old_blob_id).exists?).to be(true)
|
||||
expect(material.file.blob.filename.to_s).to eq('updated.jpg')
|
||||
expect(material.file.blob.content_type).to eq('image/jpeg')
|
||||
expect(material.version_no).to eq(2)
|
||||
expect(material.material_versions.order(:version_no).last.event_type).to eq('update')
|
||||
expect(material.material_export_items.first.export_path).to eq('伊地知ニジカ/updated.jpg')
|
||||
expect(material.material_versions.order(:version_no).last.export_paths_json).to eq(
|
||||
'legacy_drive' => '伊地知ニジカ/updated.jpg'
|
||||
)
|
||||
|
||||
expect(json['id']).to eq(material.id)
|
||||
expect(json['file']).to be_present
|
||||
@@ -395,7 +315,7 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect(json.dig('tag', 'name')).to eq('material_update_new')
|
||||
end
|
||||
|
||||
it 'detaches the existing file without purging blob when url replaces file' do
|
||||
it 'purges the existing file when file is omitted and url is provided' do
|
||||
old_blob_id = material.file.blob.id
|
||||
|
||||
put "/materials/#{ material.id }", params: {
|
||||
@@ -411,7 +331,9 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect(material.updated_by_user).to eq(member_user)
|
||||
expect(material.file.attached?).to be(false)
|
||||
|
||||
expect(ActiveStorage::Blob.where(id: old_blob_id).exists?).to be(true)
|
||||
expect(
|
||||
ActiveStorage::Blob.where(id: old_blob_id).exists?
|
||||
).to be(false)
|
||||
|
||||
expect(json['id']).to eq(material.id)
|
||||
expect(json['file']).to be_nil
|
||||
@@ -419,190 +341,11 @@ RSpec.describe 'Materials API', type: :request do
|
||||
expect(json.dig('tag', 'name')).to eq('material_update_remove_file')
|
||||
expect(json['url']).to eq('https://example.com/updated-source')
|
||||
end
|
||||
|
||||
it 'does not increase version for the same snapshot update' do
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: member_user)
|
||||
|
||||
expect do
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_old'
|
||||
}
|
||||
end.not_to change(MaterialVersion, :count)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(material.reload.version_no).to eq(1)
|
||||
end
|
||||
|
||||
it 'records update version when only export_path changes' do
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: member_user)
|
||||
|
||||
expect do
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_old',
|
||||
export_paths: { legacy_drive: '素材/only-path.png' }
|
||||
}
|
||||
end.to change(MaterialVersion, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(material.reload.material_export_items.first.export_path).to eq('素材/only-path.png')
|
||||
expect(material.material_versions.order(:version_no).last.export_paths_json).to eq(
|
||||
'legacy_drive' => '素材/only-path.png'
|
||||
)
|
||||
end
|
||||
|
||||
it 'removes export_path item when blank is submitted' do
|
||||
MaterialExportItem.create!(material:, profile: 'legacy_drive',
|
||||
export_path: '素材/remove.png',
|
||||
created_by_user: member_user)
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: member_user)
|
||||
|
||||
expect do
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_old',
|
||||
export_paths: { legacy_drive: '' }
|
||||
}
|
||||
end.to change(MaterialExportItem, :count).by(-1)
|
||||
.and change(MaterialVersion, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(material.reload.material_export_items).to be_empty
|
||||
expect(material.material_versions.order(:version_no).last.export_paths_json).to eq({})
|
||||
end
|
||||
|
||||
it 'rejects sha256-blocked replacement file' do
|
||||
sha256 = Digest::SHA256.hexdigest('blocked-update')
|
||||
MaterialImportBlock.create!(match_kind: 'sha256',
|
||||
sha256:,
|
||||
reason: 'source_owner_request',
|
||||
created_by_user: admin_user)
|
||||
|
||||
put "/materials/#{ material.id }", params: {
|
||||
tag: 'material_update_old',
|
||||
file: dummy_upload(filename: 'blocked.png', body: 'blocked-update')
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(material.reload.file.blob.filename.to_s).to eq('old.png')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /materials/download.zip' do
|
||||
let!(:tag_a) { Tag.create!(tag_name: TagName.create!(name: 'zip_a'), category: :material) }
|
||||
let!(:tag_b) { Tag.create!(tag_name: TagName.create!(name: 'zip_b'), category: :material) }
|
||||
let!(:material_a) do
|
||||
build_material(tag: tag_a, user: member_user,
|
||||
file: dummy_upload(filename: 'a.png', body: 'zip-a'))
|
||||
end
|
||||
let!(:material_b) do
|
||||
build_material(tag: tag_b, user: member_user,
|
||||
file: dummy_upload(filename: 'b.png', body: 'zip-b'))
|
||||
end
|
||||
|
||||
before do
|
||||
MaterialExportItem.create!(material: material_a, profile: 'legacy_drive',
|
||||
export_path: '素材/a.png',
|
||||
created_by_user: member_user)
|
||||
MaterialExportItem.create!(material: material_b, profile: 'legacy_drive',
|
||||
export_path: '素材/b.png',
|
||||
created_by_user: member_user)
|
||||
end
|
||||
|
||||
it 'uses material_export_items.export_path as ZIP entry paths' do
|
||||
get '/materials/download.zip', params: { profile: 'legacy_drive' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(response.media_type).to eq('application/zip')
|
||||
expect(response.body.b).to include('素材/a.png'.b)
|
||||
expect(response.body.b).to include('素材/b.png'.b)
|
||||
end
|
||||
|
||||
it 'filters by tag_id' do
|
||||
get '/materials/download.zip', params: { profile: 'legacy_drive', tag_id: tag_a.id }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(response.body.b).to include('素材/a.png'.b)
|
||||
expect(response.body.b).not_to include('素材/b.png'.b)
|
||||
end
|
||||
|
||||
it 'does not include suppressed materials' do
|
||||
material_b.update!(file_suppressed_at: Time.current,
|
||||
file_suppression_reason: 'copyright_high_risk')
|
||||
|
||||
get '/materials/download.zip', params: { profile: 'legacy_drive' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(response.body.b).to include('素材/a.png'.b)
|
||||
expect(response.body.b).not_to include('素材/b.png'.b)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PATCH /materials/:id/suppress_file' do
|
||||
let!(:tag) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_suppress'), category: :material)
|
||||
end
|
||||
let!(:material) do
|
||||
build_material(tag:, user: member_user, file: dummy_upload(filename: 'suppress.png'))
|
||||
end
|
||||
|
||||
it 'allows admin to suppress a file and records a suppress version' do
|
||||
sign_in_as(admin_user)
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: member_user)
|
||||
|
||||
expect do
|
||||
patch "/materials/#{ material.id }/suppress_file",
|
||||
params: { reason: 'copyright_high_risk' }
|
||||
end.to change(MaterialVersion, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
material.reload
|
||||
expect(material.file_suppressed_at).to be_present
|
||||
expect(material.file_suppressed_by_user).to eq(admin_user)
|
||||
expect(material.file_suppression_reason).to eq('copyright_high_risk')
|
||||
expect(material.material_versions.order(:version_no).last.event_type).to eq('suppress')
|
||||
expect(json['file']).to be_nil
|
||||
expect(json['file_suppressed_at']).to be_present
|
||||
end
|
||||
|
||||
it 'purges blob when purge=true is requested' do
|
||||
sign_in_as(admin_user)
|
||||
old_blob_id = material.file.blob.id
|
||||
MaterialVersionRecorder.record!(material:, event_type: :create,
|
||||
created_by_user: member_user)
|
||||
|
||||
expect do
|
||||
patch "/materials/#{ material.id }/suppress_file",
|
||||
params: { reason: 'copyright_takedown', purge: '1' }
|
||||
end.to have_enqueued_job(ActiveStorage::PurgeJob)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
version = material.material_versions.order(:version_no).last
|
||||
expect(version.event_type).to eq('suppress')
|
||||
expect(version.file_blob_id).to eq(old_blob_id)
|
||||
expect(version.file_filename).to eq('suppress.png')
|
||||
expect(version.file_sha256).to be_present
|
||||
end
|
||||
|
||||
it 'rejects member suppression' do
|
||||
sign_in_as(member_user)
|
||||
|
||||
patch "/materials/#{ material.id }/suppress_file",
|
||||
params: { reason: 'copyright_high_risk' }
|
||||
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /materials/:id' do
|
||||
let!(:tag) do
|
||||
Tag.create!(tag_name: TagName.create!(name: 'material_destroy'), category: :material)
|
||||
end
|
||||
let!(:tag) { Tag.create!(tag_name: TagName.create!(name: 'material_destroy'), category: :material) }
|
||||
let!(:material) do
|
||||
build_material(tag:, user: member_user, file: dummy_upload(filename: 'destroy.png'))
|
||||
end
|
||||
|
||||
@@ -36,6 +36,7 @@ RSpec.describe 'Posts API', type: :request do
|
||||
title: post.title,
|
||||
url: post.url,
|
||||
thumbnail_base: post.thumbnail_base,
|
||||
video_ms: post.video_ms,
|
||||
tags: post.snapshot_tag_names.join(' '),
|
||||
parent_post_ids: post.snapshot_parent_post_ids.join(' '),
|
||||
original_created_from: post.original_created_from,
|
||||
@@ -127,6 +128,22 @@ RSpec.describe 'Posts API', type: :request do
|
||||
expect(all_tag_names).to include("spec_tag")
|
||||
end
|
||||
|
||||
it 'keeps children and sections keys in non-detail tag responses' do
|
||||
PostTagSection.create!(post: hit_post, tag:, begin_ms: 1_000, end_ms: nil)
|
||||
|
||||
get '/posts'
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
hit_json = json.fetch('posts').find { |post| post['id'] == hit_post.id }
|
||||
tag_json = hit_json.fetch('tags').find { |item| item['name'] == 'spec_tag' }
|
||||
|
||||
expect(tag_json.fetch('children')).to eq([])
|
||||
expect(tag_json.fetch('sections')).to eq([
|
||||
{ 'begin_ms' => 1_000, 'end_ms' => nil }
|
||||
])
|
||||
end
|
||||
|
||||
context "when q is provided" do
|
||||
it "filters posts by q (hit case)" do
|
||||
get "/posts", params: { tags: "spec_tag" }
|
||||
@@ -767,6 +784,182 @@ RSpec.describe 'Posts API', type: :request do
|
||||
expect(saved_names).not_to include('deprecated_parent', 'deprecated_grandparent')
|
||||
end
|
||||
|
||||
it 'returns validation error for an invalid section literal' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'invalid section literal',
|
||||
url: 'https://example.com/invalid-section-literal',
|
||||
tags: 'spec_tag[1:aa-2:00]',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json).to include(
|
||||
'type' => 'validation_error',
|
||||
'message' => '入力内容を確認してください.'
|
||||
)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'tags' => ['タグ区間の記法が不正です.']
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates a video post with duration' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'video post',
|
||||
url: 'https://example.com/video-post',
|
||||
tags: '動画 spec_tag',
|
||||
duration: '3:00.500',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(Post.find(json.fetch('id')).video_ms).to eq(180_500)
|
||||
expect(json.fetch('video_ms')).to eq(180_500)
|
||||
end
|
||||
|
||||
it 'clears video_ms when the saved tags do not include 動画' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'non video post',
|
||||
url: 'https://example.com/non-video-post',
|
||||
tags: 'spec_tag',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(Post.find(json.fetch('id')).video_ms).to be_nil
|
||||
end
|
||||
|
||||
it 'returns validation error when a bounded section exceeds duration' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'too long section',
|
||||
url: 'https://example.com/too-long-section',
|
||||
tags: '動画 伊地知ニジカ[2:50-3:10]',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'video_ms' => ['タグ区間の終端が動画時間を超えてゐます.']
|
||||
)
|
||||
end
|
||||
|
||||
it 'saves open-ended sections with end_ms NULL' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'open ended section literal',
|
||||
url: 'https://example.com/open-ended-section-literal',
|
||||
tags: '動画 伊地知ニジカ[1:00-]',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
created_post = Post.find(json.fetch('id'))
|
||||
tag = Tag.joins(:tag_name).find_by!(tag_names: { name: '伊地知ニジカ' })
|
||||
section = PostTagSection.find_by!(post: created_post, tag:)
|
||||
|
||||
expect(section.begin_ms).to eq(60_000)
|
||||
expect(section.end_ms).to be_nil
|
||||
end
|
||||
|
||||
it 'does not save sections for [-]' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'fully open section literal',
|
||||
url: 'https://example.com/fully-open-section-literal',
|
||||
tags: '伊地知ニジカ[-]',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
created_post = Post.find(json.fetch('id'))
|
||||
tag = Tag.joins(:tag_name).find_by!(tag_names: { name: '伊地知ニジカ' })
|
||||
expect(PostTagSection.find_by(post: created_post, tag:)).to be_nil
|
||||
end
|
||||
|
||||
it 'does not save sections for [0:00-]' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'zero open ended section literal',
|
||||
url: 'https://example.com/zero-open-ended-section-literal',
|
||||
tags: '伊地知ニジカ[0:00-]',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
created_post = Post.find(json.fetch('id'))
|
||||
tag = Tag.joins(:tag_name).find_by!(tag_names: { name: '伊地知ニジカ' })
|
||||
expect(PostTagSection.find_by(post: created_post, tag:)).to be_nil
|
||||
end
|
||||
|
||||
it 'returns end_ms null for open-ended sections in show response' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'show open ended section literal',
|
||||
url: 'https://example.com/show-open-ended-section-literal',
|
||||
tags: '動画 伊地知ニジカ[1:00-]',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
get "/posts/#{ json.fetch('id') }"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
tag_json = json.fetch('tags').find { |item| item['name'] == '伊地知ニジカ' }
|
||||
expect(tag_json.fetch('sections')).to eq([
|
||||
{ 'begin_ms' => 60_000, 'end_ms' => nil }
|
||||
])
|
||||
end
|
||||
|
||||
it 'allows open-ended sections when begin is within duration' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'valid open ended section literal',
|
||||
url: 'https://example.com/valid-open-ended-section-literal',
|
||||
tags: '動画 伊地知ニジカ[1:00-]',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
end
|
||||
|
||||
it 'rejects open-ended sections when begin equals duration' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'invalid open ended section literal',
|
||||
url: 'https://example.com/invalid-open-ended-section-literal',
|
||||
tags: '動画 伊地知ニジカ[3:00-]',
|
||||
duration: '3:00',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'video_ms' => ['タグ区間の開始が動画時間以上です.']
|
||||
)
|
||||
end
|
||||
|
||||
context "when nico tag already exists in tags" do
|
||||
before do
|
||||
Tag.find_undiscard_or_create_by!(
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ const RouteTransitionWrapper = ({ user, setUser }: {
|
||||
<Route path="/materials" element={<MaterialBasePage/>}>
|
||||
<Route index element={<MaterialListPage/>}/>
|
||||
<Route path="new" element={<MaterialNewPage/>}/>
|
||||
<Route path=":id" element ={<MaterialDetailPage user={user}/>}/>
|
||||
<Route path=":id" element ={<MaterialDetailPage/>}/>
|
||||
</Route>
|
||||
{/* <Route path="/materials/search" element={<MaterialSearchPage/>}/> */}
|
||||
<Route path="/wiki" element={<WikiSearchPage/>}/>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
|
||||
import PostFormTagsArea from '@/components/PostFormTagsArea'
|
||||
import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeField'
|
||||
@@ -9,29 +9,35 @@ import { Button } from '@/components/ui/button'
|
||||
import { toast } from '@/components/ui/use-toast'
|
||||
import { isApiError } from '@/lib/api'
|
||||
import { updatePost } from '@/lib/posts'
|
||||
import { inputClass } from '@/lib/utils'
|
||||
import { inputClass, msToTime } from '@/lib/utils'
|
||||
import { useValidationErrors } from '@/lib/useValidationErrors'
|
||||
|
||||
import type { FC, FormEvent } from 'react'
|
||||
|
||||
import type { Post, Tag } from '@/types'
|
||||
import type { Post, TagWithSections } from '@/types'
|
||||
|
||||
type PostFormField =
|
||||
'parentPostIds' | 'tags' | 'originalCreatedAt'
|
||||
'parentPostIds' | 'tags' | 'videoMs' | 'originalCreatedAt'
|
||||
|
||||
const videoMsToDurationValue = (videoMs: number | null): string =>
|
||||
videoMs == null ? '' : String (videoMs / 1_000)
|
||||
|
||||
|
||||
const tagsToStr = (tags: Tag[]): string => {
|
||||
const result: Tag[] = []
|
||||
const tagsToStr = (tags: TagWithSections[]): string => {
|
||||
const result: Omit<TagWithSections, 'children'>[] = []
|
||||
|
||||
const walk = (tag: Tag) => {
|
||||
const walk = (tag: TagWithSections) => {
|
||||
const { children, ...rest } = tag
|
||||
result.push (rest)
|
||||
children?.forEach (walk)
|
||||
children.forEach (walk)
|
||||
}
|
||||
|
||||
tags.filter (t => t.category !== 'nico').forEach (walk)
|
||||
|
||||
return [...(new Set (result.map (t => t.name)))].join (' ')
|
||||
return [...(new Set (result.map (t =>
|
||||
`${ t.name }${ t.sections
|
||||
.map (s => `[${ msToTime (s.beginMs) }-${ s.endMs == null ? '' : msToTime (s.endMs) }]`)
|
||||
.join ('') }`)))].join (' ')
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +47,7 @@ type Props = { post: Post
|
||||
|
||||
const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
const [disabled, setDisabled] = useState (false)
|
||||
const [duration, setDuration] = useState<string> (videoMsToDurationValue (post.videoMs))
|
||||
const { baseErrors, fieldErrors, clearValidationErrors, applyValidationError } =
|
||||
useValidationErrors<PostFormField> ()
|
||||
const [originalCreatedBefore, setOriginalCreatedBefore] =
|
||||
@@ -52,6 +59,10 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
const [tags, setTags] = useState<string> ('')
|
||||
const [title, setTitle] = useState (post.title)
|
||||
|
||||
const videoFlg =
|
||||
useMemo (() => tags.split (/\s+/).some (tag => tag.replace (/\[.*\]$/, '') === '動画'),
|
||||
[tags])
|
||||
|
||||
const dialogue = useDialogue ()
|
||||
|
||||
const update = async (...args: Parameters<typeof updatePost>) => {
|
||||
@@ -63,6 +74,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
onSave ({ ...post,
|
||||
versionNo: data.versionNo,
|
||||
title: data.title,
|
||||
videoMs: data.videoMs,
|
||||
tags: data.tags,
|
||||
parentPosts: data.parentPosts,
|
||||
childPosts: data.childPosts,
|
||||
@@ -102,6 +114,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
{
|
||||
// TODO: 差分 UI
|
||||
await update ({ id: post.id, title, tags, parentPostIds,
|
||||
duration: videoFlg ? duration : null,
|
||||
originalCreatedFrom, originalCreatedBefore },
|
||||
{ baseVersionNo: post.versionNo, merge: true })
|
||||
return
|
||||
@@ -110,6 +123,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
if (action === 'overwrite')
|
||||
{
|
||||
await update ({ id: post.id, title, tags, parentPostIds,
|
||||
duration: videoFlg ? duration : null,
|
||||
originalCreatedFrom, originalCreatedBefore },
|
||||
{ baseVersionNo: post.versionNo, force: true })
|
||||
return
|
||||
@@ -124,6 +138,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
try
|
||||
{
|
||||
await update ({ id: post.id, title, tags, parentPostIds,
|
||||
duration: videoFlg ? duration : null,
|
||||
originalCreatedFrom, originalCreatedBefore },
|
||||
{ baseVersionNo: post.versionNo })
|
||||
}
|
||||
@@ -134,9 +149,15 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
}
|
||||
|
||||
useEffect (() => {
|
||||
setTags(tagsToStr (post.tags))
|
||||
setTags (tagsToStr (post.tags))
|
||||
setDuration (videoMsToDurationValue (post.videoMs))
|
||||
}, [post])
|
||||
|
||||
useEffect (() => {
|
||||
if (!(videoFlg))
|
||||
setDuration ('')
|
||||
}, [videoFlg])
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="max-w-xl pt-2 space-y-4">
|
||||
<FieldError messages={baseErrors}/>
|
||||
@@ -149,7 +170,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
disabled={disabled}
|
||||
className={inputClass (invalid)}
|
||||
value={title ?? ''}
|
||||
onChange={ev => setTitle (ev.target.value)}/>)}
|
||||
onChange={e => setTitle (e.target.value)}/>)}
|
||||
</FormField>
|
||||
|
||||
{/* 親投稿 */}
|
||||
@@ -181,6 +202,20 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
setOriginalCreatedBefore={setOriginalCreatedBefore}
|
||||
errors={fieldErrors.originalCreatedAt}/>
|
||||
|
||||
{/* 動画時間 */}
|
||||
{videoFlg && (
|
||||
<FormField label="動画時間" messages={fieldErrors.videoMs}>
|
||||
{({ invalid }) => (
|
||||
<input
|
||||
type="number"
|
||||
min="0.001"
|
||||
step="0.001"
|
||||
disabled={disabled}
|
||||
className={inputClass (invalid)}
|
||||
value={duration}
|
||||
onChange={e => setDuration (e.target.value)}/>)}
|
||||
</FormField>)}
|
||||
|
||||
{/* 送信 */}
|
||||
<Button type="submit" disabled={disabled}>
|
||||
更新
|
||||
@@ -188,4 +223,5 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||
</form>)
|
||||
}
|
||||
|
||||
|
||||
export default PostEditForm
|
||||
|
||||
@@ -26,13 +26,13 @@ import { dateString, originalCreatedAtString } from '@/lib/utils'
|
||||
import type { DragEndEvent } from '@dnd-kit/core'
|
||||
import type { FC, MutableRefObject, ReactNode } from 'react'
|
||||
|
||||
import type { Category, Post, Tag } from '@/types'
|
||||
import type { Category, Post, TagWithSections } from '@/types'
|
||||
|
||||
type TagByCategory = { [key in Category]: Tag[] }
|
||||
type TagByCategory = { [key in Category]: TagWithSections[] }
|
||||
|
||||
|
||||
const renderTagTree = (
|
||||
tag: Tag,
|
||||
tag: TagWithSections,
|
||||
nestLevel: number,
|
||||
path: string,
|
||||
suppressClickRef: MutableRefObject<boolean>,
|
||||
@@ -63,7 +63,7 @@ const renderTagTree = (
|
||||
|
||||
|
||||
const isDescendant = (
|
||||
root: Tag,
|
||||
root: TagWithSections,
|
||||
targetId: number,
|
||||
): boolean => {
|
||||
if (!(root.children))
|
||||
@@ -84,8 +84,8 @@ const isDescendant = (
|
||||
const findTag = (
|
||||
byCat: TagByCategory,
|
||||
id: number,
|
||||
): Tag | undefined => {
|
||||
const walk = (nodes: Tag[]): Tag | undefined => {
|
||||
): TagWithSections | undefined => {
|
||||
const walk = (nodes: TagWithSections[]): TagWithSections | undefined => {
|
||||
for (const t of nodes)
|
||||
{
|
||||
if (t.id === id)
|
||||
@@ -167,7 +167,7 @@ const TagDetailSidebar: FC<Props> = ({ post, sp }) => {
|
||||
}
|
||||
|
||||
for (const cat of Object.keys (tagsTmp) as (keyof typeof tagsTmp)[])
|
||||
tagsTmp[cat].sort ((tagA: Tag, tagB: Tag) => tagA.name < tagB.name ? -1 : 1)
|
||||
tagsTmp[cat].sort ((tagA: TagWithSections, tagB: TagWithSections) => tagA.name < tagB.name ? -1 : 1)
|
||||
|
||||
return tagsTmp
|
||||
}, [post])
|
||||
|
||||
@@ -46,6 +46,7 @@ export const updatePost = async (
|
||||
post: { id: number
|
||||
title: string | null
|
||||
tags: string
|
||||
duration: string | null
|
||||
parentPostIds: string
|
||||
originalCreatedFrom: string | null
|
||||
originalCreatedBefore: string | null },
|
||||
@@ -58,6 +59,7 @@ export const updatePost = async (
|
||||
`/posts/${ post.id }`,
|
||||
{ title: post.title,
|
||||
tags: post.tags,
|
||||
duration: post.duration,
|
||||
parent_post_ids: post.parentPostIds,
|
||||
original_created_from: post.originalCreatedFrom,
|
||||
original_created_before: post.originalCreatedBefore },
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { cn, originalCreatedAtString, toDate } from '@/lib/utils'
|
||||
import { cn, msToTime, originalCreatedAtString, toDate } from '@/lib/utils'
|
||||
|
||||
describe ('utils', () => {
|
||||
it ('converts strings to dates and leaves date instances intact', () => {
|
||||
@@ -26,3 +26,14 @@ describe ('utils', () => {
|
||||
).toContain ('時刻不詳')
|
||||
})
|
||||
})
|
||||
|
||||
describe ('msToTime', () => {
|
||||
it ('keeps milliseconds when present', () => {
|
||||
expect (msToTime (60_500)).toBe ('1:00.500')
|
||||
expect (msToTime (60_001)).toBe ('1:00.001')
|
||||
})
|
||||
|
||||
it ('omits milliseconds when they are zero', () => {
|
||||
expect (msToTime (60_000)).toBe ('1:00')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -73,6 +73,22 @@ export const originalCreatedAtString = (
|
||||
}
|
||||
|
||||
|
||||
export const msToTime = (ms: number): string => {
|
||||
const totalS = Math.trunc (ms / 1_000)
|
||||
const s = String (totalS % 60)
|
||||
const min = String (Math.trunc (totalS / 60) % 60)
|
||||
const h = Math.trunc (totalS / 3_600)
|
||||
const remainderMs = ms % 1_000
|
||||
|
||||
const base =
|
||||
(h > 0
|
||||
? `${ h }:${ min.padStart (2, '0') }:${ s.padStart (2, '0') }`
|
||||
: `${ min }:${ s.padStart (2, '0') }`)
|
||||
|
||||
return remainderMs > 0 ? `${ base }.${ remainderMs.toString ().padStart (3, '0') }` : base
|
||||
}
|
||||
|
||||
|
||||
export const inputClass = (invalid?: boolean, className?: string): string =>
|
||||
cn ('w-full rounded border p-2',
|
||||
(invalid
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Helmet } from 'react-helmet-async'
|
||||
import { useParams } from 'react-router-dom'
|
||||
|
||||
import TagLink from '@/components/TagLink'
|
||||
@@ -78,6 +79,11 @@ const DeerjikistDetailPage: FC = () => {
|
||||
|
||||
return (
|
||||
<MainArea>
|
||||
<Helmet>
|
||||
<meta name="robots" content="noindex"/>
|
||||
{tag && <title>{tag.name} | ニジラー情報</title>}
|
||||
</Helmet>
|
||||
|
||||
{(loading || !(tag)) ? 'Loading...' : (
|
||||
<div className="max-w-xl">
|
||||
<PageTitle>
|
||||
|
||||
@@ -8,7 +8,6 @@ import { renderWithProviders } from '@/test/render'
|
||||
|
||||
const api = vi.hoisted (() => ({
|
||||
apiGet: vi.fn (),
|
||||
apiPatch: vi.fn (),
|
||||
apiPut: vi.fn (),
|
||||
}))
|
||||
|
||||
@@ -27,7 +26,7 @@ vi.mock ('@/components/ui/use-toast', () => toastApi)
|
||||
const renderPage = () =>
|
||||
renderWithProviders (
|
||||
<Routes>
|
||||
<Route path="/materials/:id" element={<MaterialDetailPage user={null}/>}/>
|
||||
<Route path="/materials/:id" element={<MaterialDetailPage/>}/>
|
||||
</Routes>,
|
||||
{ route: '/materials/8' },
|
||||
)
|
||||
@@ -74,7 +73,6 @@ describe ('MaterialDetailPage', () => {
|
||||
const textboxes = screen.getAllByRole ('textbox')
|
||||
fireEvent.change (textboxes[0], { target: { value: 'new' } })
|
||||
fireEvent.change (textboxes[1], { target: { value: 'https://example.com/ref' } })
|
||||
fireEvent.change (textboxes[2], { target: { value: '素材/new.png' } })
|
||||
fireEvent.click (screen.getByRole ('button', { name: '更新' }))
|
||||
|
||||
await waitFor (() => {
|
||||
@@ -83,7 +81,6 @@ describe ('MaterialDetailPage', () => {
|
||||
const formData = api.apiPut.mock.calls[0]?.[1] as FormData
|
||||
expect (formData.get ('tag')).toBe ('new')
|
||||
expect (formData.get ('url')).toBe ('https://example.com/ref')
|
||||
expect (formData.get ('export_paths[legacy_drive]')).toBe ('素材/new.png')
|
||||
expect (toastApi.toast).toHaveBeenCalledWith ({ title: '更新成功!' })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,23 +13,22 @@ import MainArea from '@/components/layout/MainArea'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { toast } from '@/components/ui/use-toast'
|
||||
import { SITE_TITLE } from '@/config'
|
||||
import { apiGet, apiPatch, apiPut } from '@/lib/api'
|
||||
import { apiGet, apiPut } from '@/lib/api'
|
||||
import { inputClass } from '@/lib/utils'
|
||||
import { useValidationErrors } from '@/lib/useValidationErrors'
|
||||
|
||||
import type { FC } from 'react'
|
||||
|
||||
import type { Material, Tag, User } from '@/types'
|
||||
import type { Material, Tag } from '@/types'
|
||||
|
||||
type MaterialWithTag = Material & { tag: Tag }
|
||||
|
||||
type MaterialFormField = 'tag' | 'file' | 'url' | 'exportPaths'
|
||||
type MaterialFormField = 'tag' | 'file' | 'url'
|
||||
|
||||
|
||||
const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
const MaterialDetailPage: FC = () => {
|
||||
const { id } = useParams ()
|
||||
|
||||
const [exportPath, setExportPath] = useState ('')
|
||||
const [file, setFile] = useState<File | null> (null)
|
||||
const [filePreview, setFilePreview] = useState ('')
|
||||
const [loading, setLoading] = useState (false)
|
||||
@@ -50,7 +49,6 @@ const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
formData.append ('file', file)
|
||||
if (url.trim ())
|
||||
formData.append ('url', url)
|
||||
formData.append ('export_paths[legacy_drive]', exportPath)
|
||||
|
||||
try
|
||||
{
|
||||
@@ -70,30 +68,6 @@ const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
}
|
||||
}
|
||||
|
||||
const handleSuppress = async () => {
|
||||
const reason = window.prompt ('抑止理由を入力してください。')
|
||||
if (reason == null || reason.trim () === '')
|
||||
return
|
||||
if (!window.confirm ('素材ファイルを抑止します。表示と ZIP export から除外されます。'))
|
||||
return
|
||||
|
||||
try
|
||||
{
|
||||
const data = await apiPatch<Material> (
|
||||
`/materials/${ id }/suppress_file`,
|
||||
{ reason },
|
||||
)
|
||||
setMaterial (data)
|
||||
setFile (null)
|
||||
setFilePreview ('')
|
||||
toast ({ title: '抑止しました' })
|
||||
}
|
||||
catch
|
||||
{
|
||||
toast ({ title: '抑止に失敗しました' })
|
||||
}
|
||||
}
|
||||
|
||||
useEffect (() => {
|
||||
if (!(id))
|
||||
return
|
||||
@@ -108,10 +82,11 @@ const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
if (data.file && data.contentType)
|
||||
{
|
||||
setFilePreview (data.file)
|
||||
setFile (null)
|
||||
setFile (new File ([await (await fetch (data.file)).blob ()],
|
||||
data.file,
|
||||
{ type: data.contentType }))
|
||||
}
|
||||
setURL (data.url ?? '')
|
||||
setExportPath (data.exportPaths.legacyDrive ?? '')
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -136,14 +111,7 @@ const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
withCount={false}/>
|
||||
</PageTitle>
|
||||
|
||||
{material.fileSuppressedAt && (
|
||||
<div className="mb-4 rounded border border-red-300 bg-red-50 p-3 text-red-700">
|
||||
<span>素材ファイルは抑止済みです。</span>
|
||||
{material.fileSuppressionReason && (
|
||||
<span> 理由: {material.fileSuppressionReason}</span>)}
|
||||
</div>)}
|
||||
|
||||
{(!material.fileSuppressedAt && material.file && material.contentType) && (
|
||||
{(material.file && material.contentType) && (
|
||||
(/image\/.*/.test (material.contentType) && (
|
||||
<img src={material.file} alt={material.tag.name || undefined}/>))
|
||||
|| (/video\/.*/.test (material.contentType) && (
|
||||
@@ -221,36 +189,13 @@ const MaterialDetailPage: FC<{ user: User | null }> = ({ user }) => {
|
||||
className={inputClass (invalid)}/>)}
|
||||
</FormField>
|
||||
|
||||
<FormField
|
||||
label="ZIP 出力パス"
|
||||
messages={fieldErrors.exportPaths}>
|
||||
{({ describedBy, invalid }) => (
|
||||
<input
|
||||
type="text"
|
||||
value={exportPath}
|
||||
onChange={e => setExportPath (e.target.value)}
|
||||
placeholder="伊地知ニジカ/表情/泣き.png"
|
||||
aria-describedby={describedBy}
|
||||
aria-invalid={invalid}
|
||||
className={inputClass (invalid)}/>)}
|
||||
</FormField>
|
||||
|
||||
{/* 送信 */}
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400"
|
||||
disabled={sending}>
|
||||
更新
|
||||
</Button>
|
||||
{user?.role === 'admin' && !material.fileSuppressedAt && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="destructive"
|
||||
onClick={handleSuppress}>
|
||||
ファイルを抑止
|
||||
</Button>)}
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400"
|
||||
disabled={sending}>
|
||||
更新
|
||||
</Button>
|
||||
</div>
|
||||
</Tab>
|
||||
</TabGroup>
|
||||
|
||||
@@ -9,7 +9,7 @@ import PageTitle from '@/components/common/PageTitle'
|
||||
import SectionTitle from '@/components/common/SectionTitle'
|
||||
import SubsectionTitle from '@/components/common/SubsectionTitle'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
import { API_BASE_URL, SITE_TITLE } from '@/config'
|
||||
import { SITE_TITLE } from '@/config'
|
||||
import { apiGet } from '@/lib/api'
|
||||
|
||||
import type { FC } from 'react'
|
||||
@@ -30,15 +30,10 @@ const MaterialCard = ({ tag }: { tag: TagWithMaterial }) => {
|
||||
to={`/materials/${ tag.material.id }`}
|
||||
className="block w-40 h-40">
|
||||
<div
|
||||
className={`w-full h-full overflow-hidden rounded-xl shadow
|
||||
text-center content-center text-4xl ${
|
||||
tag.material.fileSuppressedAt
|
||||
? 'border-2 border-red-300 bg-red-50 text-base text-red-700'
|
||||
: '' }`}
|
||||
className="w-full h-full overflow-hidden rounded-xl shadow
|
||||
text-center content-center text-4xl"
|
||||
style={{ fontFamily: 'Nikumaru' }}>
|
||||
{tag.material.fileSuppressedAt
|
||||
? <span>抑止済み</span>
|
||||
: (tag.material.contentType && /image\/.*/.test (tag.material.contentType))
|
||||
{(tag.material.contentType && /image\/.*/.test (tag.material.contentType))
|
||||
? <img src={tag.material.file || undefined}/>
|
||||
: <span>照会</span>}
|
||||
</div>
|
||||
@@ -113,7 +108,7 @@ const MaterialListPage: FC = () => {
|
||||
|
||||
<MaterialCard tag={tag}/>
|
||||
|
||||
<div className="ml-2 overflow-x-auto pb-2">
|
||||
<div className="ml-2">
|
||||
{tag.children.map (c2 => (
|
||||
<Fragment key={c2.id}>
|
||||
<SectionTitle>
|
||||
@@ -164,11 +159,7 @@ const MaterialListPage: FC = () => {
|
||||
<p>もしくは……</p>
|
||||
<ul>
|
||||
<li><PrefetchLink to="/materials/new">素材を新規追加する</PrefetchLink></li>
|
||||
<li>
|
||||
<a href={`${ API_BASE_URL }/materials/download.zip?profile=legacy_drive`}>
|
||||
すべての素材をダウンロードする
|
||||
</a>
|
||||
</li>
|
||||
{/* <li><a href="#">すべての素材をダウンロードする</a></li> */}
|
||||
</ul>
|
||||
</>))}
|
||||
</MainArea>)
|
||||
|
||||
@@ -17,7 +17,7 @@ import { useValidationErrors } from '@/lib/useValidationErrors'
|
||||
|
||||
import type { FC } from 'react'
|
||||
|
||||
type MaterialFormField = 'tag' | 'file' | 'url' | 'exportPaths'
|
||||
type MaterialFormField = 'tag' | 'file' | 'url'
|
||||
|
||||
|
||||
const MaterialNewPage: FC = () => {
|
||||
@@ -32,7 +32,6 @@ const MaterialNewPage: FC = () => {
|
||||
const [sending, setSending] = useState (false)
|
||||
const [tag, setTag] = useState (tagQuery)
|
||||
const [url, setURL] = useState ('')
|
||||
const [exportPath, setExportPath] = useState ('')
|
||||
const { baseErrors, fieldErrors, clearValidationErrors, applyValidationError } =
|
||||
useValidationErrors<MaterialFormField> ()
|
||||
|
||||
@@ -46,7 +45,6 @@ const MaterialNewPage: FC = () => {
|
||||
formData.append ('file', file)
|
||||
if (url)
|
||||
formData.append ('url', url)
|
||||
formData.append ('export_paths[legacy_drive]', exportPath)
|
||||
|
||||
try
|
||||
{
|
||||
@@ -135,20 +133,6 @@ const MaterialNewPage: FC = () => {
|
||||
className={inputClass (invalid)}/>)}
|
||||
</FormField>
|
||||
|
||||
<FormField
|
||||
label="ZIP 出力パス"
|
||||
messages={fieldErrors.exportPaths}>
|
||||
{({ describedBy, invalid }) => (
|
||||
<input
|
||||
type="text"
|
||||
value={exportPath}
|
||||
onChange={e => setExportPath (e.target.value)}
|
||||
placeholder="伊地知ニジカ/表情/泣き.png"
|
||||
aria-describedby={describedBy}
|
||||
aria-invalid={invalid}
|
||||
className={inputClass (invalid)}/>)}
|
||||
</FormField>
|
||||
|
||||
{/* 送信 */}
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
|
||||
@@ -15,7 +15,7 @@ import { SITE_TITLE } from '@/config'
|
||||
import { fetchPostChanges, updatePost } from '@/lib/posts'
|
||||
import { postsKeys, tagsKeys } from '@/lib/queryKeys'
|
||||
import { fetchTag } from '@/lib/tags'
|
||||
import { cn, dateString, originalCreatedAtString } from '@/lib/utils'
|
||||
import { cn, dateString, msToTime, originalCreatedAtString } from '@/lib/utils'
|
||||
|
||||
import type { FC, MouseEvent } from 'react'
|
||||
|
||||
@@ -91,9 +91,13 @@ const PostHistoryPage: FC = () => {
|
||||
.filter (p => p.type !== 'removed')
|
||||
.map (p => p.id)
|
||||
.join (' ')
|
||||
const duration =
|
||||
change.videoMs.current == null
|
||||
? null
|
||||
: msToTime (change.videoMs.current)
|
||||
const originalCreatedFrom = change.originalCreatedFrom.current
|
||||
const originalCreatedBefore = change.originalCreatedBefore.current
|
||||
await updatePost ({ id, title, tags, parentPostIds,
|
||||
await updatePost ({ id, title, tags, duration, parentPostIds,
|
||||
originalCreatedFrom, originalCreatedBefore },
|
||||
{ force: true })
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useState, useRef } from 'react'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { Helmet } from 'react-helmet-async'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
|
||||
@@ -25,7 +25,7 @@ import type { User } from '@/types'
|
||||
type Props = { user: User | null }
|
||||
|
||||
type PostFormField =
|
||||
'url' | 'title' | 'tags' | 'parentPostIds' | 'originalCreatedAt' | 'thumbnail'
|
||||
'url' | 'title' | 'tags' | 'parentPostIds' | 'videoMs' | 'originalCreatedAt' | 'thumbnail'
|
||||
|
||||
|
||||
const PostNewPage: FC<Props> = ({ user }) => {
|
||||
@@ -40,6 +40,7 @@ const PostNewPage: FC<Props> = ({ user }) => {
|
||||
const [originalCreatedFrom, setOriginalCreatedFrom] = useState<string | null> (null)
|
||||
const [parentPostIds, setParentPostIds] = useState ('')
|
||||
const [tags, setTags] = useState ('')
|
||||
const [duration, setDuration] = useState ('')
|
||||
const [thumbnailAutoFlg, setThumbnailAutoFlg] = useState (true)
|
||||
const [thumbnailFile, setThumbnailFile] = useState<File | null> (null)
|
||||
const [thumbnailLoading, setThumbnailLoading] = useState (false)
|
||||
@@ -51,6 +52,9 @@ const PostNewPage: FC<Props> = ({ user }) => {
|
||||
|
||||
const previousURLRef = useRef ('')
|
||||
const thumbnailPreviewRef = useRef ('')
|
||||
const videoFlg =
|
||||
useMemo (() => tags.split (/\s+/).some (tag => tag.replace (/\[.*\]$/, '') === '動画'),
|
||||
[tags])
|
||||
|
||||
const handleSubmit = async () => {
|
||||
clearValidationErrors ()
|
||||
@@ -60,6 +64,8 @@ const PostNewPage: FC<Props> = ({ user }) => {
|
||||
formData.append ('url', url)
|
||||
formData.append ('tags', tags)
|
||||
formData.append ('parent_post_ids', parentPostIds)
|
||||
if (videoFlg && duration !== '')
|
||||
formData.append ('duration', duration)
|
||||
if (thumbnailFile)
|
||||
formData.append ('thumbnail', thumbnailFile)
|
||||
if (originalCreatedFrom)
|
||||
@@ -129,6 +135,11 @@ const PostNewPage: FC<Props> = ({ user }) => {
|
||||
fetchThumbnail ()
|
||||
}, [fetchThumbnail, thumbnailAutoFlg, url])
|
||||
|
||||
useEffect (() => {
|
||||
if (!(videoFlg))
|
||||
setDuration ('')
|
||||
}, [videoFlg])
|
||||
|
||||
if (!(editable))
|
||||
return <Forbidden/>
|
||||
|
||||
@@ -233,6 +244,20 @@ const PostNewPage: FC<Props> = ({ user }) => {
|
||||
setOriginalCreatedBefore={setOriginalCreatedBefore}
|
||||
errors={fieldErrors.originalCreatedAt}/>
|
||||
|
||||
{/* 動画時間 */}
|
||||
{(videoFlg &&
|
||||
<FormField label="動画時間" messages={fieldErrors.videoMs}>
|
||||
{({ invalid }) => (
|
||||
<input
|
||||
type="number"
|
||||
min="0.001"
|
||||
step="0.001"
|
||||
value={duration}
|
||||
onChange={e => setDuration (e.target.value)}
|
||||
aria-invalid={invalid}
|
||||
className={inputClass (invalid)}/>)}
|
||||
</FormField>)}
|
||||
|
||||
{/* 送信 */}
|
||||
<Button onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Material,
|
||||
Post,
|
||||
Tag,
|
||||
TagWithSections,
|
||||
Theatre,
|
||||
TheatreComment,
|
||||
TheatreInfo,
|
||||
@@ -9,7 +9,7 @@ import type { Material,
|
||||
User,
|
||||
WikiPage } from '@/types'
|
||||
|
||||
export const buildTag = (overrides: Partial<Tag> = {}): Tag => ({
|
||||
export const buildTag = (overrides: Partial<TagWithSections> = {}): TagWithSections => ({
|
||||
id: 1,
|
||||
name: 'テストタグ',
|
||||
category: 'general',
|
||||
@@ -23,6 +23,8 @@ export const buildTag = (overrides: Partial<Tag> = {}): Tag => ({
|
||||
materialId: null,
|
||||
hasDeerjikists: false,
|
||||
matchedAlias: null,
|
||||
sections: [],
|
||||
children: [],
|
||||
...overrides,
|
||||
})
|
||||
|
||||
@@ -33,6 +35,7 @@ export const buildPost = (overrides: Partial<Post> = {}): Post => ({
|
||||
title: 'テスト投稿',
|
||||
thumbnail: 'https://example.com/thumb.jpg',
|
||||
thumbnailBase: null,
|
||||
videoMs: null,
|
||||
tags: [buildTag ()],
|
||||
parentPosts: [],
|
||||
childPosts: [],
|
||||
@@ -71,21 +74,16 @@ export const buildWikiPage = (overrides: Partial<WikiPage> = {}): WikiPage => ({
|
||||
})
|
||||
|
||||
export const buildMaterial = (overrides: Partial<Material> = {}): Material => ({
|
||||
id: 1,
|
||||
versionNo: 1,
|
||||
tag: buildTag (),
|
||||
file: null,
|
||||
url: null,
|
||||
wikiPageBody: null,
|
||||
contentType: null,
|
||||
fileSuppressedAt: null,
|
||||
fileSuppressionReason: null,
|
||||
exportPaths: {},
|
||||
exportItems: [],
|
||||
createdAt: '2026-01-02T03:04:05.000Z',
|
||||
createdByUser: { id: 1, name: 'creator' },
|
||||
updatedAt: '2026-01-03T03:04:05.000Z',
|
||||
updatedByUser: { id: 2, name: 'updater' },
|
||||
id: 1,
|
||||
tag: buildTag (),
|
||||
file: null,
|
||||
url: null,
|
||||
wikiPageBody: null,
|
||||
contentType: null,
|
||||
createdAt: '2026-01-02T03:04:05.000Z',
|
||||
createdByUser: { id: 1, name: 'creator' },
|
||||
updatedAt: '2026-01-03T03:04:05.000Z',
|
||||
updatedByUser: { id: 2, name: 'updater' },
|
||||
...overrides,
|
||||
})
|
||||
|
||||
|
||||
+17
-23
@@ -66,27 +66,16 @@ export type FetchNicoTagsOrder = `${ FetchNicoTagsOrderField }:${ 'asc' | 'desc'
|
||||
export type FetchNicoTagsOrderField = 'name' | 'created_at' | 'updated_at'
|
||||
|
||||
export type Material = {
|
||||
id: number
|
||||
versionNo: number
|
||||
tag: Tag
|
||||
file: string | null
|
||||
url: string | null
|
||||
wikiPageBody?: string | null
|
||||
contentType: string | null
|
||||
fileSuppressedAt: string | null
|
||||
fileSuppressionReason: string | null
|
||||
exportPaths: Record<string, string>
|
||||
exportItems: MaterialExportItem[]
|
||||
createdAt: string
|
||||
createdByUser: { id: number; name: string }
|
||||
updatedAt: string
|
||||
updatedByUser: { id: number; name: string } }
|
||||
|
||||
export type MaterialExportItem = {
|
||||
id: number
|
||||
profile: string
|
||||
exportPath: string
|
||||
enabled: boolean }
|
||||
id: number
|
||||
tag: Tag
|
||||
file: string | null
|
||||
url: string | null
|
||||
wikiPageBody?: string | null
|
||||
contentType: string | null
|
||||
createdAt: string
|
||||
createdByUser: { id: number; name: string }
|
||||
updatedAt: string
|
||||
updatedByUser: { id: number; name: string } }
|
||||
|
||||
export type Menu = MenuItem[]
|
||||
|
||||
@@ -151,11 +140,12 @@ export type Post = {
|
||||
title: string | null
|
||||
thumbnail: string | null
|
||||
thumbnailBase: string | null
|
||||
videoMs: number | null
|
||||
postSimilarityEdges?: {
|
||||
targetPostId: number
|
||||
cos: number
|
||||
}[]
|
||||
tags: Tag[]
|
||||
tags: TagWithSections[]
|
||||
parentPosts?: Post[]
|
||||
childPosts?: Post[]
|
||||
siblingPosts?: Record<`${ number }`, Post[]>
|
||||
@@ -183,6 +173,7 @@ export type PostVersion = {
|
||||
url: { current: string; prev: string | null }
|
||||
thumbnail: { current: string | null; prev: string | null }
|
||||
thumbnailBase: { current: string | null; prev: string | null }
|
||||
videoMs: { current: number | null; prev: number | null }
|
||||
tags: { name: string
|
||||
type: 'context' | 'added' | 'removed' }[]
|
||||
parentPosts: { id: number
|
||||
@@ -217,7 +208,6 @@ export type Tag = {
|
||||
hasWiki: boolean
|
||||
materialId: number | null
|
||||
hasDeerjikists: boolean
|
||||
children?: Tag[]
|
||||
matchedAlias?: string | null }
|
||||
|
||||
export type TagVersion = {
|
||||
@@ -232,6 +222,10 @@ export type TagVersion = {
|
||||
createdAt: string
|
||||
createdByUser: { id: number; name: string | null } | null }
|
||||
|
||||
export type TagWithSections = Tag & { sections: { beginMs: number
|
||||
endMs: number | null }[]
|
||||
children: TagWithSections[] }
|
||||
|
||||
export type Theatre = {
|
||||
id: number
|
||||
name: string | null
|
||||
|
||||
新しい課題から参照
ユーザをブロックする