mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 01:31:35 +08:00
DEV: Extract theme resolution to a helper (#27426)
This ensures that the theme id is resolved as early as possible in the request cycle. This is necessary for the custom homepage to skip preloading the wrong data.
This commit is contained in:
36
lib/theme_resolver.rb
Normal file
36
lib/theme_resolver.rb
Normal file
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ThemeResolver
|
||||
def self.resolve_theme_id(request, guardian, current_user)
|
||||
return request.env[:resolved_theme_id] if request.env[:resolved_theme_id] != nil
|
||||
|
||||
theme_id = nil
|
||||
|
||||
if (preview_theme_id = request[:preview_theme_id]&.to_i) &&
|
||||
guardian.allow_themes?([preview_theme_id], include_preview: true)
|
||||
theme_id = preview_theme_id
|
||||
end
|
||||
|
||||
user_option = current_user&.user_option
|
||||
|
||||
if theme_id.blank? && request.cookie_jar[:theme_ids].present?
|
||||
ids, seq = request.cookie_jar[:theme_ids]&.split("|")
|
||||
id = ids&.split(",")&.map(&:to_i)&.first
|
||||
if id.present? && seq && seq.to_i == user_option&.theme_key_seq.to_i
|
||||
theme_id = id if guardian.allow_themes?([id])
|
||||
end
|
||||
end
|
||||
|
||||
if theme_id.blank?
|
||||
ids = user_option&.theme_ids || []
|
||||
theme_id = ids.first if guardian.allow_themes?(ids)
|
||||
end
|
||||
|
||||
if theme_id.blank? && SiteSetting.default_theme_id != -1 &&
|
||||
guardian.allow_themes?([SiteSetting.default_theme_id])
|
||||
theme_id = SiteSetting.default_theme_id
|
||||
end
|
||||
|
||||
request.env[:resolved_theme_id] = theme_id
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user