コミットを比較
7
コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
743c19cfa3 | ||
|
|
a1ec74c6d9 | ||
|
|
d58c2177fc | ||
|
|
241bed725d | ||
|
|
a1baaeb340 | ||
|
|
2d455f6914 | ||
|
|
f0065af00a |
@@ -28,17 +28,19 @@ class NicoTagsController < ApplicationController
|
|||||||
'ON post_tag_max.external_tag_id = external_tags.id')
|
'ON post_tag_max.external_tag_id = external_tags.id')
|
||||||
.includes(linked_tags: [:tag_names, :tag_descriptions])
|
.includes(linked_tags: [:tag_names, :tag_descriptions])
|
||||||
if name
|
if name
|
||||||
|
name_pattern = ExternalTag.sanitize_sql_like(name).tr('*?', '%_')
|
||||||
q = q.where(('external_tags.name LIKE ? ' +
|
q = q.where(('external_tags.name LIKE ? ' +
|
||||||
"OR CONCAT(external_tags.platform, ':', external_tags.name) LIKE ?"),
|
"OR CONCAT(external_tags.platform, ':', external_tags.name) LIKE ?"),
|
||||||
"%#{ name }%", "%#{ name }")
|
"%#{ name_pattern }%", "%#{ name_pattern }")
|
||||||
end
|
end
|
||||||
|
|
||||||
if linked_tag
|
if linked_tag
|
||||||
|
linked_tag_pattern = "%#{ Tag.sanitize_sql_like(linked_tag).tr('*?', '%_') }%"
|
||||||
linked_tag_ids =
|
linked_tag_ids =
|
||||||
Tag
|
Tag
|
||||||
.joins(:tag_names)
|
.joins(:tag_names)
|
||||||
.where(tag_names: { language_code: locale.language_code, primary_flg: true })
|
.where(tag_names: { language_code: locale.language_code, primary_flg: true })
|
||||||
.where('tag_names.name LIKE ?', "%#{ linked_tag }%")
|
.where('tag_names.name LIKE ?', linked_tag_pattern)
|
||||||
.pluck(:id)
|
.pluck(:id)
|
||||||
linked_nico_tag_ids = NicoTagRelation.where(tag_id: linked_tag_ids).pluck(:nico_tag_id)
|
linked_nico_tag_ids = NicoTagRelation.where(tag_id: linked_tag_ids).pluck(:nico_tag_id)
|
||||||
q = q.where(id: linked_nico_tag_ids)
|
q = q.where(id: linked_nico_tag_ids)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class PostVersionsController < ApplicationController
|
|||||||
|
|
||||||
q = HistoryAccess.visible(PostVersion.all)
|
q = HistoryAccess.visible(PostVersion.all)
|
||||||
q = q.where('post_versions.post_id = ?', post_id) if post_id
|
q = q.where('post_versions.post_id = ?', post_id) if post_id
|
||||||
q = q.merge(version_scope) if tag_id || external_tag_id
|
q = q.with_previous_version if tag_id || external_tag_id
|
||||||
if external_tag_id || (tag_id && !(Tag.exists?(id: tag_id)))
|
if external_tag_id || (tag_id && !(Tag.exists?(id: tag_id)))
|
||||||
q = q.where('JSON_CONTAINS(post_versions.tags_json,' +
|
q = q.where('JSON_CONTAINS(post_versions.tags_json,' +
|
||||||
"JSON_OBJECT('external_tag_id', #{ external_tag_id || tag_id })) " +
|
"JSON_OBJECT('external_tag_id', #{ external_tag_id || tag_id })) " +
|
||||||
@@ -29,19 +29,14 @@ class PostVersionsController < ApplicationController
|
|||||||
|
|
||||||
count = q.except(:select, :order, :limit, :offset).count
|
count = q.except(:select, :order, :limit, :offset).count
|
||||||
|
|
||||||
# Reuse the authorised previous snapshot when it is needed for filtering.
|
ids = q.order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC'))
|
||||||
page_scope = q.order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC'))
|
.limit(limit)
|
||||||
.limit(limit).offset(offset)
|
.offset(offset)
|
||||||
versions =
|
.pluck('post_versions.id')
|
||||||
if tag_id || external_tag_id
|
versions = version_scope.merge(q).where(id: ids)
|
||||||
page_scope
|
.order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC'))
|
||||||
else
|
|
||||||
version_scope.from(page_scope.select('post_versions.*'), :post_versions)
|
|
||||||
.order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC'))
|
|
||||||
end
|
|
||||||
|
|
||||||
render json: { versions: serialise_versions(versions, locale.language_code),
|
render json: { versions: serialise_versions(versions, locale.language_code), count:, population_count: HistoryAccess.population_count(PostVersion) }
|
||||||
count:, population_count: HistoryAccess.population_count(PostVersion) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ class PostsController < ApplicationController
|
|||||||
:tag_names, :tag_descriptions] }])
|
:tag_names, :tag_descriptions] }])
|
||||||
.with_attached_thumbnail
|
.with_attached_thumbnail
|
||||||
|
|
||||||
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
q = q.where('posts.url LIKE ?', "%#{ Post.sanitize_sql_like(url).tr('*?', '%_') }%") if url
|
||||||
q = q.where('posts.title LIKE ?', "%#{ title }%") if title
|
q = q.where('posts.title LIKE ?', "%#{ Post.sanitize_sql_like(title).tr('*?', '%_') }%") if title
|
||||||
if original_created_from
|
if original_created_from
|
||||||
q = q.where('posts.original_created_before > ?', original_created_from)
|
q = q.where('posts.original_created_before > ?', original_created_from)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
post_id = params[:post]
|
post_id = params[:post]
|
||||||
|
|
||||||
name = params[:name].presence
|
name = params[:name].to_s.strip.presence
|
||||||
category = params[:category].presence
|
category = params[:category].to_s.strip.presence
|
||||||
post_count_between = (params[:post_count_gte].presence || -1).to_i,
|
post_count_between = (params[:post_count_gte].presence || -1).to_i,
|
||||||
(params[:post_count_lte].presence || -1).to_i
|
(params[:post_count_lte].presence || -1).to_i
|
||||||
post_count_between[0] = nil if post_count_between[0] < 0
|
post_count_between[0] = nil if post_count_between[0] < 0
|
||||||
@@ -56,10 +56,11 @@ class TagsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
if name
|
if name
|
||||||
tags = tags.where('tag_names.name LIKE ?', "%#{ name }%")
|
name_pattern = "%#{ Tag.sanitize_sql_like(name).tr('*?', '%_') }%"
|
||||||
|
tags = tags.where('tag_names.name LIKE ?', name_pattern)
|
||||||
external_tags =
|
external_tags =
|
||||||
external_tags.where("CONCAT(external_tags.platform, ':', external_tags.name) LIKE ?",
|
external_tags.where("CONCAT(external_tags.platform, ':', external_tags.name) LIKE ?",
|
||||||
"%#{ name }%")
|
name_pattern)
|
||||||
end
|
end
|
||||||
|
|
||||||
if category == 'nico'
|
if category == 'nico'
|
||||||
|
|||||||
@@ -41,9 +41,10 @@ class HistoryAccess < ActiveSupport::CurrentAttributes
|
|||||||
table = model.table_name
|
table = model.table_name
|
||||||
keys = KEYS.fetch(table)
|
keys = KEYS.fetch(table)
|
||||||
matches = keys.map { "boundary.#{ _1 } = #{ row }.#{ _1 }" }.join(' AND ')
|
matches = keys.map { "boundary.#{ _1 } = #{ row }.#{ _1 }" }.join(' AND ')
|
||||||
# Any discard at or after this version hides it, including after a restore.
|
boundaries = "SELECT #{ keys.join(', ') }, MAX(version_no) AS version_no FROM #{ table } " +
|
||||||
clauses = ["NOT EXISTS (SELECT 1 FROM #{ table } boundary WHERE #{ matches } " +
|
"WHERE event_type = 'discard' GROUP BY #{ keys.join(', ') }"
|
||||||
"AND boundary.version_no >= #{ row }.version_no AND boundary.event_type = 'discard')"]
|
clauses = ["NOT EXISTS (SELECT 1 FROM (#{ boundaries }) boundary WHERE #{ matches } " +
|
||||||
|
"AND boundary.version_no >= #{ row }.version_no)"]
|
||||||
clauses += reference_sql(table, row) if references
|
clauses += reference_sql(table, row) if references
|
||||||
clauses.map { "(#{ _1 })" }.join(' AND ')
|
clauses.map { "(#{ _1 })" }.join(' AND ')
|
||||||
end
|
end
|
||||||
@@ -61,8 +62,8 @@ class HistoryAccess < ActiveSupport::CurrentAttributes
|
|||||||
# A scalar lookup avoids MySQL's semijoin rewrite losing the JSON row correlation.
|
# A scalar lookup avoids MySQL's semijoin rewrite losing the JSON row correlation.
|
||||||
"(SELECT 1 FROM JSON_TABLE(COALESCE(#{ document }, JSON_ARRAY()), " +
|
"(SELECT 1 FROM JSON_TABLE(COALESCE(#{ document }, JSON_ARRAY()), " +
|
||||||
"'$[*]' COLUMNS (reference_id BIGINT PATH '$' NULL ON EMPTY)) reference_entry " +
|
"'$[*]' COLUMNS (reference_id BIGINT PATH '$' NULL ON EMPTY)) reference_entry " +
|
||||||
"JOIN #{ table } blocked ON reference_entry.reference_id = blocked.#{ key } " +
|
"JOIN (#{ discarded_keys(table, key) }) blocked " +
|
||||||
"WHERE blocked.event_type = 'discard' LIMIT 1) IS NULL"
|
"ON reference_entry.reference_id = blocked.#{ key } LIMIT 1) IS NULL"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.list_reference(table, key, document)
|
def self.list_reference(table, key, document)
|
||||||
|
|||||||
@@ -3,6 +3,56 @@ require 'rails_helper'
|
|||||||
|
|
||||||
RSpec.describe 'NicoTags', type: :request do
|
RSpec.describe 'NicoTags', type: :request do
|
||||||
describe 'GET /tags/nico' do
|
describe 'GET /tags/nico' do
|
||||||
|
[:name, :linked_tag].each do |field|
|
||||||
|
context "when #{ field } contains wildcards" do
|
||||||
|
let!(:search_tags) do
|
||||||
|
names = ['wild%end', 'wild_end', 'wildXend', 'wildABend', 'wild%_end']
|
||||||
|
names.map.with_index do |name, index|
|
||||||
|
if field == :name
|
||||||
|
create(:external_tag, name:)
|
||||||
|
else
|
||||||
|
destination = create(:tag, primary_name: name)
|
||||||
|
external = create(:external_tag, name: "wild-linked-#{ index }")
|
||||||
|
NicoTagRelation.create!(nico_tag: external, tag: destination)
|
||||||
|
external
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
{
|
||||||
|
'%' => [0, 4],
|
||||||
|
'_' => [1, 4],
|
||||||
|
'wild%end' => [0],
|
||||||
|
'wild_end' => [1],
|
||||||
|
'wild*end' => [0, 1, 2, 3, 4],
|
||||||
|
'wild?end' => [0, 1, 2],
|
||||||
|
'wild??end' => [3, 4],
|
||||||
|
'wild%*end' => [0, 4],
|
||||||
|
'wild_?nd' => [1],
|
||||||
|
'wild%?end' => [4] }.each do |query, indices|
|
||||||
|
it "filters #{ field } with #{ query.inspect }" do
|
||||||
|
get '/tags/nico', params: { field => query }
|
||||||
|
|
||||||
|
expected_ids = indices.map { |index| search_tags[index].id }
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(json.fetch('tags').pluck('id')).to match_array(expected_ids)
|
||||||
|
expect(json.fetch('count')).to eq(expected_ids.size)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports wildcards in qualified external names' do
|
||||||
|
external = create(:external_tag, name: 'qualified_end')
|
||||||
|
create(:external_tag, name: 'qualifiedABend')
|
||||||
|
|
||||||
|
get '/tags/nico', params: { name: 'nico:qualified?end' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(json.fetch('tags').pluck('id')).to eq([external.id])
|
||||||
|
expect(json.fetch('count')).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns the legacy Tag-compatible external fields' do
|
it 'returns the legacy Tag-compatible external fields' do
|
||||||
external = create(:external_tag, name: 'legacy_external', post_count: 3)
|
external = create(:external_tag, name: 'legacy_external', post_count: 3)
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,7 @@ RSpec.describe 'Post history privacy', type: :request do
|
|||||||
let!(:other_post) do
|
let!(:other_post) do
|
||||||
create(:post, created_at: origin, updated_at: origin + 3.days + 1.hour)
|
create(:post, created_at: origin, updated_at: origin + 3.days + 1.hour)
|
||||||
end
|
end
|
||||||
let!(:tag) do
|
let!(:tag) { create(:tag, primary_name: 'private-history-tag') }
|
||||||
# Locale completion in the tag factory can create its own version 1.
|
|
||||||
create(:tag, primary_tag_name: nil).tap do |record|
|
|
||||||
create(:tag_name, tag: record, name: 'private-history-tag')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
let!(:external) { create(:external_tag, name: 'external-history-tag') }
|
let!(:external) { create(:external_tag, name: 'external-history-tag') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@@ -38,9 +33,6 @@ RSpec.describe 'Post history privacy', type: :request do
|
|||||||
end
|
end
|
||||||
PostVersion.create!(post: post_record, version_no: index + 1, event_type: event,
|
PostVersion.create!(post: post_record, version_no: index + 1, event_type: event,
|
||||||
title: "version #{ index + 1 }", url: post_record.url, tags: '',
|
title: "version #{ index + 1 }", url: post_record.url, tags: '',
|
||||||
thumbnail_base: "history-#{ index + 1 }", video_ms: (index + 1) * 1000,
|
|
||||||
original_created_from: origin - 1.year + index.days,
|
|
||||||
original_created_before: origin - 1.year + (index + 1).days,
|
|
||||||
tags_json: snapshots, parent_post_ids: '', created_at: origin + (index + 1).days)
|
tags_json: snapshots, parent_post_ids: '', created_at: origin + (index + 1).days)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -71,7 +63,7 @@ RSpec.describe 'Post history privacy', type: :request do
|
|||||||
expect(response.parsed_body.fetch('versions').first.fetch('title').fetch('prev')).to be_nil
|
expect(response.parsed_body.fetch('versions').first.fetch('title').fetch('prev')).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
['anonymous', 'guest', 'member', 'admin'].each do |role|
|
['anonymous', 'member', 'admin'].each do |role|
|
||||||
context "as #{ role }" do
|
context "as #{ role }" do
|
||||||
before do
|
before do
|
||||||
sign_in_as(create(:user, role.to_sym)) unless role == 'anonymous'
|
sign_in_as(create(:user, role.to_sym)) unless role == 'anonymous'
|
||||||
@@ -123,15 +115,6 @@ RSpec.describe 'Post history privacy', type: :request do
|
|||||||
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
||||||
restored = body.fetch('versions').find { _1.fetch('version_no') == 3 }
|
restored = body.fetch('versions').find { _1.fetch('version_no') == 3 }
|
||||||
expect(restored.fetch('title').fetch('prev')).to eq(role == 'admin' ? 'version 2' : nil)
|
expect(restored.fetch('title').fetch('prev')).to eq(role == 'admin' ? 'version 2' : nil)
|
||||||
unless role == 'admin'
|
|
||||||
fields = [
|
|
||||||
'url', 'thumbnail_base', 'video_ms',
|
|
||||||
'original_created_from', 'original_created_before']
|
|
||||||
fields.each do |field|
|
|
||||||
expect(restored.fetch(field).fetch('prev')).to be_nil
|
|
||||||
end
|
|
||||||
expect(restored.fetch('tags')).to eq([])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'uses only an authorised previous version for tag filtering' do
|
it 'uses only an authorised previous version for tag filtering' do
|
||||||
@@ -141,200 +124,6 @@ RSpec.describe 'Post history privacy', type: :request do
|
|||||||
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
||||||
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'paginates public tag additions and removals using the same exact count' do
|
|
||||||
public_tag = create(:tag, primary_tag_name: nil).tap do |record|
|
|
||||||
create(:tag_name, tag: record, name: 'public-history-tag')
|
|
||||||
end
|
|
||||||
public_external = create(:external_tag)
|
|
||||||
TagVersion.create!(tag: public_tag, version_no: 1, event_type: 'create',
|
|
||||||
name: 'public-history-tag', category: 'general', aliases: '', parent_tag_ids: '',
|
|
||||||
names: { ja: { name: 'public-history-tag', script_code: 'Jpan',
|
|
||||||
auto_generated: false, aliases: [] } },
|
|
||||||
created_at: origin)
|
|
||||||
NicoTagVersion.create!(external_tag: public_external, version_no: 1,
|
|
||||||
event_type: 'create', linked_tag_ids: '', created_at: origin)
|
|
||||||
snapshots = [
|
|
||||||
[{ tag_id: public_tag.id, version_no: 1, sections: [] },
|
|
||||||
{ external_tag_id: public_external.id }], []]
|
|
||||||
snapshots.each_with_index do |tags, index|
|
|
||||||
PostVersion.create!(post: post_record, version_no: index + 5, event_type: 'update',
|
|
||||||
title: "public snapshot #{ index }", url: post_record.url, tags: '',
|
|
||||||
tags_json: tags, parent_post_ids: '', created_at: origin + (index + 5).days)
|
|
||||||
end
|
|
||||||
|
|
||||||
{ tag: public_tag.id, external_tag: public_external.id }.each do |parameter, id|
|
|
||||||
[6, 5].each_with_index do |number, index|
|
|
||||||
get '/posts/versions', params: {
|
|
||||||
post: post_record.id, parameter => id, page: index + 1, limit: 1 }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
body = response.parsed_body
|
|
||||||
expect(body.fetch('count')).to eq(2)
|
|
||||||
expect(body.fetch('versions').pluck('version_no')).to eq([number])
|
|
||||||
snapshot = body.fetch('versions').first
|
|
||||||
type = index.zero? ? 'removed' : 'added'
|
|
||||||
expect(snapshot.fetch('tags')).to include(
|
|
||||||
a_hash_including('name' => 'public-history-tag', 'type' => type),
|
|
||||||
a_hash_including('name' => "nico:#{ public_external.name }", 'type' => type))
|
|
||||||
previous =
|
|
||||||
if index.zero?
|
|
||||||
'public snapshot 0'
|
|
||||||
elsif role == 'admin'
|
|
||||||
'version 4'
|
|
||||||
end
|
|
||||||
expect(snapshot.fetch('title').fetch('prev')).to eq(previous)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns an empty history with an exact count' do
|
|
||||||
get '/posts/versions', params: { post: other_post.id }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(response.parsed_body).to include('versions' => [], 'count' => 0,
|
|
||||||
'population_count' => 4)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'keeps counts and timestamp ties consistent across history pages' do
|
|
||||||
rows = 25.times.map do |index|
|
|
||||||
PostVersion.create!(post: post_record, version_no: index + 5, event_type: 'update',
|
|
||||||
title: 'page snapshot', url: post_record.url, tags: '', tags_json: [],
|
|
||||||
parent_post_ids: '', created_at: origin + 5.days)
|
|
||||||
end
|
|
||||||
rows << PostVersion.create!(post: other_post, version_no: 1, event_type: 'create',
|
|
||||||
title: 'another post', url: other_post.url, tags: '', tags_json: [],
|
|
||||||
parent_post_ids: '', created_at: origin + 5.days)
|
|
||||||
earlier = role == 'admin' ? [4, 3, 2, 1] : [3]
|
|
||||||
expected = rows.reverse.map { [_1.post_id, _1.version_no] } +
|
|
||||||
earlier.map { [post_record.id, _1] }
|
|
||||||
|
|
||||||
expected.each_slice(7).with_index do |page_rows, index|
|
|
||||||
get '/posts/versions', params: { page: index + 1, limit: 7 }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
body = response.parsed_body
|
|
||||||
expect(body.fetch('count')).to eq(expected.length)
|
|
||||||
expect(body.fetch('population_count')).to eq(30)
|
|
||||||
expect(body.fetch('versions').map { [_1.fetch('post_id'), _1.fetch('version_no')] })
|
|
||||||
.to eq(page_rows)
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/posts/versions', params: { page: 10, limit: 7 }
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expect(response.parsed_body).to include('versions' => [], 'count' => expected.length)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'loads each page with one authorised previous-snapshot join' do
|
|
||||||
[{ }, { tag: tag.id }, { external_tag: external.id }].each do |filters|
|
|
||||||
statements = []
|
|
||||||
listener = lambda do |_name, _started_at, _finished_at, _id, payload|
|
|
||||||
next if ['SCHEMA', 'TRANSACTION'].include?(payload[:name])
|
|
||||||
|
|
||||||
statements << payload[:sql] if payload[:sql].match?(/\ASELECT/i)
|
|
||||||
end
|
|
||||||
ActiveSupport::Notifications.subscribed(listener, 'sql.active_record') do
|
|
||||||
get '/posts/versions', params: { post: post_record.id, limit: 1 }.merge(filters)
|
|
||||||
end
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
history_queries = statements.select { _1.include?('post_versions') }
|
|
||||||
counts, details = history_queries.partition { _1.match?(/\ASELECT COUNT\(/i) }
|
|
||||||
expect(counts.length).to eq(2)
|
|
||||||
joins = counts.join(' ').scan('LEFT JOIN post_versions prev')
|
|
||||||
expect(joins.length).to eq(filters.empty? ? 0 : 1)
|
|
||||||
expect(details.length).to eq(1)
|
|
||||||
expect(details.first.scan('LEFT JOIN post_versions prev').length).to eq(1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'hides references to a discarded parent even after its restore' do
|
|
||||||
['discard', 'restore'].each_with_index do |event, index|
|
|
||||||
PostVersion.create!(post: other_post, version_no: index + 1, event_type: event,
|
|
||||||
title: 'parent snapshot', url: other_post.url, tags: '', tags_json: [],
|
|
||||||
parent_post_ids: '', created_at: origin + index.hours)
|
|
||||||
end
|
|
||||||
[other_post.id.to_s, ''].each_with_index do |parents, index|
|
|
||||||
PostVersion.create!(post: post_record, version_no: index + 5, event_type: 'update',
|
|
||||||
title: 'parent reference', url: post_record.url, tags: '', tags_json: [],
|
|
||||||
parent_post_ids: parents, created_at: origin + (index + 5).days)
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/posts/versions', params: { post: post_record.id }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
body = response.parsed_body
|
|
||||||
expected = role == 'admin' ? [6, 5, 4, 3, 2, 1] : [6, 3]
|
|
||||||
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
||||||
expect(body.fetch('count')).to eq(expected.length)
|
|
||||||
previous = body.fetch('versions').first.fetch('title').fetch('prev')
|
|
||||||
expect(previous).to eq(role == 'admin' ? 'parent reference' : nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'keeps restored tag references private' do
|
|
||||||
TagVersion.create!(tag:, version_no: 3, event_type: 'restore',
|
|
||||||
name: 'private-history-tag', category: 'general', aliases: '', parent_tag_ids: '',
|
|
||||||
names: { ja: { name: 'private-history-tag', script_code: 'Jpan',
|
|
||||||
auto_generated: false, aliases: [] } },
|
|
||||||
created_at: origin + 2.hours)
|
|
||||||
PostVersion.create!(post: post_record, version_no: 5, event_type: 'update',
|
|
||||||
title: 'restored tag reference', url: post_record.url, tags: '',
|
|
||||||
tags_json: [{ tag_id: tag.id, version_no: 3, sections: [] }],
|
|
||||||
parent_post_ids: '', created_at: origin + 5.days)
|
|
||||||
|
|
||||||
get '/posts/versions', params: { post: post_record.id }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expected = role == 'admin' ? [5, 4, 3, 2, 1] : [3]
|
|
||||||
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
||||||
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'uses the external linkage recorded at the post snapshot time' do
|
|
||||||
NicoTagVersion.create!(external_tag: external, version_no: 2, event_type: 'update',
|
|
||||||
linked_tag_ids: '', created_at: origin + 5.days)
|
|
||||||
PostVersion.create!(post: post_record, version_no: 5, event_type: 'update',
|
|
||||||
title: 'public external reference', url: post_record.url, tags: '',
|
|
||||||
tags_json: [{ external_tag_id: external.id }], parent_post_ids: '',
|
|
||||||
created_at: origin + 6.days)
|
|
||||||
|
|
||||||
get '/posts/versions', params: { post: post_record.id, external_tag: external.id }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
body = response.parsed_body
|
|
||||||
expected = role == 'admin' ? [5, 4] : [5]
|
|
||||||
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
||||||
expect(body.fetch('count')).to eq(expected.length)
|
|
||||||
latest = body.fetch('versions').first
|
|
||||||
previous = latest.fetch('title').fetch('prev')
|
|
||||||
expect(previous).to eq(role == 'admin' ? 'version 4' : nil)
|
|
||||||
expect(latest.fetch('tags')).to contain_exactly(a_hash_including(
|
|
||||||
'name' => 'nico:external-history-tag',
|
|
||||||
'type' => role == 'admin' ? 'context' : 'added'))
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'hides references to historical external discard events after restore' do
|
|
||||||
public_external = create(:external_tag)
|
|
||||||
# Discard/restore events can remain in history despite current event validation.
|
|
||||||
snapshots = ['create', 'discard', 'restore'].each_with_index.map do |event, index|
|
|
||||||
{ tag_id: public_external.id, version_no: index + 1, event_type: event,
|
|
||||||
linked_tag_ids: '', created_at: origin + index.days }
|
|
||||||
end
|
|
||||||
NicoTagVersion.insert_all!(snapshots)
|
|
||||||
[5, 6].each do |number|
|
|
||||||
PostVersion.create!(post: post_record, version_no: number, event_type: 'update',
|
|
||||||
title: 'external discard reference', url: post_record.url, tags: '',
|
|
||||||
tags_json: [{ external_tag_id: public_external.id }], parent_post_ids: '',
|
|
||||||
created_at: origin + number.days)
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/posts/versions', params: { post: post_record.id, external_tag: public_external.id }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:ok)
|
|
||||||
expected = role == 'admin' ? [6, 5] : []
|
|
||||||
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
||||||
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -516,6 +516,45 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
[:title, :url].each do |field|
|
||||||
|
context "when #{field} contains wildcards" do
|
||||||
|
let!(:search_posts) do
|
||||||
|
values = [
|
||||||
|
'needle%25end', 'needleX25end', 'needle_end',
|
||||||
|
'needleXend', 'needleABend', 'needle%25_end']
|
||||||
|
values.map.with_index do |value, index|
|
||||||
|
Post.create!(uploaded_user: user,
|
||||||
|
title: field == :title ? "prefix #{value} suffix" : 'url search post',
|
||||||
|
url: field == :url ? "https://example.com/prefix-#{value}-suffix" :
|
||||||
|
"https://example.com/title-search-#{index}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
{
|
||||||
|
'%' => [0, 5],
|
||||||
|
'_' => [2, 5],
|
||||||
|
'needle%25end' => [0],
|
||||||
|
'needle_end' => [2],
|
||||||
|
'needle*end' => [0, 1, 2, 3, 4, 5],
|
||||||
|
'needle?end' => [2, 3],
|
||||||
|
'needle??end' => [4],
|
||||||
|
'needle%*end' => [0, 5],
|
||||||
|
'needle_?nd' => [2],
|
||||||
|
'needle%25_end' => [5],
|
||||||
|
'needle%25_*end' => [5]
|
||||||
|
}.each do |query, expected_indices|
|
||||||
|
it "matches #{query.inspect} using only * and ? as wildcards" do
|
||||||
|
get '/posts', params: { field => query }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expected_ids = expected_indices.map { |i| search_posts[i].id }
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] }).to match_array(expected_ids)
|
||||||
|
expect(json.fetch('count')).to eq(expected_ids.size)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when created_from/created_to are provided' do
|
context 'when created_from/created_to are provided' do
|
||||||
let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) }
|
let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) }
|
||||||
let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }
|
let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }
|
||||||
|
|||||||
@@ -39,6 +39,46 @@ RSpec.describe 'Tags API', type: :request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe 'GET /tags' do
|
describe 'GET /tags' do
|
||||||
|
context 'when the name contains wildcards' do
|
||||||
|
let!(:tn) { create(:tag_name, name: 'background-tag') }
|
||||||
|
|
||||||
|
let(:names) do
|
||||||
|
['wild%end', 'wild_end', 'wildXend', 'wildABend', 'wild%_end']
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
names.each do |name|
|
||||||
|
create(:tag, primary_name: name)
|
||||||
|
create(:external_tag, name:)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
{
|
||||||
|
'%' => [0, 4],
|
||||||
|
'_' => [1, 4],
|
||||||
|
'wild%end' => [0],
|
||||||
|
'wild_end' => [1],
|
||||||
|
'wild*end' => [0, 1, 2, 3, 4],
|
||||||
|
'wild?end' => [0, 1, 2],
|
||||||
|
'wild??end' => [3, 4],
|
||||||
|
'wild%*end' => [0, 4],
|
||||||
|
'wild_?nd' => [1],
|
||||||
|
'wild%?end' => [4],
|
||||||
|
'nico:wild?end' => [0, 1, 2] }.each do |query, indices|
|
||||||
|
it "filters internal and external names with #{ query.inspect }" do
|
||||||
|
get '/tags', params: { name: query }
|
||||||
|
|
||||||
|
expected_names = indices.flat_map do |index|
|
||||||
|
name = names[index]
|
||||||
|
query.start_with?('nico:') ? ["nico:#{ name }"] : [name, "nico:#{ name }"]
|
||||||
|
end
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response_names).to match_array(expected_names)
|
||||||
|
expect(json.fetch('count')).to eq(expected_names.size)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'serialises descriptions and parents without loading legacy Wiki pages' do
|
it 'serialises descriptions and parents without loading legacy Wiki pages' do
|
||||||
TagDescription.create!(tag:, language_code: 'ja', body: '', version_no: 1)
|
TagDescription.create!(tag:, language_code: 'ja', body: '', version_no: 1)
|
||||||
TagDescription.create!(tag: tag2, language_code: 'ja', body: '', version_no: 1)
|
TagDescription.create!(tag: tag2, language_code: 'ja', body: '', version_no: 1)
|
||||||
|
|||||||
@@ -418,37 +418,6 @@ test ('YouTube posts return HTTP 200 with the embed rendered by SSR', async t =>
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test ('post history HTTP requests preserve filters for every role', async t => {
|
|
||||||
const accounts = [
|
|
||||||
{ role: 'guest', cookie: null },
|
|
||||||
{ role: 'member', cookie: await authenticatedCookie ('second-recovery-secret') },
|
|
||||||
{ role: 'admin', cookie: await authenticatedCookie () }]
|
|
||||||
for (const account of accounts) {
|
|
||||||
for (const prefix of ['', '/ja', '/ain-Kana']) {
|
|
||||||
await t.test (`${account.role} ${prefix || 'no locale prefix'}`, async () => {
|
|
||||||
for (const query of ['', '?id=1&page=2&limit=5']) {
|
|
||||||
const start = requests.length
|
|
||||||
const response = await fetch (
|
|
||||||
`${httpOrigin}${prefix}/posts/changes${query}`, {
|
|
||||||
headers: account.cookie == null ? {} : { Cookie: account.cookie } })
|
|
||||||
assert.equal (response.status, 200)
|
|
||||||
const html = await response.text ()
|
|
||||||
assert.match (html, /<table/)
|
|
||||||
assert.doesNotMatch (html, /Switched to client rendering|template id="B:/)
|
|
||||||
const calls = requests.slice (start).map (request => new URL (request.url, apiURL))
|
|
||||||
.filter (url => url.pathname === '/posts/versions')
|
|
||||||
assert.equal (calls.length, 1)
|
|
||||||
assert.equal (calls[0].searchParams.get ('post'), query ? '1' : null)
|
|
||||||
assert.equal (calls[0].searchParams.get ('page'), query ? '2' : '1')
|
|
||||||
assert.equal (calls[0].searchParams.get ('limit'), query ? '5' : '20')
|
|
||||||
assert.equal (calls[0].searchParams.get ('locale'), prefix === '/ain-Kana'
|
|
||||||
? 'ain-Kana' : 'ja')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
test ('parallel localized requests keep list data isolated', async () => {
|
test ('parallel localized requests keep list data isolated', async () => {
|
||||||
const start = requests.length
|
const start = requests.length
|
||||||
const pages = await Promise.all (['ja', 'ain-Kana', 'ja', 'ain-Kana'].map (locale =>
|
const pages = await Promise.all (['ja', 'ain-Kana', 'ja', 'ain-Kana'].map (locale =>
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { QueryClient } from '@tanstack/react-query'
|
|||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
import { prefetchForURL } from '@/lib/prefetchers'
|
import { prefetchForURL } from '@/lib/prefetchers'
|
||||||
import { postsKeys } from '@/lib/queryKeys'
|
|
||||||
|
|
||||||
const postsApi = vi.hoisted (() => ({
|
const postsApi = vi.hoisted (() => ({
|
||||||
fetchPost: vi.fn (),
|
fetchPost: vi.fn (),
|
||||||
@@ -89,63 +88,6 @@ describe ('prefetchForURL', () => {
|
|||||||
expect (postsApi.fetchPost).toHaveBeenCalledWith ('12', 'ja')
|
expect (postsApi.fetchPost).toHaveBeenCalledWith ('12', 'ja')
|
||||||
})
|
})
|
||||||
|
|
||||||
it.each ([
|
|
||||||
['/posts/changes', 'ja'], ['/ja/posts/changes', 'ja'],
|
|
||||||
['/ain-Kana/posts/changes', 'ain-Kana']] as const) (
|
|
||||||
'matches post history filters and cache key at %s', async (path, locale) => {
|
|
||||||
const client = qc ()
|
|
||||||
const params = { post: '12', page: 2, limit: 5 }
|
|
||||||
const history = { versions: [], count: 7 }
|
|
||||||
postsApi.fetchPostChanges.mockResolvedValueOnce (history)
|
|
||||||
|
|
||||||
await prefetchForURL (client, `${path}?id=12&page=2&limit=5`)
|
|
||||||
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenCalledExactlyOnceWith (params, locale)
|
|
||||||
expect (client.getQueryData (postsKeys.changes (params, locale))).toEqual (history)
|
|
||||||
expect (client.getQueryCache ().getAll ()).toHaveLength (1)
|
|
||||||
})
|
|
||||||
|
|
||||||
it ('preserves a tag filter when prefetching one post history', async () => {
|
|
||||||
const client = qc ()
|
|
||||||
const params = { post: '12', tag: '7', page: 1, limit: 20 }
|
|
||||||
|
|
||||||
await prefetchForURL (client, '/ja/posts/changes?id=12&tag=7')
|
|
||||||
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenCalledExactlyOnceWith (params, 'ja')
|
|
||||||
expect (client.getQueryData (postsKeys.changes (params, 'ja')))
|
|
||||||
.toEqual ({ versions: [], count: 0 })
|
|
||||||
})
|
|
||||||
|
|
||||||
it ('preserves an external tag filter when prefetching one post history', async () => {
|
|
||||||
const client = qc ()
|
|
||||||
const params = { post: '12', externalTag: '7', page: 1, limit: 20 }
|
|
||||||
|
|
||||||
await prefetchForURL (client, '/ja/posts/changes?id=12&external_tag=7')
|
|
||||||
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenCalledExactlyOnceWith (params, 'ja')
|
|
||||||
expect (client.getQueryData (postsKeys.changes (params, 'ja')))
|
|
||||||
.toEqual ({ versions: [], count: 0 })
|
|
||||||
})
|
|
||||||
|
|
||||||
it ('keeps one post history separate from unfiltered history in the cache', async () => {
|
|
||||||
const client = qc ()
|
|
||||||
const params = { page: 2, limit: 5 }
|
|
||||||
const postParams = { ...params, post: '12' }
|
|
||||||
const history = { versions: [], count: 7 }
|
|
||||||
postsApi.fetchPostChanges.mockResolvedValueOnce (history)
|
|
||||||
|
|
||||||
await prefetchForURL (client, '/ja/posts/changes?id=12&page=2&limit=5')
|
|
||||||
await prefetchForURL (client, '/ja/posts/changes?page=2&limit=5')
|
|
||||||
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenCalledTimes (2)
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenNthCalledWith (1, postParams, 'ja')
|
|
||||||
expect (postsApi.fetchPostChanges).toHaveBeenNthCalledWith (2, params, 'ja')
|
|
||||||
expect (client.getQueryData (postsKeys.changes (postParams, 'ja'))).toEqual (history)
|
|
||||||
expect (client.getQueryData (postsKeys.changes (params, 'ja')))
|
|
||||||
.toEqual ({ versions: [], count: 0 })
|
|
||||||
expect (client.getQueryCache ().getAll ()).toHaveLength (2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it ('prefetches tag indexes from query parameters', async () => {
|
it ('prefetches tag indexes from query parameters', async () => {
|
||||||
await prefetchForURL (
|
await prefetchForURL (
|
||||||
qc (),
|
qc (),
|
||||||
|
|||||||
@@ -214,11 +214,11 @@ const prefetchPostChanges: Prefetcher = async (qc, url, locale) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: postsKeys.changes ({ ...(id && { post: id }),
|
queryKey: postsKeys.changes ({ ...(id && { id }),
|
||||||
...(tag && { tag }),
|
...(tag && { tag }),
|
||||||
...(externalTag && { externalTag }),
|
...(externalTag && { externalTag }),
|
||||||
page, limit }, locale),
|
page, limit }, locale),
|
||||||
queryFn: () => fetchPostChanges ({ ...(id && { post: id }),
|
queryFn: () => fetchPostChanges ({ ...(id && { id }),
|
||||||
...(tag && { tag }),
|
...(tag && { tag }),
|
||||||
...(externalTag && { externalTag }),
|
...(externalTag && { externalTag }),
|
||||||
page, limit }, locale) })
|
page, limit }, locale) })
|
||||||
|
|||||||
@@ -21,9 +21,7 @@ if (typeof originalTransform === 'function')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineConfig (({ command }) => ({
|
export default defineConfig ({
|
||||||
// Production React does not provide jsxDEV, regardless of the build environment.
|
|
||||||
esbuild: command === 'build' ? { jsxDev: false } : undefined,
|
|
||||||
plugins: [{ name: 'request-render-context', enforce: 'pre',
|
plugins: [{ name: 'request-render-context', enforce: 'pre',
|
||||||
resolveId (id, _importer, options) {
|
resolveId (id, _importer, options) {
|
||||||
const contextPath = path.resolve (__dirname, './src/lib/renderContext')
|
const contextPath = path.resolve (__dirname, './src/lib/renderContext')
|
||||||
@@ -42,4 +40,4 @@ export default defineConfig (({ command }) => ({
|
|||||||
rewrite: requestPath => requestPath.replace (/^\/api/, ''),
|
rewrite: requestPath => requestPath.replace (/^\/api/, ''),
|
||||||
secure: false } },
|
secure: false } },
|
||||||
watch: { usePolling: true,
|
watch: { usePolling: true,
|
||||||
interval: 100 } } }))
|
interval: 100 } } })
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ import { defineConfig, mergeConfig } from 'vitest/config'
|
|||||||
import viteConfig from './vite.config'
|
import viteConfig from './vite.config'
|
||||||
|
|
||||||
|
|
||||||
export default defineConfig (env => mergeConfig (
|
export default mergeConfig (
|
||||||
viteConfig (env),
|
viteConfig,
|
||||||
{ test: { environment: 'jsdom',
|
defineConfig ({
|
||||||
testTimeout: 30_000,
|
test: { environment: 'jsdom',
|
||||||
exclude: ['**/node_modules/**', '**/dist/**', 'server.ssr.test.mjs'],
|
testTimeout: 30_000,
|
||||||
setupFiles: './src/test/setup.ts',
|
exclude: ['**/node_modules/**', '**/dist/**', 'server.ssr.test.mjs'],
|
||||||
globals: false } }))
|
setupFiles: './src/test/setup.ts',
|
||||||
|
globals: false } }))
|
||||||
|
|||||||
新しいイシューから参照
ユーザーをブロックする