0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/app/models/category_setting.rb
Isaac Janzen 55ecb9feae
FIX: Handle invalid posting-review enum values in category update endpoint (#42196)
## Summary

Correctly rescue ArgumentError when invalid topic_posting_review_mode or
reply_posting_review_mode values are passed to the category update
endpoint, returning a 422 validation response instead of an internal
server error. The patch wraps cat.update(category_params) in a rescue
block that returns the enum error message with HTTP 422, matching the
existing handling in the category create endpoint.

## Source

- Patch Triage: https://patch.discourse.org/patch-triage/1555

Co-authored-by: discourse-patch-triage
<272280883+discourse-patch-triage[bot]@users.noreply.github.com>
2026-07-31 10:10:35 -05:00

76 lines
2.5 KiB
Ruby
Vendored

# frozen_string_literal: true
class CategorySetting < ActiveRecord::Base
# TODO: drop columns require_topic_approval, require_reply_approval in a future migration
self.ignored_columns += %i[require_topic_approval require_reply_approval]
belongs_to :category
enum :topic_posting_review_mode,
{ no_one: 0, everyone: 1, everyone_except: 2, no_one_except: 3 },
prefix: true,
validate: true
enum :reply_posting_review_mode,
{ no_one: 0, everyone: 1, everyone_except: 2, no_one_except: 3 },
prefix: true,
validate: true
after_save :clear_nested_replies_conversion_completed,
if: :saved_change_to_nested_replies_default?
def require_topic_approval=(value)
self.topic_posting_review_mode =
ActiveModel::Type::Boolean.new.cast(value) ? :everyone : :no_one
end
def require_reply_approval=(value)
self.reply_posting_review_mode =
ActiveModel::Type::Boolean.new.cast(value) ? :everyone : :no_one
end
alias_method :require_topic_approval, :topic_posting_review_mode_everyone?
alias_method :require_topic_approval?, :topic_posting_review_mode_everyone?
alias_method :require_reply_approval, :reply_posting_review_mode_everyone?
alias_method :require_reply_approval?, :reply_posting_review_mode_everyone?
GROUP_BASED_MODES = %w[everyone_except no_one_except].freeze
validates :num_auto_bump_daily,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0,
allow_nil: true,
}
validates :auto_bump_cooldown_days,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0,
allow_nil: true,
}
private
def clear_nested_replies_conversion_completed
category.clear_nested_replies_conversion_completed! unless nested_replies_default?
end
end
# == Schema Information
#
# Table name: category_settings
#
# id :bigint not null, primary key
# auto_bump_cooldown_days :integer default(1)
# nested_replies_default :boolean default(FALSE), not null
# num_auto_bump_daily :integer default(0)
# reply_posting_review_mode :integer default("no_one"), not null
# topic_posting_review_mode :integer default("no_one"), not null
# created_at :datetime not null
# updated_at :datetime not null
# category_id :bigint not null
#
# Indexes
#
# index_category_settings_on_category_id (category_id) UNIQUE
#