2020-06-03 14:45:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BootstrapController < ApplicationController
|
|
|
|
include ApplicationHelper
|
|
|
|
|
2022-10-19 13:10:06 -04:00
|
|
|
skip_before_action :redirect_to_login_if_required, :check_xhr
|
2021-06-10 09:36:41 -04:00
|
|
|
|
2020-06-03 14:45:23 -04:00
|
|
|
# This endpoint allows us to produce the data required to start up Discourse via JSON API,
|
|
|
|
# so that you don't have to scrape the HTML for `data-*` payloads
|
|
|
|
def index
|
|
|
|
locale = script_asset_path("locales/#{I18n.locale}")
|
|
|
|
|
|
|
|
preload_anonymous_data
|
|
|
|
if current_user
|
|
|
|
current_user.sync_notification_channel_position
|
|
|
|
preload_current_user_data
|
|
|
|
end
|
|
|
|
|
2020-09-04 14:09:55 -04:00
|
|
|
@stylesheets = []
|
2021-07-01 04:58:26 -04:00
|
|
|
|
|
|
|
add_scheme(scheme_id, "all", "light-scheme")
|
|
|
|
add_scheme(dark_scheme_id, "(prefers-color-scheme: dark)", "dark-scheme")
|
|
|
|
|
2020-09-04 14:09:55 -04:00
|
|
|
if rtl?
|
|
|
|
add_style(mobile_view? ? :mobile_rtl : :desktop_rtl)
|
|
|
|
else
|
|
|
|
add_style(mobile_view? ? :mobile : :desktop)
|
|
|
|
end
|
2023-05-31 22:27:11 -04:00
|
|
|
add_style(rtl? ? :admin_rtl : :admin) if staff?
|
|
|
|
add_style(rtl? ? :wizard_rtl : :wizard) if admin?
|
2021-12-01 11:10:40 -05:00
|
|
|
|
|
|
|
assets_fake_request = ActionDispatch::Request.new(request.env.dup)
|
|
|
|
assets_for_url = params[:for_url]
|
|
|
|
if assets_for_url
|
|
|
|
path, query = assets_for_url.split("?", 2)
|
|
|
|
assets_fake_request.env["PATH_INFO"] = path
|
|
|
|
assets_fake_request.env["QUERY_STRING"] = query
|
|
|
|
end
|
|
|
|
|
2020-09-04 14:09:55 -04:00
|
|
|
Discourse
|
|
|
|
.find_plugin_css_assets(
|
|
|
|
include_official: allow_plugins?,
|
|
|
|
include_unofficial: allow_third_party_plugins?,
|
|
|
|
mobile_view: mobile_view?,
|
|
|
|
desktop_view: !mobile_view?,
|
2021-12-01 11:10:40 -05:00
|
|
|
request: assets_fake_request,
|
2023-05-31 22:27:11 -04:00
|
|
|
rtl: rtl?,
|
2020-09-04 14:09:55 -04:00
|
|
|
)
|
2020-11-27 11:54:25 -05:00
|
|
|
.each { |file| add_style(file, plugin: true) }
|
2021-06-15 02:57:17 -04:00
|
|
|
add_style(mobile_view? ? :mobile_theme : :desktop_theme) if theme_id.present?
|
2020-09-04 14:09:55 -04:00
|
|
|
|
2020-09-23 14:48:52 -04:00
|
|
|
extra_locales = []
|
|
|
|
if ExtraLocalesController.client_overrides_exist?
|
|
|
|
extra_locales << ExtraLocalesController.url("overrides")
|
|
|
|
end
|
2022-06-17 08:50:21 -04:00
|
|
|
|
2020-09-23 14:48:52 -04:00
|
|
|
extra_locales << ExtraLocalesController.url("admin") if staff?
|
|
|
|
|
2022-06-17 08:50:21 -04:00
|
|
|
extra_locales << ExtraLocalesController.url("wizard") if admin?
|
|
|
|
|
2020-11-27 11:54:25 -05:00
|
|
|
plugin_js =
|
|
|
|
Discourse
|
|
|
|
.find_plugin_js_assets(
|
|
|
|
include_official: allow_plugins?,
|
|
|
|
include_unofficial: allow_third_party_plugins?,
|
2021-12-01 11:10:40 -05:00
|
|
|
request: assets_fake_request,
|
2020-11-27 11:54:25 -05:00
|
|
|
)
|
|
|
|
.map { |f| script_asset_path(f) }
|
|
|
|
|
2022-05-31 20:49:53 -04:00
|
|
|
plugin_test_js =
|
|
|
|
if Rails.env != "production"
|
|
|
|
script_asset_path("plugin-tests")
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2020-06-03 14:45:23 -04:00
|
|
|
bootstrap = {
|
2021-06-17 22:16:26 -04:00
|
|
|
theme_id: theme_id,
|
2021-07-02 10:43:10 -04:00
|
|
|
theme_color: "##{ColorScheme.hex_for_name("header_background", scheme_id)}",
|
2020-06-03 14:45:23 -04:00
|
|
|
title: SiteSetting.title,
|
|
|
|
current_homepage: current_homepage,
|
2020-09-04 14:09:55 -04:00
|
|
|
locale_script: locale,
|
|
|
|
stylesheets: @stylesheets,
|
2020-11-27 11:54:25 -05:00
|
|
|
plugin_js: plugin_js,
|
2022-05-31 20:49:53 -04:00
|
|
|
plugin_test_js: plugin_test_js,
|
2020-06-03 14:45:23 -04:00
|
|
|
setup_data: client_side_setup_data,
|
2020-09-23 14:48:52 -04:00
|
|
|
preloaded: @preloaded,
|
2021-02-26 13:00:31 -05:00
|
|
|
html: create_html,
|
|
|
|
theme_html: create_theme_html,
|
2021-03-01 14:04:02 -05:00
|
|
|
html_classes: html_classes,
|
2021-03-09 10:09:35 -05:00
|
|
|
html_lang: html_lang,
|
2021-07-29 09:01:11 -04:00
|
|
|
login_path: main_app.login_path,
|
|
|
|
authentication_data: authentication_data,
|
2020-06-03 14:45:23 -04:00
|
|
|
}
|
2020-09-23 14:48:52 -04:00
|
|
|
bootstrap[:extra_locales] = extra_locales if extra_locales.present?
|
2021-02-05 17:18:29 -05:00
|
|
|
bootstrap[:csrf_token] = form_authenticity_token if current_user
|
2020-06-03 14:45:23 -04:00
|
|
|
|
|
|
|
render_json_dump(bootstrap: bootstrap)
|
|
|
|
end
|
2020-09-04 14:09:55 -04:00
|
|
|
|
2022-10-19 13:10:06 -04:00
|
|
|
def plugin_css_for_tests
|
|
|
|
urls =
|
|
|
|
Discourse
|
|
|
|
.find_plugin_css_assets(include_disabled: true, desktop_view: true)
|
|
|
|
.map do |target|
|
|
|
|
details = Stylesheet::Manager.new().stylesheet_details(target, "all")
|
|
|
|
details[0][:new_href]
|
|
|
|
end
|
|
|
|
|
|
|
|
stylesheet = <<~CSS
|
|
|
|
/* For use in tests only - `@import`s all plugin stylesheets */
|
|
|
|
#{urls.map { |url| "@import \"#{url}\";" }.join("\n")}
|
|
|
|
CSS
|
|
|
|
|
|
|
|
render plain: stylesheet, content_type: "text/css"
|
|
|
|
end
|
|
|
|
|
2020-09-04 14:09:55 -04:00
|
|
|
private
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2021-07-01 04:58:26 -04:00
|
|
|
def add_scheme(scheme_id, media, css_class)
|
2020-09-04 14:09:55 -04:00
|
|
|
return if scheme_id.to_i == -1
|
|
|
|
|
2021-06-15 02:57:17 -04:00
|
|
|
if style =
|
|
|
|
Stylesheet::Manager.new(theme_id: theme_id).color_scheme_stylesheet_details(
|
|
|
|
scheme_id,
|
|
|
|
media,
|
|
|
|
)
|
2021-07-01 04:58:26 -04:00
|
|
|
@stylesheets << { href: style[:new_href], media: media, class: css_class }
|
2020-09-04 14:09:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-27 11:54:25 -05:00
|
|
|
def add_style(target, opts = nil)
|
2021-06-15 02:57:17 -04:00
|
|
|
if styles = Stylesheet::Manager.new(theme_id: theme_id).stylesheet_details(target, "all")
|
2020-09-04 14:09:55 -04:00
|
|
|
styles.each do |style|
|
|
|
|
@stylesheets << {
|
|
|
|
href: style[:new_href],
|
|
|
|
media: "all",
|
|
|
|
theme_id: style[:theme_id],
|
|
|
|
target: style[:target],
|
2020-11-27 11:54:25 -05:00
|
|
|
}.merge(opts || {})
|
2020-09-04 14:09:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-26 13:00:31 -05:00
|
|
|
def create_html
|
|
|
|
html = {}
|
|
|
|
return html unless allow_plugins?
|
|
|
|
|
|
|
|
add_plugin_html(html, :before_body_close)
|
|
|
|
add_plugin_html(html, :before_head_close)
|
|
|
|
add_plugin_html(html, :before_script_load)
|
|
|
|
add_plugin_html(html, :header)
|
|
|
|
|
|
|
|
html
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_plugin_html(html, key)
|
|
|
|
add_if_present(
|
|
|
|
html,
|
|
|
|
key,
|
|
|
|
DiscoursePluginRegistry.build_html("server:#{key.to_s.dasherize}", self),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_theme_html
|
|
|
|
theme_html = {}
|
|
|
|
return theme_html if customization_disabled?
|
|
|
|
|
|
|
|
theme_view = mobile_view? ? :mobile : :desktop
|
|
|
|
|
2021-06-15 02:57:17 -04:00
|
|
|
add_if_present(theme_html, :body_tag, Theme.lookup_field(theme_id, theme_view, "body_tag"))
|
|
|
|
add_if_present(theme_html, :head_tag, Theme.lookup_field(theme_id, theme_view, "head_tag"))
|
|
|
|
add_if_present(theme_html, :header, Theme.lookup_field(theme_id, theme_view, "header"))
|
|
|
|
add_if_present(
|
|
|
|
theme_html,
|
|
|
|
:translations,
|
|
|
|
Theme.lookup_field(theme_id, :translations, I18n.locale),
|
|
|
|
)
|
|
|
|
add_if_present(theme_html, :js, Theme.lookup_field(theme_id, :extra_js, nil))
|
2021-02-26 13:00:31 -05:00
|
|
|
|
|
|
|
theme_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_if_present(hash, key, val)
|
|
|
|
hash[key] = val if val.present?
|
|
|
|
end
|
2020-06-03 14:45:23 -04:00
|
|
|
end
|