WIP: preserve failed Codex job issue-205-idew24v8
このコミットが含まれているのは:
@@ -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
|
||||
@@ -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
|
||||
新しいイシューから参照
ユーザーをブロックする