フォームのバリデーションとニコ連携の画面変更 (#090) #355
@@ -43,25 +43,12 @@ class ApplicationController < ActionController::API
|
|||||||
render json: { errors: [error] }, status:
|
render json: { errors: [error] }, status:
|
||||||
end
|
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
|
def render_record_invalid error
|
||||||
render_model_errors(error.record)
|
render_validation_error error.record
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_record_not_unique _error = nil
|
def render_record_not_unique _error = nil
|
||||||
render_unprocessable_entity('既に存在してゐます.', code: :taken)
|
render_validation_error base: ['すでに存在してゐます.']
|
||||||
end
|
end
|
||||||
|
|
||||||
def reject_banned_ip_address!
|
def reject_banned_ip_address!
|
||||||
@@ -77,27 +64,27 @@ class ApplicationController < ActionController::API
|
|||||||
head :forbidden
|
head :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_validation_error record = nil, fields: { }, base: []
|
def render_validation_error record = nil, fields: { }, base: [], status: :unprocessable_entity
|
||||||
errors = { }
|
errors = { }
|
||||||
|
|
||||||
if record
|
if record
|
||||||
record.errors.messages.each do |attr, messages|
|
record.errors.each do |error|
|
||||||
errors[attr] ||= []
|
errors[error.attribute] ||= []
|
||||||
errors[attr].concat(messages)
|
errors[error.attribute] << error.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fields.each do |attr, messages|
|
fields.each do |attr, messages|
|
||||||
errors[attr] ||= []
|
errors[attr.to_sym] ||= []
|
||||||
errors[attr].concat(Array(messages))
|
errors[attr.to_sym].concat(Array(messages))
|
||||||
end
|
end
|
||||||
|
|
||||||
base_errors = Array(base) - Array(errors.delete(:base))
|
base_errors = Array(base) + Array(errors.delete(:base))
|
||||||
|
|
||||||
render json: { type: 'validation_error',
|
render json: { type: 'validation_error',
|
||||||
message: '入力内容を確認してください.',
|
message: '入力内容を確認してください.',
|
||||||
errors:,
|
errors:,
|
||||||
base_errors: },
|
base_errors: },
|
||||||
status: :unprocessable_entity
|
status:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class PostsController < ApplicationController
|
|||||||
filtered_posts
|
filtered_posts
|
||||||
.joins("LEFT JOIN (#{ pt_max_sql }) pt_max ON pt_max.post_id = posts.id")
|
.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"))
|
.reselect('posts.*', Arel.sql("#{ updated_at_all_sql } AS updated_at_all"))
|
||||||
.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
.preload(:uploaded_user, tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||||
.with_attached_thumbnail
|
.with_attached_thumbnail
|
||||||
|
|
||||||
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
||||||
@@ -95,7 +95,9 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def random
|
def random
|
||||||
post = filtered_posts.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
post = filtered_posts.preload(:uploaded_user,
|
||||||
|
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||||
|
.with_attached_thumbnail
|
||||||
.order('RAND()')
|
.order('RAND()')
|
||||||
.first
|
.first
|
||||||
return head :not_found unless post
|
return head :not_found unless post
|
||||||
@@ -104,12 +106,24 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
post = Post.includes(tags: [:deerjikists, :materials, { tag_name: :wiki_page }]).find_by(id: params[:id])
|
post =
|
||||||
|
Post
|
||||||
|
.includes(:uploaded_user, tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||||
|
.with_attached_thumbnail
|
||||||
|
.find_by(id: params[:id])
|
||||||
return head :not_found unless post
|
return head :not_found unless post
|
||||||
|
|
||||||
render json: PostRepr.base(post, current_user)
|
parent_posts = post.parents.with_attached_thumbnail.order(:id).to_a
|
||||||
.merge(tags: build_tag_tree_for(post.tags),
|
child_posts = post.children.with_attached_thumbnail.order(:id).to_a
|
||||||
related: PostRepr.many(post.related(limit: 20)))
|
sibling_posts = sibling_posts_by_parent(parent_posts.map(&:id))
|
||||||
|
related = post.related(limit: 20).to_a
|
||||||
|
|
||||||
|
render json: PostRepr.detail(post, current_user,
|
||||||
|
parent_posts:,
|
||||||
|
child_posts:,
|
||||||
|
sibling_posts:,
|
||||||
|
related:)
|
||||||
|
.merge(tags: build_tag_tree_for(post.tags))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@@ -148,11 +162,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
|
||||||
render_bad_request('ニコニコ・タグは直接指定できません.', field: :tags)
|
render_validation_error fields: { tags: 'ニコニコ・タグは直接指定できません.' }
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
render_unprocessable_entity(e.message)
|
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||||
rescue ActiveRecord::RecordInvalid => e
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
render_model_errors(e.record)
|
render_post_form_record_invalid e.record
|
||||||
end
|
end
|
||||||
|
|
||||||
def viewed
|
def viewed
|
||||||
@@ -238,11 +252,11 @@ class PostsController < ApplicationController
|
|||||||
json['tags'] = build_tag_tree_for(post.tags)
|
json['tags'] = build_tag_tree_for(post.tags)
|
||||||
render json:, status: :ok
|
render json:, status: :ok
|
||||||
rescue Tag::NicoTagNormalisationError
|
rescue Tag::NicoTagNormalisationError
|
||||||
render_bad_request('ニコニコ・タグは直接指定できません.', field: :tags)
|
render_validation_error fields: { tags: ['ニコニコ・タグは直接指定できません.'] }
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
render_validation_error(fields: { parent_post_ids: [e.message] })
|
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||||
rescue ActiveRecord::RecordInvalid => e
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
render_validation_error(e.record)
|
render_post_form_record_invalid e.record
|
||||||
end
|
end
|
||||||
|
|
||||||
def changes
|
def changes
|
||||||
@@ -385,7 +399,7 @@ class PostsController < ApplicationController
|
|||||||
return nil unless tag
|
return nil unless tag
|
||||||
|
|
||||||
if path.include?(tag_id)
|
if path.include?(tag_id)
|
||||||
return TagRepr.base(tag).merge(children: [])
|
return TagRepr.inline(tag).merge(children: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
if memo.key?(tag_id)
|
if memo.key?(tag_id)
|
||||||
@@ -397,12 +411,26 @@ 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:)
|
memo[tag_id] = TagRepr.inline(tag).merge(children:)
|
||||||
end
|
end
|
||||||
|
|
||||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sibling_posts_by_parent parent_post_ids
|
||||||
|
return { } if parent_post_ids.blank?
|
||||||
|
|
||||||
|
implications =
|
||||||
|
PostImplication
|
||||||
|
.where(parent_post_id: parent_post_ids)
|
||||||
|
.includes(post: { thumbnail_attachment: :blob })
|
||||||
|
.order(:parent_post_id, :post_id)
|
||||||
|
|
||||||
|
implications.group_by(&:parent_post_id).transform_values { |items|
|
||||||
|
items.map(&:post)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def parse_parent_post_ids
|
def parse_parent_post_ids
|
||||||
raise ArgumentError, 'parent_post_ids は必須です.' unless params.key?(:parent_post_ids)
|
raise ArgumentError, 'parent_post_ids は必須です.' unless params.key?(:parent_post_ids)
|
||||||
|
|
||||||
@@ -416,7 +444,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(:parent_post_ids, '自分自身を親投稿にはできません.')
|
post.errors.add :parent_post_ids, '自分自身を親投稿にはできません.'
|
||||||
raise ActiveRecord::RecordInvalid, post
|
raise ActiveRecord::RecordInvalid, post
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -424,7 +452,8 @@ class PostsController < ApplicationController
|
|||||||
missing_ids = parent_post_ids - existing_ids
|
missing_ids = parent_post_ids - existing_ids
|
||||||
|
|
||||||
if missing_ids.present?
|
if missing_ids.present?
|
||||||
post.errors.add(:base, "存在しない親投稿 ID があります: #{ missing_ids.join(' ') }")
|
post.errors.add :parent_post_ids,
|
||||||
|
"存在しない親投稿 ID があります: #{ missing_ids.join(' ') }"
|
||||||
raise ActiveRecord::RecordInvalid, post
|
raise ActiveRecord::RecordInvalid, post
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -640,4 +669,14 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
merged.uniq.sort
|
merged.uniq.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_post_form_record_invalid record
|
||||||
|
if e.record.is_a?(TagName) || e.record.is_a?(Tag)
|
||||||
|
render_validation_error(fields: { tags: e.record.errors.full_messages.map { |message|
|
||||||
|
"タグ名 “#{ e.record.name }”: #{ message }"
|
||||||
|
} })
|
||||||
|
else
|
||||||
|
render_validation_error(record)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class Post < ApplicationRecord
|
|||||||
return if !(f) || !(b)
|
return if !(f) || !(b)
|
||||||
|
|
||||||
if f >= b
|
if f >= b
|
||||||
errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.'
|
errors.add :original_created_at, 'オリジナルの作成日時の順番がをかしぃです.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,65 @@
|
|||||||
|
|
||||||
|
|
||||||
module PostRepr
|
module PostRepr
|
||||||
BASE = { include: { tags: TagRepr::BASE, uploaded_user: UserRepr::BASE },
|
BASE_FIELDS = [
|
||||||
methods: [:parent_posts, :child_posts, :sibling_posts] }.freeze
|
:id,
|
||||||
|
:version_no,
|
||||||
|
:url,
|
||||||
|
:title,
|
||||||
|
:thumbnail_base,
|
||||||
|
:original_created_from,
|
||||||
|
:original_created_before,
|
||||||
|
:created_at,
|
||||||
|
:updated_at
|
||||||
|
].freeze
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def base post, current_user = nil
|
def base post, current_user = nil
|
||||||
json = post.as_json(BASE)
|
json = common(post)
|
||||||
return json.merge(viewed: false) unless current_user
|
json['tags'] = tag_json(post.tags)
|
||||||
|
json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user)
|
||||||
|
json['viewed'] = current_user ? current_user.viewed?(post) : false
|
||||||
|
json
|
||||||
|
end
|
||||||
|
|
||||||
viewed = current_user.viewed?(post)
|
def detail post, current_user = nil, parent_posts: [], child_posts: [],
|
||||||
json.merge(viewed:)
|
sibling_posts: { }, related: []
|
||||||
|
base(post, current_user).merge(
|
||||||
|
'parent_posts' => cards(parent_posts),
|
||||||
|
'child_posts' => cards(child_posts),
|
||||||
|
'sibling_posts' => sibling_posts.transform_keys(&:to_s).transform_values { |posts|
|
||||||
|
cards(posts)
|
||||||
|
},
|
||||||
|
'related' => cards(related))
|
||||||
|
end
|
||||||
|
|
||||||
|
def card post
|
||||||
|
common(post).merge('parent_posts' => [], 'child_posts' => [])
|
||||||
|
end
|
||||||
|
|
||||||
|
def cards posts
|
||||||
|
posts.map { |post| card(post) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def many posts, current_user = nil
|
def many posts, current_user = nil
|
||||||
posts.map { |p| base(p, current_user) }
|
posts.map { |p| base(p, current_user) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def common post
|
||||||
|
BASE_FIELDS.to_h { |field| [field.to_s, post.public_send(field)] }
|
||||||
|
.merge('thumbnail' => thumbnail_url(post))
|
||||||
|
end
|
||||||
|
|
||||||
|
def tag_json tags
|
||||||
|
tags.map { |tag| TagRepr.inline(tag) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def thumbnail_url post
|
||||||
|
return nil unless post.thumbnail.attached?
|
||||||
|
|
||||||
|
Rails.application.routes.url_helpers.rails_blob_url(post.thumbnail, only_path: false)
|
||||||
|
rescue
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,5 +12,9 @@ module TagRepr
|
|||||||
parents: tag.parents.map { _1.as_json(BASE) })
|
parents: tag.parents.map { _1.as_json(BASE) })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inline tag
|
||||||
|
tag.as_json(BASE).merge(aliases: [], parents: [])
|
||||||
|
end
|
||||||
|
|
||||||
def many(tags) = tags.map { |t| base(t) }
|
def many(tags) = tags.map { |t| base(t) }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -57,6 +57,23 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
post_write_params({ base_version_no: base_version.version_no }.merge(params))
|
post_write_params({ base_version_no: base_version.version_no }.merge(params))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def count_sql_queries
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
callback = lambda do |_name, _started, _finished, _id, payload|
|
||||||
|
next if payload[:cached]
|
||||||
|
next if ['SCHEMA', 'TRANSACTION'].include?(payload[:name])
|
||||||
|
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
|
||||||
|
count
|
||||||
|
end
|
||||||
|
|
||||||
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
||||||
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
|
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
|
||||||
|
|
||||||
@@ -558,6 +575,59 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
expect(sibling_ids).to include(sibling_post.id)
|
expect(sibling_ids).to include(sibling_post.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not issue a query per tag or related post' do
|
||||||
|
user = create_member_user!
|
||||||
|
|
||||||
|
tags =
|
||||||
|
15.times.map do |i|
|
||||||
|
tag_name = TagName.create!(name: "show_query_tag_#{ i }")
|
||||||
|
tag = Tag.create!(tag_name:, category: :general)
|
||||||
|
TagName.create!(name: "show_query_alias_#{ i }", canonical: tag_name)
|
||||||
|
PostTag.create!(post: post_record, tag:)
|
||||||
|
tag
|
||||||
|
end
|
||||||
|
|
||||||
|
tags.each_cons(2) do |parent_tag, child_tag|
|
||||||
|
TagImplication.create!(parent_tag:, tag: child_tag)
|
||||||
|
end
|
||||||
|
|
||||||
|
parent_post = Post.create!(
|
||||||
|
title: 'query parent post',
|
||||||
|
url: 'https://example.com/query-parent-post'
|
||||||
|
)
|
||||||
|
sibling_post = Post.create!(
|
||||||
|
title: 'query sibling post',
|
||||||
|
url: 'https://example.com/query-sibling-post'
|
||||||
|
)
|
||||||
|
child_post = Post.create!(
|
||||||
|
title: 'query child post',
|
||||||
|
url: 'https://example.com/query-child-post'
|
||||||
|
)
|
||||||
|
|
||||||
|
PostImplication.create!(post: post_record, parent_post:)
|
||||||
|
PostImplication.create!(post: sibling_post, parent_post:)
|
||||||
|
PostImplication.create!(post: child_post, parent_post: post_record)
|
||||||
|
|
||||||
|
20.times do |i|
|
||||||
|
related_post = Post.create!(
|
||||||
|
title: "query related post #{ i }",
|
||||||
|
url: "https://example.com/query-related-post-#{ i }"
|
||||||
|
)
|
||||||
|
PostSimilarity.create!(post: post_record,
|
||||||
|
target_post: related_post,
|
||||||
|
cos: 1.0 - (i / 100.0))
|
||||||
|
end
|
||||||
|
|
||||||
|
query_count =
|
||||||
|
count_sql_queries do
|
||||||
|
get "/posts/#{ post_record.id }",
|
||||||
|
headers: { 'X-Transfer-Code' => user.inheritance_code }
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(query_count).to be <= 45
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when post does not exist' do
|
context 'when post does not exist' do
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { toast } from '@/components/ui/use-toast'
|
|||||||
import { isApiError } from '@/lib/api'
|
import { isApiError } from '@/lib/api'
|
||||||
import { extractValidationError } from '@/lib/apiErrors'
|
import { extractValidationError } from '@/lib/apiErrors'
|
||||||
import { updatePost } from '@/lib/posts'
|
import { updatePost } from '@/lib/posts'
|
||||||
|
import { inputClass } from '@/lib/utils'
|
||||||
|
|
||||||
import type { FC, FormEvent } from 'react'
|
import type { FC, FormEvent } from 'react'
|
||||||
|
|
||||||
@@ -150,21 +151,25 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
className="w-full border rounded p-2"
|
className={inputClass ()}
|
||||||
value={title ?? ''}
|
value={title ?? ''}
|
||||||
onChange={ev => setTitle (ev.target.value)}/>
|
onChange={ev => setTitle (ev.target.value)}/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 親投稿 */}
|
{/* 親投稿 */}
|
||||||
<div>
|
<div>
|
||||||
<Label>親投稿</Label>
|
<Label invalid={fieldErrors.parentPostIds && fieldErrors.parentPostIds.length > 0}>
|
||||||
|
親投稿
|
||||||
|
</Label>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
value={parentPostIds}
|
value={parentPostIds}
|
||||||
onChange={e => setParentPostIds (e.target.value)}
|
onChange={e => setParentPostIds (e.target.value)}
|
||||||
className="w-full border p-2 rounded"/>
|
alia-invalid={fieldErrors.parentPostIds && fieldErrors.parentPostIds.length > 0}
|
||||||
<FieldError messages={fieldErrors.url}/>
|
className={inputClass (fieldErrors.parentPostIds
|
||||||
|
&& fieldErrors.parentPostIds.length > 0)}/>
|
||||||
|
<FieldError messages={fieldErrors.parentPostIds}/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* タグ */}
|
{/* タグ */}
|
||||||
@@ -181,8 +186,7 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
setOriginalCreatedFrom={setOriginalCreatedFrom}
|
setOriginalCreatedFrom={setOriginalCreatedFrom}
|
||||||
originalCreatedBefore={originalCreatedBefore}
|
originalCreatedBefore={originalCreatedBefore}
|
||||||
setOriginalCreatedBefore={setOriginalCreatedBefore}
|
setOriginalCreatedBefore={setOriginalCreatedBefore}
|
||||||
fromErrors={fieldErrors.originalCreatedFrom}
|
errors={fieldErrors.originalCreatedAt}/>
|
||||||
beforeErrors={fieldErrors.originalCreatedBefore}/>
|
|
||||||
|
|
||||||
{/* 送信 */}
|
{/* 送信 */}
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -76,11 +76,12 @@ const PostFormTagsArea: FC<Props> = ({ tags, setTags, errors, ...rest }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative w-full">
|
<div className="relative w-full">
|
||||||
<Label>タグ</Label>
|
<Label invalid={errors && errors.length > 0}>タグ</Label>
|
||||||
<TextArea
|
<TextArea
|
||||||
{...rest}
|
{...rest}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
value={tags}
|
value={tags}
|
||||||
|
invalid={errors && errors.length > 0}
|
||||||
onChange={ev => setTags (ev.target.value)}
|
onChange={ev => setTags (ev.target.value)}
|
||||||
onSelect={async (ev: SyntheticEvent<HTMLTextAreaElement>) => {
|
onSelect={async (ev: SyntheticEvent<HTMLTextAreaElement>) => {
|
||||||
const pos = (ev.target as HTMLTextAreaElement).selectionStart
|
const pos = (ev.target as HTMLTextAreaElement).selectionStart
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import DateTimeField from '@/components/common/DateTimeField'
|
import DateTimeField from '@/components/common/DateTimeField'
|
||||||
|
import { FieldError } from '@/components/common/FieldError'
|
||||||
import Label from '@/components/common/Label'
|
import Label from '@/components/common/Label'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
|
|
||||||
@@ -10,23 +11,26 @@ type Props = {
|
|||||||
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[]
|
errors?: string[] }
|
||||||
beforeErrors?: string[] }
|
|
||||||
|
|
||||||
|
|
||||||
const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
const PostOriginalCreatedTimeField: FC<Props> = (
|
||||||
|
{ disabled,
|
||||||
originalCreatedFrom,
|
originalCreatedFrom,
|
||||||
setOriginalCreatedFrom,
|
setOriginalCreatedFrom,
|
||||||
originalCreatedBefore,
|
originalCreatedBefore,
|
||||||
setOriginalCreatedBefore }) => (
|
setOriginalCreatedBefore,
|
||||||
|
errors }: Props,
|
||||||
|
) => (
|
||||||
<div>
|
<div>
|
||||||
<Label>オリジナルの作成日時</Label>
|
<Label invalid={errors && errors.length > 0}>オリジナルの作成日時</Label>
|
||||||
|
|
||||||
<div className="my-1 flex">
|
<div className="my-1 flex">
|
||||||
<div className="w-80">
|
<div className="w-80">
|
||||||
<DateTimeField
|
<DateTimeField
|
||||||
className="mr-2"
|
className="mr-2"
|
||||||
disabled={disabled ?? false}
|
disabled={disabled ?? false}
|
||||||
|
aria-invalid={errors && errors.length > 0}
|
||||||
value={originalCreatedFrom ?? undefined}
|
value={originalCreatedFrom ?? undefined}
|
||||||
onChange={setOriginalCreatedFrom}
|
onChange={setOriginalCreatedFrom}
|
||||||
onBlur={ev => {
|
onBlur={ev => {
|
||||||
@@ -54,13 +58,13 @@ 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
|
||||||
className="mr-2"
|
className="mr-2"
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
aria-invalid={errors && errors.length > 0}
|
||||||
value={originalCreatedBefore ?? undefined}
|
value={originalCreatedBefore ?? undefined}
|
||||||
onChange={setOriginalCreatedBefore}/>
|
onChange={setOriginalCreatedBefore}/>
|
||||||
より前
|
より前
|
||||||
@@ -76,7 +80,8 @@ const PostOriginalCreatedTimeField: FC<Props> = ({ disabled,
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<FieldError messages={beforeErrors}/>
|
|
||||||
|
<FieldError messages={errors}/>
|
||||||
</div>)
|
</div>)
|
||||||
|
|
||||||
export default PostOriginalCreatedTimeField
|
export default PostOriginalCreatedTimeField
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export const FieldError: FC<Props> = ({ messages }: Props) => {
|
|||||||
return null
|
return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul className="mt-1 space-y-1 text-red-600 dark:text-red-400">
|
<ul className="mt-1 space-y-1 text-red-700 dark:text-red-300">
|
||||||
{messages.map ((message, i) => <li key={i}>{message}</li>)}
|
{messages.map ((message, i) => <li key={i}>{message}</li>)}
|
||||||
</ul>)
|
</ul>)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
|
|
||||||
type Props = { children: React.ReactNode
|
type Props = { children: React.ReactNode
|
||||||
checkBox?: { label: string
|
checkBox?: { label: string
|
||||||
checked: boolean
|
checked: boolean
|
||||||
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void } }
|
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void }
|
||||||
|
invalid?: boolean }
|
||||||
|
|
||||||
|
|
||||||
const Label: FC<Props> = ({ children, checkBox }) => {
|
const Label: FC<Props> = ({ children, checkBox, invalid }: Props) => {
|
||||||
|
const labelClassName = cn ('block font-semibold mb-1',
|
||||||
|
invalid && 'text-red-700 dark:text-red-300')
|
||||||
|
|
||||||
if (!(checkBox))
|
if (!(checkBox))
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
<label className="block font-semibold mb-1">
|
<label className={labelClassName}>
|
||||||
{children}
|
{children}
|
||||||
</label>)
|
</label>)
|
||||||
}
|
}
|
||||||
@@ -29,4 +35,5 @@ const Label: FC<Props> = ({ children, checkBox }) => {
|
|||||||
</div>)
|
</div>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default Label
|
export default Label
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
import { forwardRef } from 'react'
|
import { forwardRef } from 'react'
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
import type { TextareaHTMLAttributes } from 'react'
|
import type { TextareaHTMLAttributes } from 'react'
|
||||||
|
|
||||||
type Props = TextareaHTMLAttributes<HTMLTextAreaElement>
|
type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & { invalid?: boolean }
|
||||||
|
|
||||||
|
|
||||||
export default forwardRef<HTMLTextAreaElement, Props> (({ ...props }, ref) => (
|
export default forwardRef<HTMLTextAreaElement, Props> (
|
||||||
<textarea ref={ref} className="rounded border w-full p-2 h-32" {...props}/>))
|
({ className, invalid = false, ...props }, ref) => (
|
||||||
|
<textarea
|
||||||
|
ref={ref}
|
||||||
|
aria-invalid={invalid}
|
||||||
|
className={cn ('rounded border w-full p-2 h-32',
|
||||||
|
(invalid
|
||||||
|
? ['border-red-500 bg-red-50 text-red-900',
|
||||||
|
'focus:border-red-500 focus:outline-none focus:ring-2',
|
||||||
|
'foucs:ring-red-200',
|
||||||
|
'dark:border-red-500 dark:bg-red-950/30 dark:text-red-100']
|
||||||
|
: ['border-gray-300',
|
||||||
|
'focus:border-blue-500 focus:outline-none focus:ring-2',
|
||||||
|
'focus:ring-blue-200']),
|
||||||
|
className)}
|
||||||
|
{...props}/>))
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
|
|
||||||
|
import type { UserRole } from '@/types'
|
||||||
|
|
||||||
|
const userWithRole = (role: UserRole) => ({ role })
|
||||||
|
|
||||||
|
describe ('user permission helpers', () => {
|
||||||
|
it ('allows admins and members to edit content', () => {
|
||||||
|
expect (canEditContent (userWithRole ('admin'))).toBe (true)
|
||||||
|
expect (canEditContent (userWithRole ('member'))).toBe (true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it ('does not allow guests or missing users to edit content', () => {
|
||||||
|
expect (canEditContent (userWithRole ('guest'))).toBe (false)
|
||||||
|
expect (canEditContent (null)).toBe (false)
|
||||||
|
expect (canEditContent (undefined)).toBe (false)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import type { User, UserRole } from '@/types'
|
||||||
|
|
||||||
|
const CONTENT_EDITOR_ROLES: readonly UserRole[] = ['admin', 'member']
|
||||||
|
|
||||||
|
export const canEditContent = (
|
||||||
|
user: Pick<User, 'role'> | null | undefined,
|
||||||
|
): boolean => user != null && CONTENT_EDITOR_ROLES.includes (user.role)
|
||||||
@@ -71,3 +71,15 @@ export const originalCreatedAtString = (
|
|||||||
.join (' '))
|
.join (' '))
|
||||||
return rtn === '〜' ? '年月日不詳' : rtn
|
return rtn === '〜' ? '年月日不詳' : rtn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const inputClass = (invalid?: boolean, className?: string): string =>
|
||||||
|
cn ('w-full rounded border p-2',
|
||||||
|
(invalid
|
||||||
|
? ['border-red-500 bg-red-50 text-red-900',
|
||||||
|
'placeholder:text-red-300',
|
||||||
|
'focus:border-red-500 focus:outline-none focus:ring-2 focus:ring-red-200',
|
||||||
|
'dark:border-red-500 dark:bg-red-950/30 dark:text-red-100']
|
||||||
|
: ['border-gray-300',
|
||||||
|
'focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-200']),
|
||||||
|
className)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import { SITE_TITLE } from '@/config'
|
|||||||
import { isApiError } from '@/lib/api'
|
import { isApiError } from '@/lib/api'
|
||||||
import { fetchPost, toggleViewedFlg } from '@/lib/posts'
|
import { fetchPost, toggleViewedFlg } from '@/lib/posts'
|
||||||
import { postsKeys, tagsKeys } from '@/lib/queryKeys'
|
import { postsKeys, tagsKeys } from '@/lib/queryKeys'
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import NotFound from '@/pages/NotFound'
|
import NotFound from '@/pages/NotFound'
|
||||||
import ServiceUnavailable from '@/pages/ServiceUnavailable'
|
import ServiceUnavailable from '@/pages/ServiceUnavailable'
|
||||||
@@ -27,6 +28,7 @@ type Props = { user: User | null }
|
|||||||
|
|
||||||
|
|
||||||
const PostDetailPage: FC<Props> = ({ user }) => {
|
const PostDetailPage: FC<Props> = ({ user }) => {
|
||||||
|
const editable = canEditContent (user)
|
||||||
const { id } = useParams ()
|
const { id } = useParams ()
|
||||||
const postId = String (id ?? '')
|
const postId = String (id ?? '')
|
||||||
const postKey = postsKeys.show (postId)
|
const postKey = postsKeys.show (postId)
|
||||||
@@ -163,7 +165,7 @@ const PostDetailPage: FC<Props> = ({ user }) => {
|
|||||||
? <PostList posts={post.related}/>
|
? <PostList posts={post.related}/>
|
||||||
: 'まだないよ(笑)'}
|
: 'まだないよ(笑)'}
|
||||||
</Tab>
|
</Tab>
|
||||||
{['admin', 'member'].some (r => user?.role === r) && (
|
{editable && (
|
||||||
<Tab name="編輯">
|
<Tab name="編輯">
|
||||||
<PostEditForm
|
<PostEditForm
|
||||||
post={post}
|
post={post}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { Button } from '@/components/ui/button'
|
|||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiGet, apiPost } from '@/lib/api'
|
import { apiGet, apiPost } from '@/lib/api'
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
import Forbidden from '@/pages/Forbidden'
|
import Forbidden from '@/pages/Forbidden'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
@@ -22,7 +23,7 @@ type Props = { user: User | null }
|
|||||||
|
|
||||||
|
|
||||||
const PostNewPage: FC<Props> = ({ user }) => {
|
const PostNewPage: FC<Props> = ({ user }) => {
|
||||||
const editable = ['admin', 'member'].some (r => user?.role === r)
|
const editable = canEditContent (user)
|
||||||
|
|
||||||
const navigate = useNavigate ()
|
const navigate = useNavigate ()
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import MainArea from '@/components/layout/MainArea'
|
|||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiGet, apiPut } from '@/lib/api'
|
import { apiGet, apiPut } from '@/lib/api'
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
|
|
||||||
import type { NicoTag, Tag, User } from '@/types'
|
import type { NicoTag, Tag, User } from '@/types'
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ const NicoTagListPage: FC<Props> = ({ user }) => {
|
|||||||
|
|
||||||
const loaderRef = useRef<HTMLDivElement | null> (null)
|
const loaderRef = useRef<HTMLDivElement | null> (null)
|
||||||
|
|
||||||
const memberFlg = ['admin', 'member'].some (r => user?.role === r)
|
const editable = canEditContent (user)
|
||||||
|
|
||||||
const applyLoadedTags = useCallback ((data: { tags: NicoTag[]; nextCursor: string },
|
const applyLoadedTags = useCallback ((data: { tags: NicoTag[]; nextCursor: string },
|
||||||
withCursor: boolean) => {
|
withCursor: boolean) => {
|
||||||
@@ -117,7 +118,7 @@ const NicoTagListPage: FC<Props> = ({ user }) => {
|
|||||||
<tr>
|
<tr>
|
||||||
<th className="p-2 text-left">ニコニコタグ</th>
|
<th className="p-2 text-left">ニコニコタグ</th>
|
||||||
<th className="p-2 text-left">連携タグ</th>
|
<th className="p-2 text-left">連携タグ</th>
|
||||||
{memberFlg && <th></th>}
|
{editable && <th></th>}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -139,7 +140,7 @@ const NicoTagListPage: FC<Props> = ({ user }) => {
|
|||||||
withCount={false}/>
|
withCount={false}/>
|
||||||
</span>))}
|
</span>))}
|
||||||
</td>
|
</td>
|
||||||
{memberFlg && (
|
{editable && (
|
||||||
<td className="p-2">
|
<td className="p-2">
|
||||||
<a href="#" onClick={ev => {
|
<a href="#" onClick={ev => {
|
||||||
ev.preventDefault ()
|
ev.preventDefault ()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { toast } from '@/components/ui/use-toast'
|
|||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiGet, apiPut } from '@/lib/api'
|
import { apiGet, apiPut } from '@/lib/api'
|
||||||
import { wikiKeys } from '@/lib/queryKeys'
|
import { wikiKeys } from '@/lib/queryKeys'
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
import Forbidden from '@/pages/Forbidden'
|
import Forbidden from '@/pages/Forbidden'
|
||||||
|
|
||||||
import 'react-markdown-editor-lite/lib/index.css'
|
import 'react-markdown-editor-lite/lib/index.css'
|
||||||
@@ -24,7 +25,7 @@ type Props = { user: User | null }
|
|||||||
|
|
||||||
|
|
||||||
const WikiEditPage: FC<Props> = ({ user }) => {
|
const WikiEditPage: FC<Props> = ({ user }) => {
|
||||||
const editable = ['admin', 'member'].some (r => user?.role === r)
|
const editable = canEditContent (user)
|
||||||
|
|
||||||
const { id } = useParams ()
|
const { id } = useParams ()
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import MainArea from '@/components/layout/MainArea'
|
|||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiPost } from '@/lib/api'
|
import { apiPost } from '@/lib/api'
|
||||||
|
import { canEditContent } from '@/lib/users'
|
||||||
import Forbidden from '@/pages/Forbidden'
|
import Forbidden from '@/pages/Forbidden'
|
||||||
|
|
||||||
import 'react-markdown-editor-lite/lib/index.css'
|
import 'react-markdown-editor-lite/lib/index.css'
|
||||||
@@ -22,7 +23,7 @@ type Props = { user: User | null }
|
|||||||
|
|
||||||
|
|
||||||
const WikiNewPage: FC<Props> = ({ user }) => {
|
const WikiNewPage: FC<Props> = ({ user }) => {
|
||||||
const editable = ['admin', 'member'].some (r => user?.role === r)
|
const editable = canEditContent (user)
|
||||||
|
|
||||||
const location = useLocation ()
|
const location = useLocation ()
|
||||||
const navigate = useNavigate ()
|
const navigate = useNavigate ()
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする