コミットを比較

..
3 コミット
作成者 SHA1 メッセージ 日付
Codex Night Worker d6486cda3e AI: address review on PR #461 2026-11-13 12:38:13 +09:00
Codex Night Worker 62f501be2e AI: address review on PR #461 2026-11-12 18:55:43 +09:00
Codex Night Worker da63b46d4b AI: address issue #443 2026-11-12 12:37:23 +09:00
4個のファイルの変更、114行の追加、110行の削除
+54 -60
ファイルの表示
@@ -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)
+20 -7
ファイルの表示
@@ -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 = TagName.where(language_code: locale.language_code, name: names) registered = matching_names(TagName.where(language_code: locale.language_code),
.pluck(:name, :tag_id).to_h names, 'tag_names.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,12 +19,25 @@ 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 = TagName.where(language_code: locale.language_code, name: names) internal = matching_names(TagName.where(language_code: locale.language_code),
.pluck(:name, :tag_id).group_by(&:first) names, 'tag_names.name', :tag_id).group_by(&:first)
external = names.empty? ? {} : ExternalTag.where( external = matching_names(ExternalTag.all, names,
"CONCAT(platform, ':', name) IN (?)", names) "CONCAT(external_tags.platform, ':', external_tags.name)", :id).group_by(&:first)
.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
+39
ファイルの表示
@@ -206,6 +206,25 @@ 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' }
@@ -266,6 +285,26 @@ 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 -43
ファイルの表示
@@ -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