コミットを比較
3
コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
a259087840 | ||
|
|
006295dc45 | ||
|
|
c3c6f2bc48 |
@@ -3,8 +3,8 @@ class PostTagSearch
|
|||||||
def self.literals(locale, raw)
|
def self.literals(locale, raw)
|
||||||
tokens = raw.split.reject { _1.downcase == 'type:or' }
|
tokens = raw.split.reject { _1.downcase == 'type:or' }
|
||||||
names = tokens.map { _1.sub(/\Anot:/i, '') }
|
names = tokens.map { _1.sub(/\Anot:/i, '') }
|
||||||
registered = matching_names(TagName.where(language_code: locale.language_code),
|
registered = TagName.where(language_code: locale.language_code, name: names)
|
||||||
names, 'tag_names.name', :tag_id).to_h
|
.pluck(:name, :tag_id).to_h
|
||||||
primary = TagName.where(language_code: locale.language_code,
|
primary = TagName.where(language_code: locale.language_code,
|
||||||
tag_id: registered.values, primary_flg: true).pluck(:tag_id, :name).to_h
|
tag_id: registered.values, primary_flg: true).pluck(:tag_id, :name).to_h
|
||||||
tokens.zip(names).map do |token, name|
|
tokens.zip(names).map do |token, name|
|
||||||
@@ -19,25 +19,12 @@ class PostTagSearch
|
|||||||
|
|
||||||
def self.references_for(locale, literals)
|
def self.references_for(locale, literals)
|
||||||
names = literals.map { _1[:name] }.uniq
|
names = literals.map { _1[:name] }.uniq
|
||||||
internal = matching_names(TagName.where(language_code: locale.language_code),
|
internal = TagName.where(language_code: locale.language_code, name: names)
|
||||||
names, 'tag_names.name', :tag_id).group_by(&:first)
|
.pluck(:name, :tag_id).group_by(&:first)
|
||||||
external = matching_names(ExternalTag.all, names,
|
external = names.empty? ? {} : ExternalTag.where(
|
||||||
"CONCAT(external_tags.platform, ':', external_tags.name)", :id).group_by(&:first)
|
"CONCAT(platform, ':', name) IN (?)", names)
|
||||||
|
.pluck(Arel.sql("CONCAT(platform, ':', name)"), :id).group_by(&:first)
|
||||||
names.to_h { |name| [name, [internal.fetch(name, []).map(&:last).uniq,
|
names.to_h { |name| [name, [internal.fetch(name, []).map(&:last).uniq,
|
||||||
external.fetch(name, []).map(&:last).uniq]] }
|
external.fetch(name, []).map(&:last).uniq]] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.matching_names(scope, names, expression, identifier)
|
|
||||||
return [] if names.empty?
|
|
||||||
|
|
||||||
queries = names.uniq.map do |name|
|
|
||||||
scope.klass.sanitize_sql_array(['SELECT ? AS name', name])
|
|
||||||
end
|
|
||||||
# Return the input spelling so Ruby does not recheck database equivalence.
|
|
||||||
scope.joins(<<~SQL).pluck(Arel.sql('search_names.name'), identifier)
|
|
||||||
INNER JOIN (#{ queries.join(' UNION ALL ') }) AS search_names
|
|
||||||
ON #{ expression } = search_names.name COLLATE utf8mb4_0900_ai_ci
|
|
||||||
SQL
|
|
||||||
end
|
|
||||||
private_class_method :matching_names
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -206,25 +206,6 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when q is provided' do
|
context 'when q is provided' do
|
||||||
['SPEC_TAG', 'SpEc_TaG', 'spec_tág', 'MANKO', 'MaNkO', 'mánko'].each do |name|
|
|
||||||
it "matches #{ name } using database collation" do
|
|
||||||
get '/posts', params: { locale: 'ja', tags: name }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.fetch('count')).to eq(2)
|
|
||||||
expect(json.fetch('posts').map { _1.fetch('id') })
|
|
||||||
.to contain_exactly(post_record.id, hit_post.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "excludes #{ name } using database collation" do
|
|
||||||
get '/posts', params: { locale: 'ja', tags: "not:#{ name }" }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.fetch('count')).to eq(1)
|
|
||||||
expect(json.fetch('posts').map { _1.fetch('id') }).to eq([miss_post.id])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'filters posts by q (hit case)' do
|
it 'filters posts by q (hit case)' do
|
||||||
get '/posts', params: { tags: 'spec_tag' }
|
get '/posts', params: { tags: 'spec_tag' }
|
||||||
|
|
||||||
@@ -285,26 +266,6 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
PostExternalTag.create!(post: miss_post, external_tag:)
|
PostExternalTag.create!(post: miss_post, external_tag:)
|
||||||
end
|
end
|
||||||
|
|
||||||
['NICO:SEARCH_EXTERNAL', 'NiCo:SeArCh_ExTeRnAl', 'nico:séarch_external'].each do |name|
|
|
||||||
it "matches #{ name } using database collation" do
|
|
||||||
get '/posts', params: { locale: 'ja', tags: name }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.fetch('count')).to eq(2)
|
|
||||||
expect(json.fetch('posts').map { _1.fetch('id') })
|
|
||||||
.to contain_exactly(miss_post.id, both_post.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "excludes #{ name } using database collation" do
|
|
||||||
get '/posts', params: { locale: 'ja', tags: "not:#{ name }" }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.fetch('count')).to eq(2)
|
|
||||||
expect(json.fetch('posts').map { _1.fetch('id') })
|
|
||||||
.to contain_exactly(post_record.id, hit_post.id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'keeps internal name searches independent of colliding external ids' do
|
it 'keeps internal name searches independent of colliding external ids' do
|
||||||
get '/posts', params: { tags: primary_tag_name_for(tag, 'ja').name }
|
get '/posts', params: { tags: primary_tag_name_for(tag, 'ja').name }
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { fireEvent, screen, waitFor } from '@testing-library/react'
|
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
|
||||||
import { describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
import { textContentOf } from '@/test/text'
|
import { textContentOf } from '@/test/text'
|
||||||
import PostEditForm from '@/components/PostEditForm'
|
import PostEditForm from '@/components/PostEditForm'
|
||||||
import { buildPost, buildTag } from '@/test/factories'
|
import { buildPost, buildTag } from '@/test/factories'
|
||||||
import { renderWithProviders as render } from '@/test/render'
|
import { renderWithProviders as render } from '@/test/render'
|
||||||
|
|
||||||
|
import type { DialogueAPI } from '@/lib/dialogues/useDialogue'
|
||||||
|
|
||||||
const postsApi = vi.hoisted (() => ({
|
const postsApi = vi.hoisted (() => ({
|
||||||
updatePost: vi.fn (),
|
updatePost: vi.fn (),
|
||||||
}))
|
}))
|
||||||
@@ -18,18 +20,27 @@ const toastApi = vi.hoisted (() => ({
|
|||||||
toast: vi.fn (),
|
toast: vi.fn (),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
const dialogue = vi.hoisted (() => ({
|
||||||
|
confirm: vi.fn<DialogueAPI['confirm']> (),
|
||||||
|
choice: vi.fn () }))
|
||||||
|
|
||||||
vi.mock ('@/lib/posts', () => postsApi)
|
vi.mock ('@/lib/posts', () => postsApi)
|
||||||
vi.mock ('@/lib/api', () => api)
|
vi.mock ('@/lib/api', () => api)
|
||||||
vi.mock ('@/components/ui/use-toast', () => toastApi)
|
vi.mock ('@/components/ui/use-toast', () => toastApi)
|
||||||
vi.mock ('@/lib/dialogues/useDialogue', async importOriginal => ({
|
vi.mock ('@/lib/dialogues/useDialogue', async importOriginal => ({
|
||||||
...await importOriginal<typeof import ('@/lib/dialogues/useDialogue')> (),
|
...await importOriginal<typeof import ('@/lib/dialogues/useDialogue')> (),
|
||||||
default: () => ({
|
default: () => dialogue }))
|
||||||
choice: vi.fn (),
|
|
||||||
}),
|
|
||||||
}))
|
|
||||||
|
|
||||||
describe ('PostEditForm', () => {
|
describe ('PostEditForm', () => {
|
||||||
|
beforeEach (() => {
|
||||||
|
vi.resetAllMocks ()
|
||||||
|
})
|
||||||
|
|
||||||
it ('submits edited post fields with the current base version', async () => {
|
it ('submits edited post fields with the current base version', async () => {
|
||||||
|
const confirm = vi.fn<(value: boolean) => void> ()
|
||||||
|
dialogue.confirm.mockReturnValueOnce (new Promise<boolean> (resolve => {
|
||||||
|
confirm.mockImplementation (resolve)
|
||||||
|
}))
|
||||||
const onSave = vi.fn ()
|
const onSave = vi.fn ()
|
||||||
const post = buildPost ({
|
const post = buildPost ({
|
||||||
id: 8,
|
id: 8,
|
||||||
@@ -55,6 +66,16 @@ describe ('PostEditForm', () => {
|
|||||||
fireEvent.change (parentIds, { target: { value: '3 4' } })
|
fireEvent.change (parentIds, { target: { value: '3 4' } })
|
||||||
fireEvent.submit (screen.getByRole ('button', { name: '更新' }).closest ('form')!)
|
fireEvent.submit (screen.getByRole ('button', { name: '更新' }).closest ('form')!)
|
||||||
|
|
||||||
|
expect (postsApi.updatePost).not.toHaveBeenCalled ()
|
||||||
|
expect (dialogue.confirm).toHaveBeenCalledTimes (1)
|
||||||
|
const difference = textContentOf (
|
||||||
|
dialogue.confirm.mock.calls[0][0].description)
|
||||||
|
expect (difference).toContain ('oldnew')
|
||||||
|
expect (difference).toContain ('23 4')
|
||||||
|
expect (difference).not.toContain ('general-tag')
|
||||||
|
|
||||||
|
await act (async () => { confirm (true) })
|
||||||
|
|
||||||
await waitFor (() => {
|
await waitFor (() => {
|
||||||
expect (postsApi.updatePost).toHaveBeenCalledWith (
|
expect (postsApi.updatePost).toHaveBeenCalledWith (
|
||||||
expect.objectContaining ({
|
expect.objectContaining ({
|
||||||
@@ -71,6 +92,28 @@ describe ('PostEditForm', () => {
|
|||||||
expect (textContentOf (toastApi.toast.mock.calls[0][0].description)).toBe ('更新しました.')
|
expect (textContentOf (toastApi.toast.mock.calls[0][0].description)).toBe ('更新しました.')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it ('keeps edited fields without submitting when confirmation is cancelled', async () => {
|
||||||
|
const onSave = vi.fn ()
|
||||||
|
dialogue.confirm.mockResolvedValueOnce (false)
|
||||||
|
render (<PostEditForm post={buildPost ()} onSave={onSave}/>)
|
||||||
|
|
||||||
|
const [title, parentIds] = screen.getAllByRole ('textbox')
|
||||||
|
fireEvent.change (title, { target: { value: 'new' } })
|
||||||
|
fireEvent.change (parentIds, { target: { value: '3 4' } })
|
||||||
|
const button = screen.getByRole ('button', { name: '更新' })
|
||||||
|
fireEvent.submit (button.closest ('form')!)
|
||||||
|
|
||||||
|
await waitFor (() => {
|
||||||
|
expect (button).toBeEnabled ()
|
||||||
|
})
|
||||||
|
expect (dialogue.confirm).toHaveBeenCalledTimes (1)
|
||||||
|
expect (postsApi.updatePost).not.toHaveBeenCalled ()
|
||||||
|
expect (onSave).not.toHaveBeenCalled ()
|
||||||
|
expect (toastApi.toast).not.toHaveBeenCalled ()
|
||||||
|
expect (title).toHaveValue ('new')
|
||||||
|
expect (parentIds).toHaveValue ('3 4')
|
||||||
|
})
|
||||||
|
|
||||||
it ('preserves duration while the video tag is temporarily removed', () => {
|
it ('preserves duration while the video tag is temporarily removed', () => {
|
||||||
const post = buildPost ({
|
const post = buildPost ({
|
||||||
videoMs: 180_500,
|
videoMs: 180_500,
|
||||||
@@ -100,6 +143,7 @@ describe ('PostEditForm', () => {
|
|||||||
'shows deduplicated original-created endpoint errors on the shared datetime field',
|
'shows deduplicated original-created endpoint errors on the shared datetime field',
|
||||||
async () => {
|
async () => {
|
||||||
const post = buildPost ()
|
const post = buildPost ()
|
||||||
|
dialogue.confirm.mockResolvedValueOnce (true)
|
||||||
api.isApiError.mockReturnValue (true)
|
api.isApiError.mockReturnValue (true)
|
||||||
postsApi.updatePost.mockRejectedValueOnce ({
|
postsApi.updatePost.mockRejectedValueOnce ({
|
||||||
response: {
|
response: {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import { msToTime } from '@/lib/utils'
|
|||||||
import { useValidationErrors } from '@/lib/useValidationErrors'
|
import { useValidationErrors } from '@/lib/useValidationErrors'
|
||||||
import { useUITranslation } from '@/lib/useUITranslation'
|
import { useUITranslation } from '@/lib/useUITranslation'
|
||||||
|
|
||||||
import type { FC, FormEvent } from 'react'
|
import type { FC, FormEvent, ReactNode } from 'react'
|
||||||
|
|
||||||
import type { Post, TagWithSections } from '@/types'
|
import type { Post, TagWithSections } from '@/types'
|
||||||
|
|
||||||
@@ -23,6 +23,8 @@ type PostFormField =
|
|||||||
'parentPostIds' | 'tags' | 'videoMs'
|
'parentPostIds' | 'tags' | 'videoMs'
|
||||||
| 'originalCreatedAt' | 'originalCreatedFrom' | 'originalCreatedBefore'
|
| 'originalCreatedAt' | 'originalCreatedFrom' | 'originalCreatedBefore'
|
||||||
|
|
||||||
|
type PostEditData = Omit<Parameters<typeof updatePost>[0], 'id'>
|
||||||
|
|
||||||
const groupedMessages = (...values: (string[] | undefined)[]): string[] =>
|
const groupedMessages = (...values: (string[] | undefined)[]): string[] =>
|
||||||
[...new Set (values.flatMap (value => value ?? []))]
|
[...new Set (values.flatMap (value => value ?? []))]
|
||||||
|
|
||||||
@@ -150,12 +152,63 @@ const PostEditForm: FC<Props> = ({ post, onSave }) => {
|
|||||||
const handleSubmit = async (e: FormEvent) => {
|
const handleSubmit = async (e: FormEvent) => {
|
||||||
e.preventDefault ()
|
e.preventDefault ()
|
||||||
|
|
||||||
|
if (disabled) return
|
||||||
|
|
||||||
setDisabled (true)
|
setDisabled (true)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await update ({ id: post.id, title, tags, parentPostIds,
|
const current: PostEditData = {
|
||||||
|
title, tags, parentPostIds,
|
||||||
duration: videoFlg ? duration : null,
|
duration: videoFlg ? duration : null,
|
||||||
originalCreatedFrom, originalCreatedBefore },
|
originalCreatedFrom, originalCreatedBefore }
|
||||||
|
const fields: {
|
||||||
|
[Key in keyof PostEditData]-?: {
|
||||||
|
name: ReactNode
|
||||||
|
prev: PostEditData[Key] } } = {
|
||||||
|
title: { name: label ('shared:labels.title'), prev: post.title },
|
||||||
|
parentPostIds: {
|
||||||
|
name: label ('shared:labels.parentPosts'),
|
||||||
|
prev: (post.parentPosts ?? []).map (parent => parent.id).join (' ') },
|
||||||
|
tags: { name: label ('tags:title'), prev: tagsToStr (post.tags) },
|
||||||
|
duration: {
|
||||||
|
name: label ('shared:labels.duration'),
|
||||||
|
prev: videoMsToDurationValue (post.videoMs) },
|
||||||
|
originalCreatedFrom: {
|
||||||
|
name: <>{label ('shared:labels.originalCreatedAt')}{' '}
|
||||||
|
{label ('shared:labels.since')}</>,
|
||||||
|
prev: post.originalCreatedFrom },
|
||||||
|
originalCreatedBefore: {
|
||||||
|
name: <>{label ('shared:labels.originalCreatedAt')}{' '}
|
||||||
|
{label ('shared:labels.before')}</>,
|
||||||
|
prev: post.originalCreatedBefore } }
|
||||||
|
const differences = (Object.keys (fields) as (keyof PostEditData)[])
|
||||||
|
.map (key => ({ key, name: fields[key].name,
|
||||||
|
prev: fields[key].prev ?? '', current: current[key] ?? '' }))
|
||||||
|
.filter (row => row.prev !== row.current)
|
||||||
|
|
||||||
|
if (!(await dialogue.confirm ({
|
||||||
|
title: label ('shared:actions.diff'),
|
||||||
|
confirmText: label ('shared:actions.update'),
|
||||||
|
description: (
|
||||||
|
<dl className="max-h-[60dvh] overflow-y-auto space-y-3 text-left">
|
||||||
|
{differences.map (row => (
|
||||||
|
<div key={row.key}>
|
||||||
|
<dt className="font-semibold">{row.name}</dt>
|
||||||
|
<dd className="whitespace-pre-wrap break-words">
|
||||||
|
{row.prev && (
|
||||||
|
<del className="block text-red-600 dark:text-red-400">
|
||||||
|
{row.prev}
|
||||||
|
</del>)}
|
||||||
|
{row.current && (
|
||||||
|
<ins className="block text-green-700 dark:text-green-400">
|
||||||
|
{row.current}
|
||||||
|
</ins>)}
|
||||||
|
</dd>
|
||||||
|
</div>))}
|
||||||
|
</dl>) })))
|
||||||
|
return
|
||||||
|
|
||||||
|
await update ({ id: post.id, ...current },
|
||||||
{ baseVersionNo: post.versionNo })
|
{ baseVersionNo: post.versionNo })
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
新しいイシューから参照
ユーザーをブロックする