mirror of
https://github.com/discourse/discourse.git
synced 2026-08-06 13:08:40 +08:00
## Summary - enforce the resulting active-link count while holding the sidebar section row lock - scope the validation to sidebar updates so Community reset and unrelated model saves do not load or retain stale link associations - allow existing over-limit sections to make non-growing and deletion-based repairs - use a translatable ActiveRecord error key This replaces #42277 after its revert in #42287. ## Source - Patch Triage: https://patch.discourse.org/patch-triage/1583
113 lines
3.3 KiB
Ruby
Vendored
113 lines
3.3 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class SidebarSection < ActiveRecord::Base
|
|
include Localizable
|
|
|
|
MAX_TITLE_LENGTH = 30
|
|
MAX_USER_CATEGORY_LINKS = 100
|
|
|
|
belongs_to :user
|
|
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
|
|
|
|
has_many :sidebar_urls,
|
|
through: :sidebar_section_links,
|
|
source: :linkable,
|
|
source_type: "SidebarUrl"
|
|
|
|
accepts_nested_attributes_for :sidebar_urls,
|
|
allow_destroy: true,
|
|
limit: -> { SiteSetting.max_sidebar_section_links }
|
|
accepts_nested_attributes_for :localizations, allow_destroy: true
|
|
|
|
before_validation :set_default_locale
|
|
before_save :set_system_user_for_public_section
|
|
|
|
validates :title,
|
|
presence: true,
|
|
uniqueness: {
|
|
scope: %i[user_id],
|
|
},
|
|
length: {
|
|
maximum: MAX_TITLE_LENGTH,
|
|
}
|
|
|
|
validates :locale, presence: true, length: { maximum: 20 }
|
|
validate :sidebar_urls_count_within_limit, on: :sidebar_section_update
|
|
|
|
scope :public_sections, -> { where("public") }
|
|
scope :custom_sections, -> { where(section_type: nil) }
|
|
enum :section_type, { community: 0 }, scopes: false, suffix: true
|
|
|
|
def custom_section?
|
|
section_type.blank?
|
|
end
|
|
|
|
def community_section?
|
|
section_type == "community"
|
|
end
|
|
|
|
def reset_community!
|
|
ActiveRecord::Base.transaction do
|
|
update!(title: "Community")
|
|
sidebar_section_links.destroy_all
|
|
community_urls =
|
|
SidebarUrl::COMMUNITY_SECTION_LINKS.map do |url_data|
|
|
"('#{url_data[:name]}', '#{url_data[:path]}', '#{url_data[:icon]}', '#{url_data[:segment]}', false, now(), now())"
|
|
end
|
|
|
|
result = DB.query <<~SQL
|
|
INSERT INTO sidebar_urls(name, value, icon, segment, external, created_at, updated_at)
|
|
VALUES #{community_urls.join(",")}
|
|
RETURNING sidebar_urls.id
|
|
SQL
|
|
|
|
sidebar_section_links =
|
|
result.map.with_index do |url, index|
|
|
"(-1, #{url.id}, 'SidebarUrl', #{id}, #{index}, now(), now())"
|
|
end
|
|
|
|
DB.query <<~SQL
|
|
INSERT INTO sidebar_section_links(user_id, linkable_id, linkable_type, sidebar_section_id, position, created_at, updated_at)
|
|
VALUES #{sidebar_section_links.join(",")}
|
|
SQL
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def sidebar_urls_count_within_limit
|
|
count = sidebar_urls.reject(&:marked_for_destruction?).size
|
|
persisted_count = sidebar_urls.count(&:persisted?)
|
|
limit = SiteSetting.max_sidebar_section_links
|
|
return if count <= [limit, persisted_count].max
|
|
|
|
errors.add(:base, :too_many_sidebar_urls, limit:, count:)
|
|
end
|
|
|
|
def set_system_user_for_public_section
|
|
self.user_id = Discourse.system_user.id if public
|
|
end
|
|
|
|
def set_default_locale
|
|
self.locale = SiteSetting.default_locale.to_s if locale.blank?
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: sidebar_sections
|
|
#
|
|
# id :bigint not null, primary key
|
|
# locale :string(20)
|
|
# public :boolean default(FALSE), not null
|
|
# section_type :integer
|
|
# title :string(30) not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# user_id :integer not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_sidebar_sections_on_section_type (section_type) UNIQUE
|
|
# index_sidebar_sections_on_user_id_and_title (user_id,title) UNIQUE
|
|
#
|