コミットを比較
2
コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
3dba626e3f | ||
|
|
76ca985096 |
@@ -218,16 +218,63 @@ class TagsController < ApplicationController
|
|||||||
locale = resolve_locale!
|
locale = resolve_locale!
|
||||||
|
|
||||||
q = params[:q].to_s.strip.sub(/\Anot:/i, '')
|
q = params[:q].to_s.strip.sub(/\Anot:/i, '')
|
||||||
literal = ActiveRecord::Base.sanitize_sql_like(q)
|
prefix = "#{ ActiveRecord::Base.sanitize_sql_like(q) }%"
|
||||||
nico = bool?(:nico, default: true)
|
|
||||||
present = bool?(:present, default: true)
|
|
||||||
|
|
||||||
rows = autocomplete_rows(locale:, pattern: "#{ literal }%", nico:, present:)
|
with_nico = bool?(:nico, default: true)
|
||||||
if rows.length < 20 && q.present?
|
present_only = bool?(:present, default: true)
|
||||||
suffix_rows = autocomplete_rows(locale:, pattern: "%#{ literal }", nico:, present:)
|
|
||||||
rows = (rows + suffix_rows).uniq { [_1['category'], _1['id']] }.first(20)
|
alias_rows =
|
||||||
|
TagName
|
||||||
|
.where(language_code: locale.language_code, primary_flg: false)
|
||||||
|
.where('name LIKE ?', prefix)
|
||||||
|
.pluck(:tag_id, :name)
|
||||||
|
|
||||||
|
matched_alias_by_tag_id = { }
|
||||||
|
tag_ids = []
|
||||||
|
alias_rows.each do |tag_id, alias_name|
|
||||||
|
tag_ids << tag_id
|
||||||
|
matched_alias_by_tag_id[tag_id] ||= alias_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
base =
|
||||||
|
Tag
|
||||||
|
.joins(:tag_names)
|
||||||
|
.preload(:tag_names, :materials, :deerjikists, :tag_descriptions,
|
||||||
|
parents: [:tag_names, :materials, :deerjikists, :tag_descriptions])
|
||||||
|
.where(tag_names: { language_code: locale.language_code, primary_flg: true },
|
||||||
|
deprecated_at: nil)
|
||||||
|
base = base.where('tags.post_count > 0') if present_only
|
||||||
|
|
||||||
|
canonical_hit = base.where('tag_names.name LIKE ?', prefix)
|
||||||
|
|
||||||
|
internal_tags = canonical_hit.or(base.where(id: tag_ids.uniq))
|
||||||
|
|
||||||
|
internal_rows =
|
||||||
|
internal_tags
|
||||||
|
.order(Arel.sql('tags.post_count DESC, tag_names.name'))
|
||||||
|
.limit(20)
|
||||||
|
.map { |tag|
|
||||||
|
TagRepr.base(tag, language_code: locale.language_code)
|
||||||
|
.merge(matched_alias: matched_alias_by_tag_id[tag.id])
|
||||||
|
}
|
||||||
|
|
||||||
|
return render json: internal_rows unless with_nico
|
||||||
|
|
||||||
|
external_base = ExternalTag.all
|
||||||
|
external_base = external_base.where('post_count > 0') if present_only
|
||||||
|
|
||||||
|
external_rows =
|
||||||
|
external_base
|
||||||
|
.where("CONCAT(platform, ':', name) LIKE ? OR name LIKE ?", prefix, prefix)
|
||||||
|
.order(post_count: :desc, name: :asc)
|
||||||
|
.limit(20)
|
||||||
|
.map { external_tag_json(_1) }
|
||||||
|
|
||||||
|
rows =
|
||||||
|
(internal_rows + external_rows)
|
||||||
|
.sort_by { |row| [-row['post_count'], row['name'] || row[:name]] }
|
||||||
|
.first(20)
|
||||||
|
|
||||||
render json: rows
|
render json: rows
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -498,59 +545,6 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def autocomplete_rows locale:, pattern:, nico:, present:
|
|
||||||
alias_rows =
|
|
||||||
TagName
|
|
||||||
.where(language_code: locale.language_code, primary_flg: false)
|
|
||||||
.where('name LIKE ?', pattern)
|
|
||||||
.pluck(:tag_id, :name)
|
|
||||||
|
|
||||||
matched_alias_by_tag_id = { }
|
|
||||||
tag_ids = []
|
|
||||||
alias_rows.each do |tag_id, alias_name|
|
|
||||||
tag_ids << tag_id
|
|
||||||
matched_alias_by_tag_id[tag_id] ||= alias_name
|
|
||||||
end
|
|
||||||
|
|
||||||
base =
|
|
||||||
Tag
|
|
||||||
.joins(:tag_names)
|
|
||||||
.preload(:tag_names, :materials, :deerjikists, :tag_descriptions,
|
|
||||||
parents: [:tag_names, :materials, :deerjikists, :tag_descriptions])
|
|
||||||
.where(tag_names: { language_code: locale.language_code, primary_flg: true },
|
|
||||||
deprecated_at: nil)
|
|
||||||
base = base.where('tags.post_count > 0') if present
|
|
||||||
|
|
||||||
canonical_hit = base.where('tag_names.name LIKE ?', pattern)
|
|
||||||
|
|
||||||
internal_tags = canonical_hit.or(base.where(id: tag_ids.uniq))
|
|
||||||
|
|
||||||
internal_rows =
|
|
||||||
internal_tags
|
|
||||||
.order(Arel.sql('tags.post_count DESC, tag_names.name'))
|
|
||||||
.limit(20)
|
|
||||||
.map { |tag|
|
|
||||||
TagRepr.base(tag, language_code: locale.language_code)
|
|
||||||
.merge(matched_alias: matched_alias_by_tag_id[tag.id])
|
|
||||||
}
|
|
||||||
|
|
||||||
return internal_rows unless nico
|
|
||||||
|
|
||||||
external_base = ExternalTag.all
|
|
||||||
external_base = external_base.where('post_count > 0') if present
|
|
||||||
|
|
||||||
external_rows =
|
|
||||||
external_base
|
|
||||||
.where("CONCAT(platform, ':', name) LIKE ? OR name LIKE ?", pattern, pattern)
|
|
||||||
.order(post_count: :desc, name: :asc)
|
|
||||||
.limit(20)
|
|
||||||
.map { external_tag_json(_1) }
|
|
||||||
|
|
||||||
(internal_rows + external_rows)
|
|
||||||
.sort_by { |row| [-row['post_count'], row['name'] || row[:name]] }
|
|
||||||
.first(20)
|
|
||||||
end
|
|
||||||
|
|
||||||
def material_filter_param default:
|
def material_filter_param default:
|
||||||
value = params[:material_filter].to_s.presence
|
value = params[:material_filter].to_s.presence
|
||||||
return default unless ['present', 'missing', 'any'].include?(value)
|
return default unless ['present', 'missing', 'any'].include?(value)
|
||||||
|
|||||||
@@ -482,7 +482,6 @@ RSpec.describe 'Tags API', type: :request do
|
|||||||
create(:tag, category: :general, primary_name: name, post_count: 1)
|
create(:tag, category: :general, primary_name: name, post_count: 1)
|
||||||
create(:external_tag, name:, post_count: 1)
|
create(:external_tag, name:, post_count: 1)
|
||||||
end
|
end
|
||||||
create(:external_tag, name: 'suffix_combined', post_count: 100)
|
|
||||||
|
|
||||||
get '/tags/autocomplete', params: { q: 'combined' }
|
get '/tags/autocomplete', params: { q: 'combined' }
|
||||||
|
|
||||||
@@ -491,39 +490,6 @@ RSpec.describe 'Tags API', type: :request do
|
|||||||
expect(json.map { |row| row.fetch('name') }).to eq(expected.first(20))
|
expect(json.map { |row| row.fetch('name') }).to eq(expected.first(20))
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'appends primary, alias, and external suffix matches without duplicate resources' do
|
|
||||||
prefix = create(:tag, primary_name: 'tail', post_count: 1)
|
|
||||||
create(:tag_name, :alias, name: 'tail_alias', tag: prefix)
|
|
||||||
create(:tag_name, :alias, name: 'alias_tail', tag: prefix)
|
|
||||||
create(:tag, primary_name: 'primary_tail', post_count: 4)
|
|
||||||
alias_target = create(:tag, primary_name: 'alias_target', post_count: 3)
|
|
||||||
create(:tag_name, :alias, name: 'suffix_tail', tag: alias_target)
|
|
||||||
external = create(:external_tag, id: prefix.id, name: 'external_tail', post_count: 5)
|
|
||||||
create(:tag, primary_name: 'middle_tail_only', post_count: 100)
|
|
||||||
create(:external_tag, name: 'middle_tail_only', post_count: 100)
|
|
||||||
|
|
||||||
get '/tags/autocomplete', params: { q: 'tail' }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.map { |row| row.fetch('name') }).to eq(
|
|
||||||
['tail', 'nico:external_tail', 'primary_tail', 'alias_target'])
|
|
||||||
expect(json.first.fetch('matched_alias')).to eq('tail_alias')
|
|
||||||
expect(json.last.fetch('matched_alias')).to eq('suffix_tail')
|
|
||||||
expect(json[1]).to include('id' => external.id, 'category' => 'nico')
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'fills only the remaining places with suffix matches' do
|
|
||||||
names = 19.times.map { |number| "capacity_#{ number.to_s.rjust(2, '0') }" }
|
|
||||||
names.each { create(:tag, primary_name: _1, post_count: 1) }
|
|
||||||
create(:tag, primary_name: 'first_capacity', post_count: 3)
|
|
||||||
create(:external_tag, name: 'second_capacity', post_count: 2)
|
|
||||||
|
|
||||||
get '/tags/autocomplete', params: { q: 'capacity' }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(json.map { |row| row.fetch('name') }).to eq(names + ['first_capacity'])
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'excludes external tags but preserves internal alias matches when nico is false' do
|
it 'excludes external tags but preserves internal alias matches when nico is false' do
|
||||||
internal = create(:tag, category: :general, primary_name: 'switch_internal', post_count: 1)
|
internal = create(:tag, category: :general, primary_name: 'switch_internal', post_count: 1)
|
||||||
alias_target = create(:tag, category: :general, primary_name: 'alias_target', post_count: 1)
|
alias_target = create(:tag, category: :general, primary_name: 'alias_target', post_count: 1)
|
||||||
@@ -552,20 +518,12 @@ RSpec.describe 'Tags API', type: :request do
|
|||||||
create(:external_tag, name: literal, post_count: 1)
|
create(:external_tag, name: literal, post_count: 1)
|
||||||
create(:tag, category: :general, primary_name: 'literalXmatch', post_count: 2)
|
create(:tag, category: :general, primary_name: 'literalXmatch', post_count: 2)
|
||||||
create(:external_tag, name: 'literalXmatch', post_count: 2)
|
create(:external_tag, name: 'literalXmatch', post_count: 2)
|
||||||
suffix = "suffix_literal#{ wildcard }"
|
|
||||||
create(:tag, primary_name: suffix, post_count: 1)
|
|
||||||
suffix_alias = create(:tag, primary_name: 'suffix_alias_target', post_count: 1)
|
|
||||||
create(:tag_name, :alias, name: "alias_#{ suffix }", tag: suffix_alias)
|
|
||||||
create(:external_tag, name: suffix, post_count: 1)
|
|
||||||
create(:tag, primary_name: 'suffix_literalX', post_count: 2)
|
|
||||||
create(:external_tag, name: 'suffix_literalX', post_count: 2)
|
|
||||||
|
|
||||||
get '/tags/autocomplete', params: { q: "literal#{ wildcard }" }
|
get '/tags/autocomplete', params: { q: "literal#{ wildcard }" }
|
||||||
|
|
||||||
expect(json.map { |row| row.fetch('name') })
|
expect(json.map { |row| row.fetch('name') })
|
||||||
.to contain_exactly(
|
.to contain_exactly(
|
||||||
literal, primary_tag_name_for(alias_target, 'ja').name, "nico:#{ literal }",
|
literal, primary_tag_name_for(alias_target, 'ja').name, "nico:#{ literal }")
|
||||||
suffix, 'suffix_alias_target', "nico:#{ suffix }")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -36,10 +36,13 @@ 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
|
||||||
|
label?: ReactNode
|
||||||
errors?: ReactNode[] }
|
errors?: ReactNode[] }
|
||||||
|
|
||||||
|
|
||||||
const PostFormTagsArea: FC<Props> = ({ tags, setTags, errors, ...rest }) => {
|
const PostFormTagsArea: FC<Props> = (
|
||||||
|
{ tags, setTags, label: fieldLabel, errors, ...rest },
|
||||||
|
) => {
|
||||||
const { label } = useUITranslation ('navigation')
|
const { label } = useUITranslation ('navigation')
|
||||||
const locale = useLocale ()
|
const locale = useLocale ()
|
||||||
const ref = useRef<HTMLTextAreaElement> (null)
|
const ref = useRef<HTMLTextAreaElement> (null)
|
||||||
@@ -117,7 +120,10 @@ const PostFormTagsArea: FC<Props> = ({ tags, setTags, errors, ...rest }) => {
|
|||||||
}, [locale])
|
}, [locale])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormField className="relative w-full" label={label ('tags')} messages={errors}>
|
<FormField
|
||||||
|
className="relative w-full"
|
||||||
|
label={fieldLabel ?? label ('tags')}
|
||||||
|
messages={errors}>
|
||||||
{({ describedBy, invalid }) => (
|
{({ describedBy, invalid }) => (
|
||||||
<>
|
<>
|
||||||
<TextArea
|
<TextArea
|
||||||
|
|||||||
@@ -162,12 +162,13 @@ describe ('NicoTagListPage', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it ('updates links from a tag card', async () => {
|
it ('saves links after completing the tag at the cursor', async () => {
|
||||||
api.apiGet
|
api.apiGet
|
||||||
.mockResolvedValueOnce ({
|
.mockResolvedValueOnce ({
|
||||||
tags: [buildNicoTag ({ id: 7, name: 'nico:source' })],
|
tags: [buildNicoTag ({ id: 7, name: 'nico:source' })],
|
||||||
count: 1,
|
count: 1,
|
||||||
})
|
})
|
||||||
|
.mockResolvedValueOnce ([buildTag ({ id: 8, name: '連携先', postCount: 3 })])
|
||||||
.mockResolvedValueOnce ({
|
.mockResolvedValueOnce ({
|
||||||
tags: [
|
tags: [
|
||||||
buildNicoTag ({
|
buildNicoTag ({
|
||||||
@@ -183,8 +184,24 @@ describe ('NicoTagListPage', () => {
|
|||||||
renderPage ()
|
renderPage ()
|
||||||
|
|
||||||
fireEvent.click (await screen.findByRole ('button', { name: '編集' }))
|
fireEvent.click (await screen.findByRole ('button', { name: '編集' }))
|
||||||
fireEvent.change (screen.getByLabelText ('連携する広場タグ'), {
|
const textarea = screen.getByLabelText ('連携する広場タグ')
|
||||||
target: { value: '連携先' },
|
fireEvent.change (textarea, { target: { value: '既存 連携\n後続' } })
|
||||||
|
fireEvent.focus (textarea)
|
||||||
|
fireEvent.select (textarea, {
|
||||||
|
target: { selectionStart: 4, selectionEnd: 4 } })
|
||||||
|
|
||||||
|
await waitFor (() => {
|
||||||
|
expect (api.apiGet).toHaveBeenCalledWith (
|
||||||
|
'/tags/autocomplete',
|
||||||
|
{ locale: 'ja', params: { q: '連携', nico: '0' },
|
||||||
|
signal: expect.any (AbortSignal) })
|
||||||
|
})
|
||||||
|
fireEvent.mouseDown (await screen.findByText ('連携先'))
|
||||||
|
|
||||||
|
expect (textarea).toHaveValue ('既存 連携先 \n後続')
|
||||||
|
await waitFor (() => {
|
||||||
|
expect ((textarea as HTMLTextAreaElement).selectionStart).toBe (7)
|
||||||
|
expect ((textarea as HTMLTextAreaElement).selectionEnd).toBe (7)
|
||||||
})
|
})
|
||||||
fireEvent.click (screen.getByRole ('button', { name: '保存' }))
|
fireEvent.click (screen.getByRole ('button', { name: '保存' }))
|
||||||
|
|
||||||
@@ -195,6 +212,8 @@ describe ('NicoTagListPage', () => {
|
|||||||
{ locale: 'ja', headers: { 'Content-Type': 'multipart/form-data' } },
|
{ locale: 'ja', headers: { 'Content-Type': 'multipart/form-data' } },
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
const payload = api.apiPut.mock.calls[0][1] as FormData
|
||||||
|
expect (payload.get ('tags')).toBe ('既存 連携先 \n後続')
|
||||||
expect (await screen.findByText ('1 件')).toBeInTheDocument ()
|
expect (await screen.findByText ('1 件')).toBeInTheDocument ()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,10 @@ import { useLocation } from 'react-router-dom'
|
|||||||
import { useLocale, useLocaleNavigate } from '@/lib/useLocale'
|
import { useLocale, useLocaleNavigate } from '@/lib/useLocale'
|
||||||
import TagLink from '@/components/TagLink'
|
import TagLink from '@/components/TagLink'
|
||||||
import SortHeader from '@/components/SortHeader'
|
import SortHeader from '@/components/SortHeader'
|
||||||
import FieldError from '@/components/common/FieldError'
|
import PostFormTagsArea from '@/components/PostFormTagsArea'
|
||||||
import FormField from '@/components/common/FormField'
|
import FormField from '@/components/common/FormField'
|
||||||
import PageTitle from '@/components/common/PageTitle'
|
import PageTitle from '@/components/common/PageTitle'
|
||||||
import Pagination from '@/components/common/Pagination'
|
import Pagination from '@/components/common/Pagination'
|
||||||
import TextArea from '@/components/common/TextArea'
|
|
||||||
import useDialogue from '@/lib/dialogues/useDialogue'
|
import useDialogue from '@/lib/dialogues/useDialogue'
|
||||||
import MainArea from '@/components/layout/MainArea'
|
import MainArea from '@/components/layout/MainArea'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
@@ -357,21 +356,18 @@ const NicoTagListPage: FC<Props> = ({ user }) => {
|
|||||||
dark:bg-rose-950/30">
|
dark:bg-rose-950/30">
|
||||||
<td colSpan={editable ? 5 : 4} className="p-3">
|
<td colSpan={editable ? 5 : 4} className="p-3">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<label htmlFor={`nico-links-${ tag.id }`}
|
<PostFormTagsArea
|
||||||
className="block text-sm font-semibold">
|
|
||||||
{label ('nico.tagsToLink')}
|
|
||||||
</label>
|
|
||||||
<TextArea
|
|
||||||
id={`nico-links-${ tag.id }`}
|
id={`nico-links-${ tag.id }`}
|
||||||
value={rawTags[tag.id] ?? ''}
|
label={label ('nico.tagsToLink')}
|
||||||
invalid={(errorsByTagId[tag.id] ?? []).length > 0}
|
aria-label={message ('nico.tagsToLink').text}
|
||||||
|
tags={rawTags[tag.id] ?? ''}
|
||||||
|
errors={errors}
|
||||||
className="min-h-24 resize-y"
|
className="min-h-24 resize-y"
|
||||||
placeholder={message ('nico.tagsPlaceholder').text}
|
placeholder={message ('nico.tagsPlaceholder').text}
|
||||||
lang={message ('nico.tagsPlaceholder').language}
|
lang={message ('nico.tagsPlaceholder').language}
|
||||||
onChange={e => setRawTags (rawTags => ({
|
setTags={value => setRawTags (rawTags => ({
|
||||||
...rawTags,
|
...rawTags,
|
||||||
[tag.id]: e.target.value}))}/>
|
[tag.id]: value}))}/>
|
||||||
<FieldError messages={errors}/>
|
|
||||||
<div className="flex justify-end gap-2">
|
<div className="flex justify-end gap-2">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
新しいイシューから参照
ユーザーをブロックする