Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 638dccad6d |
@@ -1,4 +1,7 @@
|
|||||||
class ApplicationController < ActionController::API
|
class ApplicationController < ActionController::API
|
||||||
|
rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid
|
||||||
|
rescue_from ActiveRecord::RecordNotUnique, with: :render_record_not_unique
|
||||||
|
|
||||||
before_action :reject_banned_ip_address!
|
before_action :reject_banned_ip_address!
|
||||||
before_action :authenticate_user
|
before_action :authenticate_user
|
||||||
before_action :reject_banned_user!
|
before_action :reject_banned_user!
|
||||||
@@ -25,6 +28,42 @@ class ApplicationController < ActionController::API
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_bad_request message = 'リクエストが不正です.', field: nil, code: :bad_request
|
||||||
|
render_error(:bad_request, message, field:, code:)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_unprocessable_entity message = '入力を確認してください.', field: nil, code: :invalid
|
||||||
|
render_error(:unprocessable_entity, message, field:, code:)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_error status, message, field: nil, code: status
|
||||||
|
error = { code: code.to_s, message: }
|
||||||
|
error[:field] = field.to_s if field.present?
|
||||||
|
|
||||||
|
render json: { errors: [error] }, status:
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_model_errors record, status: :unprocessable_entity
|
||||||
|
errors =
|
||||||
|
record.errors.map do |error|
|
||||||
|
{ code: error.type.to_s,
|
||||||
|
field: error.attribute.to_s,
|
||||||
|
message: error.full_message }
|
||||||
|
end
|
||||||
|
|
||||||
|
errors = [{ code: 'invalid', message: '入力を確認してください.' }] if errors.empty?
|
||||||
|
|
||||||
|
render json: { errors: }, status:
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_record_invalid error
|
||||||
|
render_model_errors(error.record)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_record_not_unique _error = nil
|
||||||
|
render_unprocessable_entity('既に存在してゐます.', code: :taken)
|
||||||
|
end
|
||||||
|
|
||||||
def reject_banned_ip_address!
|
def reject_banned_ip_address!
|
||||||
ip_address = IpAddress.find_by(ip_address: IPAddr.new(request.remote_ip).hton)
|
ip_address = IpAddress.find_by(ip_address: IPAddr.new(request.remote_ip).hton)
|
||||||
return unless ip_address&.banned?
|
return unless ip_address&.banned?
|
||||||
@@ -37,4 +76,28 @@ class ApplicationController < ActionController::API
|
|||||||
|
|
||||||
head :forbidden
|
head :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_validation_error record = nil, fields: { }, base: []
|
||||||
|
errors = { }
|
||||||
|
|
||||||
|
if record
|
||||||
|
record.errors.messages.each do |attr, messages|
|
||||||
|
errors[attr] ||= []
|
||||||
|
errors[attr].concat(messages)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fields.each do |attr, messages|
|
||||||
|
errors[attr] ||= []
|
||||||
|
errors[attr].concat(Array(messages))
|
||||||
|
end
|
||||||
|
|
||||||
|
base_errors = Array(base) - Array(errors.delete(:base))
|
||||||
|
|
||||||
|
render json: { type: 'validation_error',
|
||||||
|
message: '入力内容を確認してください.',
|
||||||
|
errors:,
|
||||||
|
base_errors: },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ class DeerjikistsController < ApplicationController
|
|||||||
def show
|
def show
|
||||||
platform = params[:platform].to_s.strip
|
platform = params[:platform].to_s.strip
|
||||||
code = params[:code].to_s.strip
|
code = params[:code].to_s.strip
|
||||||
return head :bad_request if platform.blank? || code.blank?
|
return render_bad_request('platform は必須です.', field: :platform) if platform.blank?
|
||||||
|
return render_bad_request('code は必須です.', field: :code) if code.blank?
|
||||||
|
|
||||||
deerjikist = Deerjikist
|
deerjikist = Deerjikist
|
||||||
.joins(:tag)
|
.joins(:tag)
|
||||||
@@ -22,7 +23,9 @@ class DeerjikistsController < ApplicationController
|
|||||||
platform = params[:platform].to_s.strip
|
platform = params[:platform].to_s.strip
|
||||||
code = params[:code].to_s.strip
|
code = params[:code].to_s.strip
|
||||||
tag_id = params[:tag_id].to_i
|
tag_id = params[:tag_id].to_i
|
||||||
return head :bad_request if platform.blank? || code.blank? || tag_id <= 0
|
return render_bad_request('platform は必須です.', field: :platform) if platform.blank?
|
||||||
|
return render_bad_request('code は必須です.', field: :code) if code.blank?
|
||||||
|
return render_bad_request('tag_id が不正です.', field: :tag_id) if tag_id <= 0
|
||||||
|
|
||||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:).tap do |d|
|
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:).tap do |d|
|
||||||
d.tag_id = tag_id
|
d.tag_id = tag_id
|
||||||
@@ -38,7 +41,8 @@ class DeerjikistsController < ApplicationController
|
|||||||
|
|
||||||
platform = params[:platform].to_s.strip
|
platform = params[:platform].to_s.strip
|
||||||
code = params[:code].to_s.strip
|
code = params[:code].to_s.strip
|
||||||
return head :bad_request if platform.blank? || code.blank?
|
return render_bad_request('platform は必須です.', field: :platform) if platform.blank?
|
||||||
|
return render_bad_request('code は必須です.', field: :code) if code.blank?
|
||||||
|
|
||||||
Deerjikist.find([platform, code]).destroy!
|
Deerjikist.find([platform, code]).destroy!
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ class MaterialsController < ApplicationController
|
|||||||
tag_name_raw = params[:tag].to_s.strip
|
tag_name_raw = params[:tag].to_s.strip
|
||||||
file = params[:file]
|
file = params[:file]
|
||||||
url = params[:url].to_s.strip.presence
|
url = params[:url].to_s.strip.presence
|
||||||
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
|
return render_bad_request('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||||
|
return render_bad_request('ファイルまたは URL は必須です.') if file.blank? && url.blank?
|
||||||
|
|
||||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||||
tag = tag_name.tag
|
tag = tag_name.tag
|
||||||
@@ -54,7 +55,7 @@ class MaterialsController < ApplicationController
|
|||||||
if material.save
|
if material.save
|
||||||
render json: MaterialRepr.base(material, host: request.base_url), status: :created
|
render json: MaterialRepr.base(material, host: request.base_url), status: :created
|
||||||
else
|
else
|
||||||
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
|
render_model_errors(material)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -68,7 +69,8 @@ class MaterialsController < ApplicationController
|
|||||||
tag_name_raw = params[:tag].to_s.strip
|
tag_name_raw = params[:tag].to_s.strip
|
||||||
file = params[:file]
|
file = params[:file]
|
||||||
url = params[:url].to_s.strip.presence
|
url = params[:url].to_s.strip.presence
|
||||||
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
|
return render_bad_request('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||||
|
return render_bad_request('ファイルまたは URL は必須です.') if file.blank? && url.blank?
|
||||||
|
|
||||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||||
tag = tag_name.tag
|
tag = tag_name.tag
|
||||||
@@ -84,7 +86,7 @@ class MaterialsController < ApplicationController
|
|||||||
if material.save
|
if material.save
|
||||||
render json: MaterialRepr.base(material, host: request.base_url)
|
render json: MaterialRepr.base(material, host: request.base_url)
|
||||||
else
|
else
|
||||||
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
|
render_model_errors(material)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -30,12 +30,14 @@ class NicoTagsController < ApplicationController
|
|||||||
id = params[:id].to_i
|
id = params[:id].to_i
|
||||||
|
|
||||||
tag = Tag.find(id)
|
tag = Tag.find(id)
|
||||||
return head :bad_request unless tag.nico?
|
return render_bad_request('ニコニコ・タグを指定してください.', field: :id) unless tag.nico?
|
||||||
|
|
||||||
linked_tag_names = params[:tags].to_s.split
|
linked_tag_names = params[:tags].to_s.split
|
||||||
linked_tags = Tag.normalise_tags!(linked_tag_names, with_tagme: false,
|
linked_tags = Tag.normalise_tags!(linked_tag_names, with_tagme: false,
|
||||||
with_no_deerjikist: false)
|
with_no_deerjikist: false)
|
||||||
return head :bad_request if linked_tags.any? { |t| t.nico? }
|
if linked_tags.any? { |t| t.nico? }
|
||||||
|
return render_bad_request('ニコニコ・タグ同士は連携できません.', field: :tags)
|
||||||
|
end
|
||||||
|
|
||||||
ApplicationRecord.transaction do
|
ApplicationRecord.transaction do
|
||||||
TagVersioning.record_tag_snapshots!(linked_tags, created_by_user: current_user)
|
TagVersioning.record_tag_snapshots!(linked_tags, created_by_user: current_user)
|
||||||
|
|||||||
@@ -95,10 +95,9 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def random
|
def random
|
||||||
post = filtered_posts
|
post = filtered_posts.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||||
.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
.order('RAND()')
|
||||||
.order('RAND()')
|
.first
|
||||||
.first
|
|
||||||
return head :not_found unless post
|
return head :not_found unless post
|
||||||
|
|
||||||
render json: PostRepr.base(post, current_user)
|
render json: PostRepr.base(post, current_user)
|
||||||
@@ -109,7 +108,7 @@ class PostsController < ApplicationController
|
|||||||
return head :not_found unless post
|
return head :not_found unless post
|
||||||
|
|
||||||
render json: PostRepr.base(post, current_user)
|
render json: PostRepr.base(post, current_user)
|
||||||
.merge(tags: build_tag_tree_for(post),
|
.merge(tags: build_tag_tree_for(post.tags),
|
||||||
related: PostRepr.many(post.related(limit: 20)))
|
related: PostRepr.many(post.related(limit: 20)))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -133,11 +132,11 @@ class PostsController < ApplicationController
|
|||||||
ApplicationRecord.transaction do
|
ApplicationRecord.transaction do
|
||||||
post.save!
|
post.save!
|
||||||
|
|
||||||
Tag.normalise_tags!(tag_names, with_sections: true) => { tags:, sections: }
|
tags = Tag.normalise_tags!(tag_names)
|
||||||
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user)
|
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user)
|
||||||
|
|
||||||
tags = Tag.expand_parent_tags(tags)
|
tags = Tag.expand_parent_tags(tags)
|
||||||
sync_post_tags!(post, tags, sections)
|
sync_post_tags!(post, tags)
|
||||||
|
|
||||||
sync_parent_posts!(post, parent_post_ids)
|
sync_parent_posts!(post, parent_post_ids)
|
||||||
|
|
||||||
@@ -149,11 +148,11 @@ class PostsController < ApplicationController
|
|||||||
post.reload
|
post.reload
|
||||||
render json: PostRepr.base(post), status: :created
|
render json: PostRepr.base(post), status: :created
|
||||||
rescue Tag::NicoTagNormalisationError
|
rescue Tag::NicoTagNormalisationError
|
||||||
head :bad_request
|
render_bad_request('ニコニコ・タグは直接指定できません.', field: :tags)
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
render json: { errors: [e.message] }, status: :unprocessable_entity
|
render_unprocessable_entity(e.message)
|
||||||
rescue ActiveRecord::RecordInvalid => e
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
|
render_model_errors(e.record)
|
||||||
end
|
end
|
||||||
|
|
||||||
def viewed
|
def viewed
|
||||||
@@ -176,10 +175,10 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
force = bool?(:force)
|
force = bool?(:force)
|
||||||
merge = bool?(:merge)
|
merge = bool?(:merge)
|
||||||
return head :bad_request if force && merge
|
return render_bad_request('force と merge は同時に指定できません.') if force && merge
|
||||||
|
|
||||||
base_version_no = parse_base_version_no
|
base_version_no = parse_base_version_no
|
||||||
return head :bad_request if !(force) && !(base_version_no)
|
return render_bad_request('base_version_no は必須です.', field: :base_version_no) if !(force) && !(base_version_no)
|
||||||
|
|
||||||
title = params[:title].presence
|
title = params[:title].presence
|
||||||
tag_names = params[:tags].to_s.split
|
tag_names = params[:tags].to_s.split
|
||||||
@@ -236,14 +235,14 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
post.reload
|
post.reload
|
||||||
json = PostRepr.base(post, current_user)
|
json = PostRepr.base(post, current_user)
|
||||||
json['tags'] = build_tag_tree_for(post)
|
json['tags'] = build_tag_tree_for(post.tags)
|
||||||
render json:, status: :ok
|
render json:, status: :ok
|
||||||
rescue Tag::NicoTagNormalisationError
|
rescue Tag::NicoTagNormalisationError
|
||||||
head :bad_request
|
render_bad_request('ニコニコ・タグは直接指定できません.', field: :tags)
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
render json: { errors: [e.message] }, status: :unprocessable_entity
|
render_validation_error(fields: { parent_post_ids: [e.message] })
|
||||||
rescue ActiveRecord::RecordInvalid => e
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
|
render_validation_error(e.record)
|
||||||
end
|
end
|
||||||
|
|
||||||
def changes
|
def changes
|
||||||
@@ -338,7 +337,7 @@ class PostsController < ApplicationController
|
|||||||
def tagged_post_ids_for(name) =
|
def tagged_post_ids_for(name) =
|
||||||
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)
|
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)
|
||||||
|
|
||||||
def sync_post_tags! post, desired_tags, sections
|
def sync_post_tags! post, desired_tags
|
||||||
desired_tags.each do |t|
|
desired_tags.each do |t|
|
||||||
t.save! if t.new_record?
|
t.save! if t.new_record?
|
||||||
end
|
end
|
||||||
@@ -357,20 +356,13 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
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|
|
PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
|
||||||
pt.discard_by!(current_user)
|
pt.discard_by!(current_user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_tag_tree_for post
|
def build_tag_tree_for tags
|
||||||
tags = post.tags.to_a
|
tags = tags.to_a
|
||||||
tag_ids = tags.map(&:id)
|
tag_ids = tags.map(&:id)
|
||||||
|
|
||||||
implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
|
implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
|
||||||
@@ -392,11 +384,8 @@ class PostsController < ApplicationController
|
|||||||
tag = tags_by_id[tag_id]
|
tag = tags_by_id[tag_id]
|
||||||
return nil unless tag
|
return nil unless tag
|
||||||
|
|
||||||
sections = PostTagSection.where(post_id: post.id, tag_id:)
|
|
||||||
.as_json(only: [:begin_ms, :end_ms])
|
|
||||||
|
|
||||||
if path.include?(tag_id)
|
if path.include?(tag_id)
|
||||||
return TagRepr.base(tag).merge(children: [], sections:)
|
return TagRepr.base(tag).merge(children: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
if memo.key?(tag_id)
|
if memo.key?(tag_id)
|
||||||
@@ -408,7 +397,7 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
||||||
|
|
||||||
memo[tag_id] = TagRepr.base(tag).merge(children:, sections:)
|
memo[tag_id] = TagRepr.base(tag).merge(children:)
|
||||||
end
|
end
|
||||||
|
|
||||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||||
@@ -419,7 +408,7 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
params[:parent_post_ids].to_s.split.map { |token|
|
params[:parent_post_ids].to_s.split.map { |token|
|
||||||
id = Integer(token, exception: false)
|
id = Integer(token, exception: false)
|
||||||
raise ArgumentError, "親投稿 Id. が不正です: #{ token }" if !(id) || id <= 0
|
raise ArgumentError, "親投稿 Id. が不正です: #{ token }" if id.nil? || id <= 0
|
||||||
|
|
||||||
id
|
id
|
||||||
}.uniq
|
}.uniq
|
||||||
@@ -427,7 +416,7 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
def sync_parent_posts! post, parent_post_ids
|
def sync_parent_posts! post, parent_post_ids
|
||||||
if parent_post_ids.include?(post.id)
|
if parent_post_ids.include?(post.id)
|
||||||
post.errors.add(:base, '自分自身を親投稿にはできません.')
|
post.errors.add(:parent_post_ids, '自分自身を親投稿にはできません.')
|
||||||
raise ActiveRecord::RecordInvalid, post
|
raise ActiveRecord::RecordInvalid, post
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -481,21 +470,7 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def editable_tag_names_from_post post
|
def editable_tag_names_from_post post
|
||||||
post
|
post.tags.not_nico.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
|
||||||
.post_tags
|
|
||||||
.kept
|
|
||||||
.joins(tag: :tag_name)
|
|
||||||
.merge(Tag.not_nico)
|
|
||||||
.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
|
end
|
||||||
|
|
||||||
def post_incoming_snapshot title:, original_created_from:, original_created_before:,
|
def post_incoming_snapshot title:, original_created_from:, original_created_before:,
|
||||||
@@ -527,16 +502,9 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def incoming_tag_names_for_snapshot raw_tag_names
|
def incoming_tag_names_for_snapshot raw_tag_names
|
||||||
Tag.normalise_tags!(raw_tag_names, with_tagme: false, with_sections: true) =>
|
tags = Tag.normalise_tags!(raw_tag_names, with_tagme: false)
|
||||||
{ tags:, sections: }
|
|
||||||
|
|
||||||
Tag.expand_parent_tags(tags).uniq(&:id).map { |tag|
|
Tag.expand_parent_tags(tags).map(&:name).uniq.sort
|
||||||
"#{ tag.name }#{ sections[tag.id].to_a.map { section_literal(_1) }.join }"
|
|
||||||
}.sort
|
|
||||||
end
|
|
||||||
|
|
||||||
def section_literal section
|
|
||||||
"[#{ Post.ms_to_time(section[0]) }-#{ Post.ms_to_time(section[1]) }]"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_conflict_json post:, base_version_no:, base_snapshot:,
|
def post_conflict_json post:, base_version_no:, base_snapshot:,
|
||||||
@@ -623,8 +591,7 @@ class PostsController < ApplicationController
|
|||||||
original_created_from: snapshot[:original_created_from],
|
original_created_from: snapshot[:original_created_from],
|
||||||
original_created_before: snapshot[:original_created_before])
|
original_created_before: snapshot[:original_created_before])
|
||||||
|
|
||||||
Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false, with_sections: true) =>
|
editable_tags = Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false)
|
||||||
{ tags: editable_tags, sections: }
|
|
||||||
TagVersioning.record_tag_snapshots!(editable_tags, created_by_user: current_user)
|
TagVersioning.record_tag_snapshots!(editable_tags, created_by_user: current_user)
|
||||||
|
|
||||||
readonly_tags = post.tags.nico.to_a
|
readonly_tags = post.tags.nico.to_a
|
||||||
@@ -632,7 +599,7 @@ class PostsController < ApplicationController
|
|||||||
tags = readonly_tags + editable_tags
|
tags = readonly_tags + editable_tags
|
||||||
tags = Tag.expand_parent_tags(tags)
|
tags = Tag.expand_parent_tags(tags)
|
||||||
|
|
||||||
sync_post_tags!(post, tags, sections)
|
sync_post_tags!(post, tags)
|
||||||
sync_parent_posts!(post, snapshot[:parent_post_ids])
|
sync_parent_posts!(post, snapshot[:parent_post_ids])
|
||||||
|
|
||||||
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user)
|
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ class PreviewController < ApplicationController
|
|||||||
return head :unauthorized unless current_user
|
return head :unauthorized unless current_user
|
||||||
|
|
||||||
url = params[:url]
|
url = params[:url]
|
||||||
return head :bad_request unless url.present?
|
return render_bad_request('URL は必須です.', field: :url) unless url.present?
|
||||||
|
|
||||||
unless url.start_with?(/http(s)?:\/\//)
|
unless url.start_with?(/http(s)?:\/\//)
|
||||||
url = 'http://' + url
|
url = 'http://' + url
|
||||||
@@ -16,7 +16,7 @@ class PreviewController < ApplicationController
|
|||||||
|
|
||||||
render json: { title: title }
|
render json: { title: title }
|
||||||
rescue => e
|
rescue => e
|
||||||
render json: { error: e.message }, status: :bad_request
|
render_bad_request(e.message, field: :url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def thumbnail
|
def thumbnail
|
||||||
@@ -25,7 +25,7 @@ class PreviewController < ApplicationController
|
|||||||
return head :unauthorized unless current_user
|
return head :unauthorized unless current_user
|
||||||
|
|
||||||
url = params[:url]
|
url = params[:url]
|
||||||
return head :bad_request if url.blank?
|
return render_bad_request('URL は必須です.', field: :url) if url.blank?
|
||||||
|
|
||||||
unless url.start_with?(/http(s)?:\/\//)
|
unless url.start_with?(/http(s)?:\/\//)
|
||||||
url = 'http://' + url
|
url = 'http://' + url
|
||||||
@@ -40,7 +40,8 @@ class PreviewController < ApplicationController
|
|||||||
File.delete(path) rescue nil
|
File.delete(path) rescue nil
|
||||||
send_file image.path, type: 'image/png', disposition: 'inline'
|
send_file image.path, type: 'image/png', disposition: 'inline'
|
||||||
else
|
else
|
||||||
render json: { error: 'Failed to generate thumbnail' }, status: :internal_server_error
|
render_error(:internal_server_error, 'サムネールを生成できませんでした.',
|
||||||
|
code: :thumbnail_generation_failed)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ class TagChildrenController < ApplicationController
|
|||||||
|
|
||||||
parent_id = params[:parent_id]
|
parent_id = params[:parent_id]
|
||||||
child_id = params[:child_id]
|
child_id = params[:child_id]
|
||||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
return render_bad_request('parent_id は必須です.', field: :parent_id) if parent_id.blank?
|
||||||
|
return render_bad_request('child_id は必須です.', field: :child_id) if child_id.blank?
|
||||||
|
|
||||||
parent = Tag.find(parent_id)
|
parent = Tag.find(parent_id)
|
||||||
child = Tag.find(child_id)
|
child = Tag.find(child_id)
|
||||||
return head :bad_request if parent.nico? || child.nico?
|
return render_bad_request('ニコニコ・タグの階層は変更できません.') if parent.nico? || child.nico?
|
||||||
|
|
||||||
ApplicationRecord.transaction do
|
ApplicationRecord.transaction do
|
||||||
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
||||||
@@ -27,11 +28,12 @@ class TagChildrenController < ApplicationController
|
|||||||
|
|
||||||
parent_id = params[:parent_id]
|
parent_id = params[:parent_id]
|
||||||
child_id = params[:child_id]
|
child_id = params[:child_id]
|
||||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
return render_bad_request('parent_id は必須です.', field: :parent_id) if parent_id.blank?
|
||||||
|
return render_bad_request('child_id は必須です.', field: :child_id) if child_id.blank?
|
||||||
|
|
||||||
parent = Tag.find(parent_id)
|
parent = Tag.find(parent_id)
|
||||||
child = Tag.find(child_id)
|
child = Tag.find(child_id)
|
||||||
return head :bad_request if parent.nico? || child.nico?
|
return render_bad_request('ニコニコ・タグの階層は変更できません.') if parent.nico? || child.nico?
|
||||||
|
|
||||||
ApplicationRecord.transaction do
|
ApplicationRecord.transaction do
|
||||||
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
def show_by_name
|
def show_by_name
|
||||||
name = params[:name].to_s.strip
|
name = params[:name].to_s.strip
|
||||||
return head :bad_request if name.blank?
|
return render_bad_request('name は必須です.', field: :name) if name.blank?
|
||||||
|
|
||||||
tag = Tag.joins(:tag_name)
|
tag = Tag.joins(:tag_name)
|
||||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||||
@@ -192,7 +192,7 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
def deerjikists_by_name
|
def deerjikists_by_name
|
||||||
name = params[:name].to_s.strip
|
name = params[:name].to_s.strip
|
||||||
return head :bad_request if name.blank?
|
return render_bad_request('name は必須です.', field: :name) if name.blank?
|
||||||
|
|
||||||
tag = Tag.joins(:tag_name)
|
tag = Tag.joins(:tag_name)
|
||||||
.includes(:tag_name, tag_name: :wiki_page)
|
.includes(:tag_name, tag_name: :wiki_page)
|
||||||
@@ -228,7 +228,7 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
def materials_by_name
|
def materials_by_name
|
||||||
name = params[:name].to_s.strip
|
name = params[:name].to_s.strip
|
||||||
return head :bad_request if name.blank?
|
return render_bad_request('name は必須です.', field: :name) if name.blank?
|
||||||
|
|
||||||
tag = Tag.joins(:tag_name)
|
tag = Tag.joins(:tag_name)
|
||||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||||
@@ -247,17 +247,16 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
name = params[:name].to_s.strip
|
name = params[:name].to_s.strip
|
||||||
category = params[:category].to_s.strip
|
category = params[:category].to_s.strip
|
||||||
return head :unprocessable_entity if name.blank? || category.blank?
|
return render_unprocessable_entity('名前は必須です.', field: :name) if name.blank?
|
||||||
|
return render_unprocessable_entity('カテゴリは必須です.', field: :category) if category.blank?
|
||||||
|
|
||||||
if name != tag.name &&
|
if name != tag.name &&
|
||||||
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico])
|
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico])
|
||||||
return render json: { error: 'システム・タグの名称は変更できません.' },
|
return render_unprocessable_entity('システム・タグの名称は変更できません.', field: :name)
|
||||||
status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if tag.nico? || category == 'nico'
|
if tag.nico? || category == 'nico'
|
||||||
return render json: { error: 'ニコタグは変更できません.' },
|
return render_unprocessable_entity('ニコタグは変更できません.', field: :category)
|
||||||
status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_names = params[:aliases].to_s.split.uniq
|
alias_names = params[:aliases].to_s.split.uniq
|
||||||
@@ -302,8 +301,7 @@ class TagsController < ApplicationController
|
|||||||
tag = Tag.find(params[:id])
|
tag = Tag.find(params[:id])
|
||||||
|
|
||||||
if tag.nico? || (category.present? && category == 'nico')
|
if tag.nico? || (category.present? && category == 'nico')
|
||||||
return render json: { error: 'ニコタグは変更できません.' },
|
return render_unprocessable_entity('ニコタグは変更できません.', field: :category)
|
||||||
status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ApplicationRecord.transaction do
|
ApplicationRecord.transaction do
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class TheatreCommentsController < ApplicationController
|
|||||||
return head :unauthorized unless current_user
|
return head :unauthorized unless current_user
|
||||||
|
|
||||||
content = params[:content]
|
content = params[:content]
|
||||||
return head :unprocessable_entity if content.blank?
|
return render_unprocessable_entity('本文は必須です.', field: :content) if content.blank?
|
||||||
|
|
||||||
theatre = Theatre.find_by(id: params[:theatre_id])
|
theatre = Theatre.find_by(id: params[:theatre_id])
|
||||||
return head :not_found unless theatre
|
return head :not_found unless theatre
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ class UsersController < ApplicationController
|
|||||||
return head :unauthorized if user&.id != params[:id].to_i
|
return head :unauthorized if user&.id != params[:id].to_i
|
||||||
|
|
||||||
name = params[:name]
|
name = params[:name]
|
||||||
return head :bad_request if name.blank?
|
return render_bad_request('名前は必須です.', field: :name) if name.blank?
|
||||||
|
|
||||||
if user.update(name:)
|
if user.update(name:)
|
||||||
render json: user.slice(:id, :name, :inheritance_code, :role), status: :ok
|
render json: user.slice(:id, :name, :inheritance_code, :role), status: :ok
|
||||||
else
|
else
|
||||||
render json: user.errors, status: :unprocessable_entity
|
render_model_errors(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class WikiPagesController < ApplicationController
|
|||||||
|
|
||||||
def diff
|
def diff
|
||||||
id = params[:id]
|
id = params[:id]
|
||||||
return head :bad_request if id.blank?
|
return render_bad_request('id は必須です.', field: :id) if id.blank?
|
||||||
|
|
||||||
from = params[:from].presence
|
from = params[:from].presence
|
||||||
to = params[:to].presence
|
to = params[:to].presence
|
||||||
@@ -56,7 +56,7 @@ class WikiPagesController < ApplicationController
|
|||||||
from_rev = from && page.wiki_revisions.find(from)
|
from_rev = from && page.wiki_revisions.find(from)
|
||||||
to_rev = to ? page.wiki_revisions.find(to) : page.current_revision
|
to_rev = to ? page.wiki_revisions.find(to) : page.current_revision
|
||||||
if ((from_rev && !(from_rev.content?)) || !(to_rev&.content?))
|
if ((from_rev && !(from_rev.content?)) || !(to_rev&.content?))
|
||||||
return head :unprocessable_entity
|
return render_unprocessable_entity('差分を表示できない版です.')
|
||||||
end
|
end
|
||||||
|
|
||||||
diffs = Diff::LCS.sdiff(from_rev&.body&.lines || [], to_rev.body.lines)
|
diffs = Diff::LCS.sdiff(from_rev&.body&.lines || [], to_rev.body.lines)
|
||||||
@@ -89,7 +89,8 @@ class WikiPagesController < ApplicationController
|
|||||||
body = params[:body].to_s
|
body = params[:body].to_s
|
||||||
message = params[:message].presence
|
message = params[:message].presence
|
||||||
|
|
||||||
return head :unprocessable_entity if title.blank? || body.blank?
|
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||||
|
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||||
|
|
||||||
tag_name = TagName.find_undiscard_or_create_by!(name: title)
|
tag_name = TagName.find_undiscard_or_create_by!(name: title)
|
||||||
|
|
||||||
@@ -101,8 +102,10 @@ class WikiPagesController < ApplicationController
|
|||||||
message:)
|
message:)
|
||||||
|
|
||||||
render json: WikiPageRepr.base(page), status: :created
|
render json: WikiPageRepr.base(page), status: :created
|
||||||
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
head :unprocessable_entity
|
render_model_errors(e.record)
|
||||||
|
rescue ActiveRecord::RecordNotUnique
|
||||||
|
render_record_not_unique
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@@ -112,7 +115,8 @@ class WikiPagesController < ApplicationController
|
|||||||
title = params[:title]&.strip
|
title = params[:title]&.strip
|
||||||
body = params[:body].to_s
|
body = params[:body].to_s
|
||||||
|
|
||||||
return head :unprocessable_entity if title.blank? || body.blank?
|
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||||
|
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||||
|
|
||||||
page = WikiPage.find(params[:id])
|
page = WikiPage.find(params[:id])
|
||||||
base_revision_id = params[:base_revision_id].presence
|
base_revision_id = params[:base_revision_id].presence
|
||||||
|
|||||||
@@ -56,38 +56,7 @@ class Post < ApplicationRecord
|
|||||||
super(options).merge(thumbnail: nil)
|
super(options).merge(thumbnail: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def snapshot_tag_names
|
def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
|
||||||
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) }-#{ 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
|
|
||||||
|
|
||||||
if h.positive?
|
|
||||||
'%d:%02d:%02d' % [h, min, s]
|
|
||||||
else
|
|
||||||
'%d:%02d' % [min, s]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def snapshot_parent_post_ids = parents.order(:id).pluck(:id)
|
def snapshot_parent_post_ids = parents.order(:id).pluck(:id)
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,6 @@ class PostTag < ApplicationRecord
|
|||||||
belongs_to :created_user, class_name: 'User', optional: true
|
belongs_to :created_user, class_name: 'User', optional: true
|
||||||
belongs_to :deleted_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 :post_id, presence: true
|
||||||
validates :tag_id, presence: true
|
validates :tag_id, presence: true
|
||||||
validates :post_id, uniqueness: {
|
validates :post_id, uniqueness: {
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
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, presence: true,
|
|
||||||
numericality: { only_integer: true, greater_than: :begin_ms }
|
|
||||||
end
|
|
||||||
@@ -92,50 +92,22 @@ class Tag < ApplicationRecord
|
|||||||
|
|
||||||
def self.normalise_tags! tag_names, with_tagme: true,
|
def self.normalise_tags! tag_names, with_tagme: true,
|
||||||
with_no_deerjikist: true,
|
with_no_deerjikist: true,
|
||||||
deny_nico: true,
|
deny_nico: true
|
||||||
with_sections: false
|
|
||||||
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') }
|
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') }
|
||||||
raise NicoTagNormalisationError
|
raise NicoTagNormalisationError
|
||||||
end
|
end
|
||||||
|
|
||||||
sections = { }
|
|
||||||
tags = tag_names.map do |name|
|
tags = tag_names.map do |name|
|
||||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.downcase.start_with?(p) } || ['', nil]
|
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 n = name.sub!(/^(\S*?)\[([0-9:.]*?)-([0-9:.]*?)\](\S*?)$/, '\1\4 \2 \3')
|
|
||||||
name, *section_raw = n.split
|
|
||||||
|
|
||||||
begin_ms, end_ms = section_raw.map { time_to_ms(_1) }
|
|
||||||
next if begin_ms == end_ms
|
|
||||||
|
|
||||||
begin_ms, end_ms = end_ms, begin_ms if begin_ms > end_ms
|
|
||||||
|
|
||||||
sections_by_tag << [begin_ms, end_ms]
|
|
||||||
end
|
|
||||||
|
|
||||||
name = TagName.canonicalise(name).first
|
|
||||||
|
|
||||||
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag|
|
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag|
|
||||||
tag.update!(category: cat) if cat && tag.category != cat
|
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme)
|
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 << 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
|
end
|
||||||
|
|
||||||
def self.expand_parent_tags tags
|
def self.expand_parent_tags tags
|
||||||
@@ -256,23 +228,4 @@ class Tag < ApplicationRecord
|
|||||||
errors.add :category, 'ニジラーと紐づいてゐるタグはニジラー・カテゴリである必要があります.'
|
errors.add :category, 'ニジラーと紐づいてゐるタグはニジラー・カテゴリである必要があります.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.time_to_ms str
|
|
||||||
parts = str.split(':')
|
|
||||||
|
|
||||||
s_part = parts.pop
|
|
||||||
s, ms = s_part.split('.')
|
|
||||||
|
|
||||||
total_s = s.to_i
|
|
||||||
|
|
||||||
if parts.length >= 1
|
|
||||||
total_s += parts.pop.to_i * 60
|
|
||||||
end
|
|
||||||
|
|
||||||
if parts.length >= 1
|
|
||||||
total_s += parts.pop.to_i * 3_600
|
|
||||||
end
|
|
||||||
|
|
||||||
total_s * 1_000 + ms.to_s.ljust(3, '0')[0, 3].to_i
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
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: false
|
|
||||||
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 'begin_ms < end_ms',
|
|
||||||
name: 'chk_post_tag_sections_end_ms_after_begin_ms'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
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
|
|
||||||
Generated
+1
-20
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[8.0].define(version: 2026_05_19_013100) do
|
ActiveRecord::Schema[8.0].define(version: 2026_05_07_213300) do
|
||||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
t.string "name", null: false
|
t.string "name", null: false
|
||||||
t.string "record_type", null: false
|
t.string "record_type", null: false
|
||||||
@@ -137,19 +137,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_19_013100) do
|
|||||||
t.index ["target_post_id"], name: "index_post_similarities_on_target_post_id"
|
t.index ["target_post_id"], name: "index_post_similarities_on_target_post_id"
|
||||||
end
|
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", null: false
|
|
||||||
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 "`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|
|
create_table "post_tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
t.bigint "post_id", null: false
|
t.bigint "post_id", null: false
|
||||||
t.bigint "tag_id", null: false
|
t.bigint "tag_id", null: false
|
||||||
@@ -200,11 +187,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_19_013100) do
|
|||||||
t.datetime "original_created_before"
|
t.datetime "original_created_before"
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.integer "version_no", 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 ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id"
|
||||||
t.index ["url"], name: "index_posts_on_url", unique: true
|
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"
|
t.check_constraint "`version_no` > 0", name: "chk_posts_version_no_positive"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -306,7 +290,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_19_013100) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.index ["expires_at"], name: "index_theatre_watching_users_on_expires_at"
|
t.index ["expires_at"], name: "index_theatre_watching_users_on_expires_at"
|
||||||
t.index ["theatre_id", "expires_at"], name: "idx_on_theatre_id_skip_expires_at_4c8de1dd42"
|
|
||||||
t.index ["theatre_id", "expires_at"], name: "index_theatre_watching_users_on_theatre_id_and_expires_at"
|
t.index ["theatre_id", "expires_at"], name: "index_theatre_watching_users_on_theatre_id_and_expires_at"
|
||||||
t.index ["theatre_id"], name: "index_theatre_watching_users_on_theatre_id"
|
t.index ["theatre_id"], name: "index_theatre_watching_users_on_theatre_id"
|
||||||
t.index ["user_id"], name: "index_theatre_watching_users_on_user_id"
|
t.index ["user_id"], name: "index_theatre_watching_users_on_user_id"
|
||||||
@@ -463,8 +446,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_19_013100) do
|
|||||||
add_foreign_key "post_implications", "posts", column: "parent_post_id"
|
add_foreign_key "post_implications", "posts", column: "parent_post_id"
|
||||||
add_foreign_key "post_similarities", "posts"
|
add_foreign_key "post_similarities", "posts"
|
||||||
add_foreign_key "post_similarities", "posts", column: "target_post_id"
|
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", "posts"
|
||||||
add_foreign_key "post_tags", "tags"
|
add_foreign_key "post_tags", "tags"
|
||||||
add_foreign_key "post_tags", "users", column: "created_user_id"
|
add_foreign_key "post_tags", "users", column: "created_user_id"
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
FactoryBot.define do
|
|
||||||
factory :post_tag_section do
|
|
||||||
association :post
|
|
||||||
association :tag
|
|
||||||
begin_ms { 1_000 }
|
|
||||||
end_ms { 2_000 }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
FactoryBot.define do
|
|
||||||
factory :post_tag do
|
|
||||||
association :post
|
|
||||||
association :tag
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
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,29 +0,0 @@
|
|||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'error responses', type: :request do
|
||||||
|
describe 'manual input errors' do
|
||||||
|
it 'returns a stable errors array for bad requests' do
|
||||||
|
user = create(:user)
|
||||||
|
sign_in_as(user)
|
||||||
|
|
||||||
|
put "/users/#{ user.id }", params: { name: ' ' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:bad_request)
|
||||||
|
expect(json.fetch('errors')).to contain_exactly(
|
||||||
|
include('code' => 'bad_request',
|
||||||
|
'field' => 'name',
|
||||||
|
'message' => be_present))
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a stable errors array for unprocessable requests' do
|
||||||
|
member = create(:user, :member)
|
||||||
|
tag = create(:tag, :general, name: 'error_response_tag')
|
||||||
|
sign_in_as(member)
|
||||||
|
|
||||||
|
patch "/tags/#{ tag.id }", params: { category: 'nico' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(json.fetch('errors')).to contain_exactly(
|
||||||
|
include('code' => 'invalid',
|
||||||
|
'field' => 'category',
|
||||||
|
'message' => be_present))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'model validation errors' do
|
||||||
|
it 'returns field, code, and message for model errors' do
|
||||||
|
user = create(:user)
|
||||||
|
sign_in_as(user)
|
||||||
|
|
||||||
|
put "/users/#{ user.id }", params: { name: 'a' * 256 }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(json.fetch('errors')).to include(
|
||||||
|
include('code' => 'too_long',
|
||||||
|
'field' => 'name',
|
||||||
|
'message' => be_present))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,34 +2,36 @@ import { useEffect, useState } from 'react'
|
|||||||
|
|
||||||
import PostFormTagsArea from '@/components/PostFormTagsArea'
|
import PostFormTagsArea from '@/components/PostFormTagsArea'
|
||||||
import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeField'
|
import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeField'
|
||||||
|
import FieldError from '@/components/common/FieldError'
|
||||||
import Label from '@/components/common/Label'
|
import Label from '@/components/common/Label'
|
||||||
import { useDialogue } from '@/components/dialogues/DialogueProvider'
|
import { useDialogue } from '@/components/dialogues/DialogueProvider'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { isApiError } from '@/lib/api'
|
import { isApiError } from '@/lib/api'
|
||||||
|
import { extractValidationError } from '@/lib/apiErrors'
|
||||||
import { updatePost } from '@/lib/posts'
|
import { updatePost } from '@/lib/posts'
|
||||||
import { msToTime } from '@/lib/utils'
|
|
||||||
|
|
||||||
import type { FC, FormEvent } from 'react'
|
import type { FC, FormEvent } from 'react'
|
||||||
|
|
||||||
import type { Post, TagWithSections } from '@/types'
|
import type { FieldErrors } from '@/lib/apiErrors'
|
||||||
|
import type { Post, Tag } from '@/types'
|
||||||
|
|
||||||
|
type PostFormField =
|
||||||
|
'parentPostId' | 'tags' | 'originalCreatedFrom' | 'originalCreatedBefore'
|
||||||
|
|
||||||
|
|
||||||
const tagsToStr = (tags: TagWithSections[]): string => {
|
const tagsToStr = (tags: Tag[]): string => {
|
||||||
const result: Omit<TagWithSections, 'children'>[] = []
|
const result: Tag[] = []
|
||||||
|
|
||||||
const walk = (tag: TagWithSections) => {
|
const walk = (tag: Tag) => {
|
||||||
const { children, ...rest } = tag
|
const { children, ...rest } = tag
|
||||||
result.push (rest)
|
result.push (rest)
|
||||||
children.forEach (walk)
|
children?.forEach (walk)
|
||||||
}
|
}
|
||||||
|
|
||||||
tags.filter (t => t.category !== 'nico').forEach (walk)
|
tags.filter (t => t.category !== 'nico').forEach (walk)
|
||||||
|
|
||||||
return [...(new Set (result.map (t =>
|
return [...(new Set (result.map (t => t.name)))].join (' ')
|
||||||
`${ t.name }${ t.sections
|
|
||||||
.map (s => `[${ msToTime (s.beginMs) }-${ msToTime (s.endMs) }]`)
|
|
||||||
.join ('') }`)))].join (' ')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,7 +40,9 @@ type Props = { post: Post
|
|||||||
|
|
||||||
|
|
||||||
const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
||||||
|
const [baseErrors, setBaseErrors] = useState<string[]> ([])
|
||||||
const [disabled, setDisabled] = useState (false)
|
const [disabled, setDisabled] = useState (false)
|
||||||
|
const [fieldErrors, setFieldErrors] = useState<FieldErrors> ({ })
|
||||||
const [originalCreatedBefore, setOriginalCreatedBefore] =
|
const [originalCreatedBefore, setOriginalCreatedBefore] =
|
||||||
useState<string | null> (post.originalCreatedBefore)
|
useState<string | null> (post.originalCreatedBefore)
|
||||||
const [originalCreatedFrom, setOriginalCreatedFrom] =
|
const [originalCreatedFrom, setOriginalCreatedFrom] =
|
||||||
@@ -51,6 +55,9 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
const dialogue = useDialogue ()
|
const dialogue = useDialogue ()
|
||||||
|
|
||||||
const update = async (...args: Parameters<typeof updatePost>) => {
|
const update = async (...args: Parameters<typeof updatePost>) => {
|
||||||
|
setFieldErrors ({ })
|
||||||
|
setBaseErrors ([])
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const data = await updatePost (...args)
|
const data = await updatePost (...args)
|
||||||
@@ -71,7 +78,18 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
|
|
||||||
if (response?.status !== 409)
|
if (response?.status !== 409)
|
||||||
{
|
{
|
||||||
toast ({ description: '更新はできなかったよ……' })
|
const validationError = extractValidationError (e)
|
||||||
|
|
||||||
|
if (validationError)
|
||||||
|
{
|
||||||
|
setFieldErrors (validationError.fieldErrors)
|
||||||
|
setBaseErrors (validationError.baseErrors)
|
||||||
|
toast ({ description: '更新はできなかったよ……' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
toast ({ title: '失敗……', description: '入力を確認してください.' })
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +139,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
setTags (tagsToStr (post.tags))
|
setTags(tagsToStr (post.tags))
|
||||||
}, [post])
|
}, [post])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -146,13 +164,15 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
value={parentPostIds}
|
value={parentPostIds}
|
||||||
onChange={e => setParentPostIds (e.target.value)}
|
onChange={e => setParentPostIds (e.target.value)}
|
||||||
className="w-full border p-2 rounded"/>
|
className="w-full border p-2 rounded"/>
|
||||||
|
<FieldError messages={fieldErrors.url}/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* タグ */}
|
{/* タグ */}
|
||||||
<PostFormTagsArea
|
<PostFormTagsArea
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
tags={tags}
|
tags={tags}
|
||||||
setTags={setTags}/>
|
setTags={setTags}
|
||||||
|
errors={fieldErrors.tags}/>
|
||||||
|
|
||||||
{/* オリジナルの作成日時 */}
|
{/* オリジナルの作成日時 */}
|
||||||
<PostOriginalCreatedTimeField
|
<PostOriginalCreatedTimeField
|
||||||
@@ -160,7 +180,9 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
originalCreatedFrom={originalCreatedFrom}
|
originalCreatedFrom={originalCreatedFrom}
|
||||||
setOriginalCreatedFrom={setOriginalCreatedFrom}
|
setOriginalCreatedFrom={setOriginalCreatedFrom}
|
||||||
originalCreatedBefore={originalCreatedBefore}
|
originalCreatedBefore={originalCreatedBefore}
|
||||||
setOriginalCreatedBefore={setOriginalCreatedBefore}/>
|
setOriginalCreatedBefore={setOriginalCreatedBefore}
|
||||||
|
fromErrors={fieldErrors.originalCreatedFrom}
|
||||||
|
beforeErrors={fieldErrors.originalCreatedBefore}/>
|
||||||
|
|
||||||
{/* 送信 */}
|
{/* 送信 */}
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import { useRef, useState } from 'react'
|
import { useRef, useState } from 'react'
|
||||||
|
|
||||||
import TagSearchBox from '@/components/TagSearchBox'
|
import TagSearchBox from '@/components/TagSearchBox'
|
||||||
|
import FieldError from '@/components/common/FieldError'
|
||||||
import Label from '@/components/common/Label'
|
import Label from '@/components/common/Label'
|
||||||
import TextArea from '@/components/common/TextArea'
|
import TextArea from '@/components/common/TextArea'
|
||||||
import { apiGet } from '@/lib/api'
|
import { apiGet } from '@/lib/api'
|
||||||
@@ -33,10 +34,11 @@ const replaceToken = (value: string, start: number, end: number, text: string) =
|
|||||||
|
|
||||||
type Props = Omit<ComponentPropsWithoutRef<'textarea'>, 'value' | 'onChange' | 'onBlur'> & {
|
type Props = Omit<ComponentPropsWithoutRef<'textarea'>, 'value' | 'onChange' | 'onBlur'> & {
|
||||||
tags: string
|
tags: string
|
||||||
setTags: (tags: string) => void }
|
setTags: (tags: string) => void
|
||||||
|
errors?: string[] }
|
||||||
|
|
||||||
|
|
||||||
const PostFormTagsArea: FC<Props> = ({ tags, setTags, ...rest }) => {
|
const PostFormTagsArea: FC<Props> = ({ tags, setTags, errors, ...rest }) => {
|
||||||
const ref = useRef<HTMLTextAreaElement> (null)
|
const ref = useRef<HTMLTextAreaElement> (null)
|
||||||
|
|
||||||
const [bounds, setBounds] = useState<{ start: number; end: number }> ({ start: 0, end: 0 })
|
const [bounds, setBounds] = useState<{ start: number; end: number }> ({ start: 0, end: 0 })
|
||||||
@@ -96,6 +98,7 @@ const PostFormTagsArea: FC<Props> = ({ tags, setTags, ...rest }) => {
|
|||||||
: [] as Tag[]}
|
: [] as Tag[]}
|
||||||
activeIndex={-1}
|
activeIndex={-1}
|
||||||
onSelect={handleTagSelect}/>)}
|
onSelect={handleTagSelect}/>)}
|
||||||
|
<FieldError messages={errors}/>
|
||||||
</div>)
|
</div>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ type Props = {
|
|||||||
originalCreatedFrom: string | null
|
originalCreatedFrom: string | null
|
||||||
setOriginalCreatedFrom: (x: string | null) => void
|
setOriginalCreatedFrom: (x: string | null) => void
|
||||||
originalCreatedBefore: string | null
|
originalCreatedBefore: string | null
|
||||||
setOriginalCreatedBefore: (x: string | null) => void }
|
setOriginalCreatedBefore: (x: string | null) => void
|
||||||
|
fromErrors?: string[]
|
||||||
|
beforeErrors?: string[] }
|
||||||
|
|
||||||
|
|
||||||
const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
||||||
@@ -19,6 +21,7 @@ const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
|||||||
setOriginalCreatedBefore }) => (
|
setOriginalCreatedBefore }) => (
|
||||||
<div>
|
<div>
|
||||||
<Label>オリジナルの作成日時</Label>
|
<Label>オリジナルの作成日時</Label>
|
||||||
|
|
||||||
<div className="my-1 flex">
|
<div className="my-1 flex">
|
||||||
<div className="w-80">
|
<div className="w-80">
|
||||||
<DateTimeField
|
<DateTimeField
|
||||||
@@ -51,6 +54,8 @@ const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<FieldError messages={fromErrors}/>
|
||||||
|
|
||||||
<div className="my-1 flex">
|
<div className="my-1 flex">
|
||||||
<div className="w-80">
|
<div className="w-80">
|
||||||
<DateTimeField
|
<DateTimeField
|
||||||
@@ -71,6 +76,7 @@ const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<FieldError messages={beforeErrors}/>
|
||||||
</div>)
|
</div>)
|
||||||
|
|
||||||
export default PostOriginalCreatedTimeField
|
export default PostOriginalCreatedTimeField
|
||||||
@@ -26,13 +26,13 @@ import { dateString, originalCreatedAtString } from '@/lib/utils'
|
|||||||
import type { DragEndEvent } from '@dnd-kit/core'
|
import type { DragEndEvent } from '@dnd-kit/core'
|
||||||
import type { FC, MutableRefObject, ReactNode } from 'react'
|
import type { FC, MutableRefObject, ReactNode } from 'react'
|
||||||
|
|
||||||
import type { Category, Post, TagWithSections } from '@/types'
|
import type { Category, Post, Tag } from '@/types'
|
||||||
|
|
||||||
type TagByCategory = { [key in Category]: TagWithSections[] }
|
type TagByCategory = { [key in Category]: Tag[] }
|
||||||
|
|
||||||
|
|
||||||
const renderTagTree = (
|
const renderTagTree = (
|
||||||
tag: TagWithSections,
|
tag: Tag,
|
||||||
nestLevel: number,
|
nestLevel: number,
|
||||||
path: string,
|
path: string,
|
||||||
suppressClickRef: MutableRefObject<boolean>,
|
suppressClickRef: MutableRefObject<boolean>,
|
||||||
@@ -63,7 +63,7 @@ const renderTagTree = (
|
|||||||
|
|
||||||
|
|
||||||
const isDescendant = (
|
const isDescendant = (
|
||||||
root: TagWithSections,
|
root: Tag,
|
||||||
targetId: number,
|
targetId: number,
|
||||||
): boolean => {
|
): boolean => {
|
||||||
if (!(root.children))
|
if (!(root.children))
|
||||||
@@ -84,8 +84,8 @@ const isDescendant = (
|
|||||||
const findTag = (
|
const findTag = (
|
||||||
byCat: TagByCategory,
|
byCat: TagByCategory,
|
||||||
id: number,
|
id: number,
|
||||||
): TagWithSections | undefined => {
|
): Tag | undefined => {
|
||||||
const walk = (nodes: TagWithSections[]): TagWithSections | undefined => {
|
const walk = (nodes: Tag[]): Tag | undefined => {
|
||||||
for (const t of nodes)
|
for (const t of nodes)
|
||||||
{
|
{
|
||||||
if (t.id === id)
|
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)[])
|
for (const cat of Object.keys (tagsTmp) as (keyof typeof tagsTmp)[])
|
||||||
tagsTmp[cat].sort ((tagA: TagWithSections, tagB: TagWithSections) => tagA.name < tagB.name ? -1 : 1)
|
tagsTmp[cat].sort ((tagA: Tag, tagB: Tag) => tagA.name < tagB.name ? -1 : 1)
|
||||||
|
|
||||||
return tagsTmp
|
return tagsTmp
|
||||||
}, [post])
|
}, [post])
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { FC } from 'react'
|
||||||
|
|
||||||
|
type Props = { messages?: string[] }
|
||||||
|
|
||||||
|
|
||||||
|
export const FieldError: FC<Props> = ({ messages }: Props) => {
|
||||||
|
if (!(messages?.length))
|
||||||
|
return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className="mt-1 space-y-1 text-red-600 dark:text-red-400">
|
||||||
|
{messages.map ((message, i) => <li key={i}>{message}</li>)}
|
||||||
|
</ul>)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default FieldError
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import toCamel from 'camelcase-keys'
|
||||||
|
|
||||||
|
import { isApiError } from '@/lib/api'
|
||||||
|
|
||||||
|
export type FieldErrors = Partial<Record<string, string[]>>
|
||||||
|
|
||||||
|
export type ValidationError = { message: string
|
||||||
|
fieldErrors: FieldErrors
|
||||||
|
baseErrors: string[] }
|
||||||
|
|
||||||
|
type RawValidationError = { type?: string
|
||||||
|
message?: string
|
||||||
|
errors?: Record<string, string[]>
|
||||||
|
baseErrors?: string[] }
|
||||||
|
|
||||||
|
|
||||||
|
export const extractValidationError = (err: unknown) => {
|
||||||
|
if (!(isApiError (err)) || err.response?.status !== 422)
|
||||||
|
return null
|
||||||
|
|
||||||
|
const data = toCamel ((err.response.data ?? { }) as Record<string, unknown>,
|
||||||
|
{ deep: true }) as RawValidationError
|
||||||
|
|
||||||
|
if (data.type !== 'validation_error' && !(data.errors))
|
||||||
|
return null
|
||||||
|
|
||||||
|
return { message: data.message ?? '入力内容を確認してください.',
|
||||||
|
fieldErrors: (data.errors ?? { }) as FieldErrors,
|
||||||
|
baseErrors: data.baseErrors ?? [] }
|
||||||
|
}
|
||||||
@@ -71,15 +71,3 @@ export const originalCreatedAtString = (
|
|||||||
.join (' '))
|
.join (' '))
|
||||||
return rtn === '〜' ? '年月日不詳' : rtn
|
return rtn === '〜' ? '年月日不詳' : rtn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
return (h > 0
|
|
||||||
? `${ h }:${ min.padStart (2, '0') }:${ s.padStart (2, '0') }`
|
|
||||||
: `${ min }:${ s.padStart (2, '0') }`)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { Helmet } from 'react-helmet-async'
|
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
|
|
||||||
import TagLink from '@/components/TagLink'
|
import TagLink from '@/components/TagLink'
|
||||||
@@ -70,11 +69,6 @@ const DeerjikistDetailPage: FC = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<MainArea>
|
<MainArea>
|
||||||
<Helmet>
|
|
||||||
<meta name="robots" content="noindex"/>
|
|
||||||
{tag && <title>{tag.name} | ニジラー情報</title>}
|
|
||||||
</Helmet>
|
|
||||||
|
|
||||||
{(loading || !(tag)) ? 'Loading...' : (
|
{(loading || !(tag)) ? 'Loading...' : (
|
||||||
<div className="max-w-xl">
|
<div className="max-w-xl">
|
||||||
<PageTitle>
|
<PageTitle>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { motion } from 'framer-motion'
|
|||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { Helmet } from 'react-helmet-async'
|
import { Helmet } from 'react-helmet-async'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
|
|
||||||
import PostEditForm from '@/components/PostEditForm'
|
import PostEditForm from '@/components/PostEditForm'
|
||||||
import PostEmbed from '@/components/PostEmbed'
|
import PostEmbed from '@/components/PostEmbed'
|
||||||
import PostList from '@/components/PostList'
|
import PostList from '@/components/PostList'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Material, Post, TagWithSections, User, WikiPage } from '@/types'
|
import type { Material, Post, Tag, User, WikiPage } from '@/types'
|
||||||
|
|
||||||
export const buildTag = (overrides: Partial<TagWithSections> = {}): TagWithSections => ({
|
export const buildTag = (overrides: Partial<Tag> = {}): Tag => ({
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'テストタグ',
|
name: 'テストタグ',
|
||||||
category: 'general',
|
category: 'general',
|
||||||
@@ -13,8 +13,6 @@ export const buildTag = (overrides: Partial<TagWithSections> = {}): TagWithSecti
|
|||||||
materialId: null,
|
materialId: null,
|
||||||
hasDeerjikists: false,
|
hasDeerjikists: false,
|
||||||
matchedAlias: null,
|
matchedAlias: null,
|
||||||
sections: [],
|
|
||||||
children: [],
|
|
||||||
...overrides,
|
...overrides,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ export type Post = {
|
|||||||
title: string | null
|
title: string | null
|
||||||
thumbnail: string | null
|
thumbnail: string | null
|
||||||
thumbnailBase: string | null
|
thumbnailBase: string | null
|
||||||
tags: TagWithSections[]
|
tags: Tag[]
|
||||||
parentPosts?: Post[]
|
parentPosts?: Post[]
|
||||||
childPosts?: Post[]
|
childPosts?: Post[]
|
||||||
siblingPosts?: Record<`${ number }`, Post[]>
|
siblingPosts?: Record<`${ number }`, Post[]>
|
||||||
@@ -187,6 +187,7 @@ export type Tag = {
|
|||||||
hasWiki: boolean
|
hasWiki: boolean
|
||||||
materialId: number | null
|
materialId: number | null
|
||||||
hasDeerjikists: boolean
|
hasDeerjikists: boolean
|
||||||
|
children?: Tag[]
|
||||||
matchedAlias?: string | null }
|
matchedAlias?: string | null }
|
||||||
|
|
||||||
export type TagVersion = {
|
export type TagVersion = {
|
||||||
@@ -200,10 +201,6 @@ export type TagVersion = {
|
|||||||
createdAt: string
|
createdAt: string
|
||||||
createdByUser: { id: number; name: string | null } | null }
|
createdByUser: { id: number; name: string | null } | null }
|
||||||
|
|
||||||
export type TagWithSections = Tag & { sections: { beginMs: number
|
|
||||||
endMs: number }[]
|
|
||||||
children: TagWithSections[] }
|
|
||||||
|
|
||||||
export type Theatre = {
|
export type Theatre = {
|
||||||
id: number
|
id: number
|
||||||
name: string | null
|
name: string | null
|
||||||
|
|||||||
Reference in New Issue
Block a user