WIP: preserve failed Codex job issue-205-idew24v8

このコミットが含まれているのは:
Codex Night Worker
2026-11-19 12:29:23 +09:00
コミット efcc363537
17個のファイルの変更、639行の追加、2行の削除
+33
ファイルの表示
@@ -0,0 +1,33 @@
class SubmissionsController < ApplicationController
def index
page_value = params[:page].to_s
page = page_value.match?(/\A[1-9][0-9]{0,5}\z/) ? page_value.to_i : 1
scope = Submission.public_listing
submissions = scope.includes(:user).order(created_at: :desc, id: :desc)
.limit(Submission::PAGE_SIZE).offset((page - 1) * Submission::PAGE_SIZE)
render json: { submissions: submissions.map { repr(_1) },
count: scope.count,
page_size: Submission::PAGE_SIZE }
end
def create
return head :unauthorized unless current_user
submission = Submission.create!(
params.permit(:category, :target_id, :content).merge(user: current_user))
render json: repr(submission), status: :created
end
private
def repr(submission)
{ id: submission.id,
category: submission.category,
target_id: submission.target_id,
content: submission.content,
user: UserRepr.base(submission.user),
created_at: submission.created_at }
end
end
+49
ファイルの表示
@@ -0,0 +1,49 @@
class Submission < ApplicationRecord
TARGETS = { 'post_removal' => [Post, PostVersion, :post_id],
'tag_removal' => [Tag, TagVersion, :tag_id],
'material_removal' => [Material, MaterialVersion, :material_id] }.freeze
CATEGORIES = (TARGETS.keys + ['opinion']).freeze
PAGE_SIZE = 20
belongs_to :user
validates :category, inclusion: { in: CATEGORIES }
validates :content, presence: true, length: { maximum: 5000 }
validates :target_id, absence: true, if: -> { category == 'opinion' }
validates :target_id, numericality: { only_integer: true,
greater_than: 0, less_than: 2**63 },
if: -> { TARGETS.key?(category) }
validate :validate_target
def self.target_scope(category)
model, version_model = TARGETS.fetch(category)
HistoryAccess.current(model.all, version_model)
end
def self.public_listing
return all if HistoryAccess.admin?
TARGETS.keys.reduce(where(category: 'opinion')) do |scope, category|
_model, version_model, key = TARGETS.fetch(category)
# Keep pre-removal submissions private even after the target is restored.
boundaries = version_model.where(event_type: 'discard').where(
version_model.arel_table[:version_no].gteq(arel_table[:target_version_no]))
scope.or(where(category:, target_id: target_scope(category).select(:id))
.where.not(target_version_no: nil)
.where.not(target_id: boundaries.select(key)))
end
end
private
def validate_target
return unless TARGETS.key?(category) && errors[:target_id].empty?
target = self.class.target_scope(category).find_by(id: target_id)
if target
self.target_version_no = target.version_no if new_record?
return
end
errors.add(:target_id, :invalid)
end
end
+1
ファイルの表示
@@ -15,6 +15,7 @@ Rails.application.routes.draw do
get 'counts/tags', to: 'counts_tags#index'
resources :locales, only: [:index]
resources :submissions, only: [:index, :create]
resources :sitemap_entries, only: [:index, :show], param: :resource
resources :tag_groups, only: [:index, :show, :create, :update]
+13
ファイルの表示
@@ -0,0 +1,13 @@
class CreateSubmissions < ActiveRecord::Migration[8.0]
def change
create_table :submissions do |t|
t.references :user, null: false, foreign_key: true
t.string :category, null: false
t.bigint :target_id
t.integer :target_version_no
t.text :content, null: false
t.timestamps
t.index [:created_at, :id]
end
end
end
生成ファイル
+15 -1
ファイルの表示
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_11_08_020000) do
ActiveRecord::Schema[8.0].define(version: 2026_11_18_000000) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -439,6 +439,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_11_08_020000) do
t.index ["user_id"], name: "index_settings_on_user_id", unique: true
end
create_table "submissions", charset: "utf8mb4",
collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "category", null: false
t.bigint "target_id"
t.integer "target_version_no"
t.text "content", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_at", "id"], name: "index_submissions_on_created_at_and_id"
t.index ["user_id"], name: "index_submissions_on_user_id"
end
create_table "tag_description_versions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "tag_id", null: false
t.string "language_code", limit: 16, null: false
@@ -816,6 +829,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_11_08_020000) do
add_foreign_key "post_versions", "users", column: "created_by_user_id"
add_foreign_key "posts", "users", column: "uploaded_user_id"
add_foreign_key "settings", "users"
add_foreign_key "submissions", "users"
add_foreign_key "tag_description_versions", "tag_description_versions", column: ["tag_id", "source_language_code", "source_version_no"], primary_key: ["tag_id", "language_code", "version_no"]
add_foreign_key "tag_descriptions", "languages", column: "language_code", primary_key: "code"
add_foreign_key "tag_descriptions", "tag_description_versions", column: ["tag_id", "source_language_code", "source_version_no"], primary_key: ["tag_id", "language_code", "version_no"]
+190
ファイルの表示
@@ -0,0 +1,190 @@
require 'rails_helper'
RSpec.describe 'Submissions', type: :request do
let(:user) { create(:user) }
let(:admin) { create(:user, :admin) }
let(:opinion) { { category: 'opinion', content: '御意見です.' } }
def record_version(target, event_type)
recorder = { Post => [PostVersionRecorder, :post],
Tag => [TagVersionRecorder, :tag],
Material => [MaterialVersionRecorder, :material] }.fetch(target.class)
recorder.first.record!(**{ recorder.last => target },
event_type:, created_by_user: admin)
target.reload
end
describe 'POST /submissions' do
it 'requires an authenticated user' do
expect { post '/submissions', params: opinion }
.not_to change(Submission, :count)
expect(response).to have_http_status(:unauthorized)
end
['guest', 'member', 'admin'].each do |role|
it "accepts opinions from #{ role } users using server identity" do
actor = create(:user, role:)
expect {
post '/submissions', params: opinion.merge(user_id: admin.id, role: 'admin'),
headers: cookie_headers(actor)
}.to change(Submission, :count).by(1)
expect(response).to have_http_status(:created)
expect(Submission.last.user).to eq(actor)
expect(Submission.last.target_id).to be_nil
expect(response.parsed_body.fetch('user')).to eq(
{ 'id' => actor.id, 'name' => actor.name })
expect(response.body).not_to include(actor.inheritance_code)
end
end
[{ content: '' }, { content: ' ' }, { content: 'x' * 5001 },
{ category: 'unknown' }, { target_id: 1 }].each do |attributes|
it "rejects invalid opinion attributes #{ attributes.keys }" do
sign_in_as(user)
expect { post '/submissions', params: opinion.merge(attributes) }
.not_to change(Submission, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body.fetch('type')).to eq('validation_error')
end
end
it 'rejects banned users' do
sign_in_as(create(:user, :banned))
expect { post '/submissions', params: opinion }
.not_to change(Submission, :count)
expect(response).to have_http_status(:forbidden)
end
it 'rejects banned IP addresses for reads and writes' do
create(:ip_address, :banned, ip_address: IPAddr.new('192.0.2.1').hton)
headers = { 'REMOTE_ADDR' => '192.0.2.1' }
sign_in_as(user)
expect { post '/submissions', params: opinion, headers: }
.not_to change(Submission, :count)
expect(response).to have_http_status(:forbidden)
get '/submissions', headers:
expect(response).to have_http_status(:forbidden)
end
end
describe 'GET /submissions' do
it 'allows anonymous browsing without exposing user credentials' do
submission = Submission.create!(**opinion, user:)
get '/submissions'
expect(response).to have_http_status(:ok)
expect(response.parsed_body.fetch('submissions')).to contain_exactly(
include('id' => submission.id, 'content' => opinion[:content],
'user' => { 'id' => user.id, 'name' => user.name }))
expect(response.body).not_to include(user.inheritance_code)
expect(response.headers['Cache-Control']).to include('no-store')
end
it 'paginates newest first with stable ordering and a public count' do
submissions = Array.new(21) { Submission.create!(**opinion, user:) }
get '/submissions'
expect(response.parsed_body.fetch('count')).to eq(21)
expect(response.parsed_body.fetch('page_size')).to eq(20)
expect(response.parsed_body.fetch('submissions').pluck('id'))
.to eq(submissions.reverse.first(20).map(&:id))
get '/submissions', params: { page: 2 }
expect(response.parsed_body.fetch('submissions').pluck('id'))
.to eq([submissions.first.id])
end
end
['post_removal', 'tag_removal', 'material_removal'].each do |category|
context category do
let(:target) do
case category
when 'post_removal' then create(:post)
when 'tag_removal' then create(:tag)
when 'material_removal' then Material.create!(url: 'https://example.com/material')
end
end
let(:attributes) { { category:, target_id: target.id, content: '除票を申請します.' } }
it 'stores a guest request without changing the target or its history' do
sign_in_as(user)
original = target.attributes
version_model = Submission::TARGETS.fetch(category)[1]
expect {
post '/submissions', params: attributes.merge(target_version_no: 99999)
}.not_to change(version_model, :count)
expect(response).to have_http_status(:created)
expect(target.reload.attributes).to eq(original)
expect(Submission.last).to have_attributes(
category:, target_id: target.id, target_version_no: target.version_no, user:)
sign_out
get '/submissions'
expect(response.parsed_body.fetch('submissions').pluck('id'))
.to eq([Submission.last.id])
end
[nil, 0, -1, '1.5', '1x', 2**63].each do |target_id|
it "rejects invalid target ID #{ target_id.inspect }" do
sign_in_as(user)
expect { post '/submissions', params: attributes.merge(target_id:) }
.not_to change(Submission, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body.fetch('errors')).to have_key('target_id')
end
end
it 'rejects a missing target' do
sign_in_as(user)
post '/submissions', params: attributes.merge(target_id: 2**62)
expect(response).to have_http_status(:unprocessable_entity)
end
it 'hides pre-removal requests after restores and every later removal' do
record_version(target, 'create')
original = Submission.create!(**attributes, user:)
record_version(target, 'discard')
sign_in_as(user)
post '/submissions', params: attributes
expect(response).to have_http_status(:unprocessable_entity)
boundary = HistoryAccess.set(user: admin) { Submission.create!(**attributes, user: admin) }
record_version(target, 'restore')
restored = Submission.create!(**attributes, user:)
[nil, user, create(:user, :member)].each do |actor|
sign_in_as(actor)
get '/submissions', params: { role: 'admin' }
expect(response.parsed_body.fetch('submissions').pluck('id')).to eq([restored.id])
expect(response.parsed_body.fetch('count')).to eq(1)
end
record_version(target, 'discard')
record_version(target, 'restore')
sign_out
get '/submissions'
expect(response.parsed_body.fetch('submissions')).to eq([])
expect(response.parsed_body.fetch('count')).to eq(0)
sign_in_as(admin)
get '/submissions'
expect(response.parsed_body.fetch('submissions').pluck('id'))
.to eq([restored.id, boundary.id, original.id])
expect(response.parsed_body.fetch('count')).to eq(3)
end
end
end
end