From 9d42fc7f5fec7972398f93a261bf707a8927c178 Mon Sep 17 00:00:00 2001 From: Codex Night Worker Date: Fri, 20 Nov 2026 00:50:34 +0900 Subject: [PATCH] WIP: unverified Codex job issue-485-z473g006 --- .../controllers/post_versions_controller.rb | 15 ++-- .../requests/post_history_privacy_spec.rb | 78 +++++++++++++++---- frontend/src/lib/prefetchers.test.ts | 21 ++++- 3 files changed, 90 insertions(+), 24 deletions(-) diff --git a/backend/app/controllers/post_versions_controller.rb b/backend/app/controllers/post_versions_controller.rb index d80bee5..7496ef8 100644 --- a/backend/app/controllers/post_versions_controller.rb +++ b/backend/app/controllers/post_versions_controller.rb @@ -16,7 +16,7 @@ class PostVersionsController < ApplicationController q = HistoryAccess.visible(PostVersion.all) q = q.where('post_versions.post_id = ?', post_id) if post_id - q = q.with_previous_version if tag_id || external_tag_id + q = q.merge(version_scope) if tag_id || external_tag_id if external_tag_id || (tag_id && !(Tag.exists?(id: tag_id))) q = q.where('JSON_CONTAINS(post_versions.tags_json,' + "JSON_OBJECT('external_tag_id', #{ external_tag_id || tag_id })) " + @@ -29,11 +29,16 @@ class PostVersionsController < ApplicationController count = q.except(:select, :order, :limit, :offset).count - # Apply visibility and pagination before joining the previous snapshots. + # Reuse the authorised previous snapshot when it is needed for filtering. page_scope = q.order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC')) - .limit(limit).offset(offset).select('post_versions.*') - versions = version_scope.from(page_scope, :post_versions) - .order(Arel.sql('post_versions.created_at DESC, post_versions.id DESC')) + .limit(limit).offset(offset) + versions = + if tag_id || external_tag_id + page_scope + 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), count:, population_count: HistoryAccess.population_count(PostVersion) } diff --git a/backend/spec/requests/post_history_privacy_spec.rb b/backend/spec/requests/post_history_privacy_spec.rb index d0f024b..d17de9b 100644 --- a/backend/spec/requests/post_history_privacy_spec.rb +++ b/backend/spec/requests/post_history_privacy_spec.rb @@ -137,6 +137,50 @@ RSpec.describe 'Post history privacy', type: :request do expect(response.parsed_body.fetch('count')).to eq(expected.length) end + it 'paginates public tag additions and removals using the same exact count' do + public_tag = create(:tag, primary_name: 'public-history-tag') + 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 } @@ -174,23 +218,27 @@ RSpec.describe 'Post history privacy', type: :request do expect(response.parsed_body).to include('versions' => [], 'count' => expected.length) end - it 'loads the page and its authorised diff in one data query' do - statements = [] - listener = lambda do |_name, _start, _finish, _id, payload| - next if ['SCHEMA', 'TRANSACTION'].include?(payload[:name]) + 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 } - end + 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) - expect(counts.join(' ')).not_to include('LEFT JOIN post_versions prev') - expect(details.length).to eq(1) + 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 diff --git a/frontend/src/lib/prefetchers.test.ts b/frontend/src/lib/prefetchers.test.ts index 232aea3..cc6b3f6 100644 --- a/frontend/src/lib/prefetchers.test.ts +++ b/frontend/src/lib/prefetchers.test.ts @@ -89,8 +89,10 @@ describe ('prefetchForURL', () => { expect (postsApi.fetchPost).toHaveBeenCalledWith ('12', 'ja') }) - it.each (['/posts/changes', '/ja/posts/changes']) ( - 'prefetches post history with matching filters and cache key at %s', async path => { + 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 } @@ -98,8 +100,8 @@ describe ('prefetchForURL', () => { await prefetchForURL (client, `${path}?id=12&page=2&limit=5`) - expect (postsApi.fetchPostChanges).toHaveBeenCalledExactlyOnceWith (params, 'ja') - expect (client.getQueryData (postsKeys.changes (params, 'ja'))).toEqual (history) + expect (postsApi.fetchPostChanges).toHaveBeenCalledExactlyOnceWith (params, locale) + expect (client.getQueryData (postsKeys.changes (params, locale))).toEqual (history) expect (client.getQueryCache ().getAll ()).toHaveLength (1) }) @@ -114,6 +116,17 @@ describe ('prefetchForURL', () => { .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 ('prefetches unfiltered post history with its own cache key', async () => { const client = qc () const params = { page: 2, limit: 5 }