Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: system themes (#32681)

Krzysztof Kotlarek2536768a43f9
diff --git a/app/assets/javascripts/admin/addon/components/color-palette-editor.gjs b/app/assets/javascripts/admin/addon/components/color-palette-editor.gjs
index 311b3843907..699fd676561 100644
--- a/app/assets/javascripts/admin/addon/components/color-palette-editor.gjs
+++ b/app/assets/javascripts/admin/addon/components/color-palette-editor.gjs
@@ -178,6 +178,13 @@ const Picker = class extends Component {
}
}
+ get disabledEditForSystemDescription() {
+ if (!this.args.system) {
+ return null;
+ }
+ return i18n("admin.config_areas.color_palettes.blocked_edit_for_system");
+ }
+
ensureSixDigitsHex(hex) {
if (hex.length === 3) {
return hex
@@ -205,6 +212,8 @@ const Picker = class extends Component {
data-position={{@position}}
type="color"
value={{this.activeValue}}
+ disabled={{@system}}
+ title={{this.disabledEditForSystemDescription}}
{{on "input" this.onInput}}
{{on "change" this.onChange}}
/>
@@ -215,6 +224,8 @@ const Picker = class extends Component {
data-position={{@position}}
type="text"
maxlength="6"
+ disabled={{@system}}
+ title={{this.disabledEditForSystemDescription}}
value={{this.displayedColor}}
{{on "keypress" this.onTextKeypress}}
{{on "change" this.onTextChange}}
@@ -318,6 +329,7 @@ export default class ColorPaletteEditor extends Component {
@showDark={{this.darkModeActive}}
@onLightChange={{fn @onLightColorChange color}}
@onDarkChange={{fn @onDarkColorChange color}}
+ @system={{@system}}
/>
{{#unless @hideRevertButton}}
<DButton
diff --git a/app/assets/javascripts/admin/addon/components/themes-grid.gjs b/app/assets/javascripts/admin/addon/components/themes-grid.gjs
index e6eaaf8bb3a..49f60d31361 100644
--- a/app/assets/javascripts/admin/addon/components/themes-grid.gjs
+++ b/app/assets/javascripts/admin/addon/components/themes-grid.gjs
@@ -28,6 +28,12 @@ export default class ThemesGrid extends Component {
} else if (b.get("default")) {
return 1;
}
+ if (a.id < 0) {
+ return a.id;
+ }
+ if (b.id < 0) {
+ return -b.id;
+ }
});
}
diff --git a/app/assets/javascripts/admin/addon/templates/customize-themes-show-colors.gjs b/app/assets/javascripts/admin/addon/templates/customize-themes-show-colors.gjs
index ecd365cc718..58c257d54d0 100644
--- a/app/assets/javascripts/admin/addon/templates/customize-themes-show-colors.gjs
+++ b/app/assets/javascripts/admin/addon/templates/customize-themes-show-colors.gjs
@@ -60,6 +60,7 @@ export default RouteTemplate(
@onLightColorChange={{this.onLightColorChange}}
@onDarkColorChange={{this.onDarkColorChange}}
@hideRevertButton={{true}}
+ @system={{@model.system}}
/>
{{#if (gt @controller.colorPaletteChangeTracker.dirtyColorsCount 0)}}
<ChangesBanner
diff --git a/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs b/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
index eb8081c30f7..3bf0afc3899 100644
--- a/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
+++ b/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
@@ -184,30 +184,34 @@ export default RouteTemplate(
</section>
{{/if}}
- {{#unless @controller.model.remote_theme.is_git}}
- <div class="control-unit">
- <div class="mini-title">{{i18n "admin.customize.theme.css_html"}}</div>
- {{#if @controller.model.hasEditedFields}}
- <div class="description">{{i18n
- "admin.customize.theme.custom_sections"
+ {{#unless @controller.model.system}}
+ {{#unless @controller.model.remote_theme.is_git}}
+ <div class="control-unit">
+ <div class="mini-title">{{i18n
+ "admin.customize.theme.css_html"
}}</div>
- <ul>
- {{#each @controller.editedFieldsFormatted as |field|}}
- <li>{{field}}</li>
- {{/each}}
- </ul>
- {{else}}
- <div class="description">
- {{i18n "admin.customize.theme.edit_css_html_help"}}
- </div>
- {{/if}}
+ {{#if @controller.model.hasEditedFields}}
+ <div class="description">{{i18n
+ "admin.customize.theme.custom_sections"
+ }}</div>
+ <ul>
+ {{#each @controller.editedFieldsFormatted as |field|}}
+ <li>{{field}}</li>
+ {{/each}}
+ </ul>
+ {{else}}
+ <div class="description">
+ {{i18n "admin.customize.theme.edit_css_html_help"}}
+ </div>
+ {{/if}}
- <DButton
- @action={{@controller.editTheme}}
- @label="admin.customize.theme.edit_css_html"
- class="btn-default edit"
- />
- </div>
+ <DButton
+ @action={{@controller.editTheme}}
+ @label="admin.customize.theme.edit_css_html"
+ class="btn-default edit edit-code"
+ />
+ </div>
+ {{/unless}}
<div class="control-unit">
<div class="mini-title">{{i18n "admin.customize.theme.uploads"}}</div>
@@ -242,7 +246,7 @@ export default RouteTemplate(
@action={{@controller.addUploadModal}}
@icon="plus"
@label="admin.customize.theme.add"
- class="btn-default"
+ class="btn-default upload"
/>
</div>
{{/unless}}
@@ -379,12 +383,14 @@ export default RouteTemplate(
class="btn-default btn-normal"
/>
{{/if}}
- <DButton
- @action={{@controller.destroyTheme}}
- @label="admin.customize.delete"
- @icon="trash-can"
- class="btn-danger"
- />
+ {{#unless @controller.model.system}}
+ <DButton
+ @action={{@controller.destroyTheme}}
+ @label="admin.customize.delete"
+ @icon="trash-can"
+ class="btn-danger delete"
+ />
+ {{/unless}}
</div>
</template>
);
diff --git a/app/assets/javascripts/admin/addon/templates/customize-themes-show.gjs b/app/assets/javascripts/admin/addon/templates/customize-themes-show.gjs
index 97b3988025c..c7c6be3fbf9 100644
--- a/app/assets/javascripts/admin/addon/templates/customize-themes-show.gjs
+++ b/app/assets/javascripts/admin/addon/templates/customize-themes-show.gjs
@@ -58,11 +58,13 @@ export default RouteTemplate(
/>
{{else}}
<span>{{@controller.model.name}}</span>
- <DButton
- @action={{@controller.startEditingName}}
- @icon="pencil"
- class="btn-small"
- />
+ {{#unless @controller.model.system}}
+ <DButton
+ @action={{@controller.startEditingName}}
+ @icon="pencil"
+ class="btn-small"
+ />
+ {{/unless}}
{{/if}}
</div>
diff --git a/app/assets/javascripts/discourse/app/app.js b/app/assets/javascripts/discourse/app/app.js
index fa7b901c0ea..2d18e7d21b5 100644
--- a/app/assets/javascripts/discourse/app/app.js
+++ b/app/assets/javascripts/discourse/app/app.js
@@ -62,7 +62,7 @@ class Discourse extends Application {
}
function moduleThemeId(moduleName) {
- const match = moduleName.match(/^discourse\/theme\-(\d+)\//);
+ const match = moduleName.match(/^discourse\/theme\-(-?\d+)\//);
if (match) {
return parseInt(match[1], 10);
}
diff --git a/app/assets/stylesheets/admin/color-palette-editor.scss b/app/assets/stylesheets/admin/color-palette-editor.scss
index 242a52a6234..d8b1501abf8 100644
--- a/app/assets/stylesheets/admin/color-palette-editor.scss
+++ b/app/assets/stylesheets/admin/color-palette-editor.scss
@@ -88,6 +88,10 @@
&:focus {
outline: none;
}
+
+ &:disabled {
+ background-color: var(--d-input-bg-color);
+ }
}
&__color-code {
diff --git a/app/controllers/admin/config/customize_controller.rb b/app/controllers/admin/config/customize_controller.rb
index 98ab25a8e43..1c235112894 100644
--- a/app/controllers/admin/config/customize_controller.rb
+++ b/app/controllers/admin/config/customize_controller.rb
@@ -11,6 +11,8 @@ class Admin::Config::CustomizeController < Admin::AdminController
.where(component: false)
.order(:name)
+ themes = themes.not_system if !SiteSetting.experimental_system_themes
+
render json: { themes: serialize_data(themes, ThemeIndexSerializer) }
end
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index 6d3c7cf13c7..9c43b79a613 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -222,6 +222,10 @@ class Admin::ThemesController < Admin::AdminController
disables_component = [false, "false"].include?(theme_params[:enabled])
enables_component = [true, "true"].include?(theme_params[:enabled])
+ if @theme.system? && (theme_params.keys - Theme::EDITABLE_SYSTEM_ATTRIBUTES).present?
+ raise Discourse::InvalidAccess.new
+ end
+
%i[name color_scheme_id user_selectable enabled auto_update].each do |field|
@theme.public_send("#{field}=", theme_params[field]) if theme_params.key?(field)
end
@@ -275,6 +279,7 @@ class Admin::ThemesController < Admin::AdminController
end
def destroy
+ raise Discourse::InvalidAccess if params[:id].to_i.negative?
@theme = Theme.find_by(id: params[:id])
raise Discourse::InvalidParameters.new(:id) unless @theme
@@ -285,6 +290,7 @@ class Admin::ThemesController < Admin::AdminController
end
def bulk_destroy
+ params[:theme_ids] = params[:theme_ids].filter { |id| id.to_i.positive? }
themes = Theme.where(id: params[:theme_ids])
raise Discourse::InvalidParameters.new(:id) if themes.blank?
@@ -375,6 +381,7 @@ class Admin::ThemesController < Admin::AdminController
end
def change_colors
+ raise Discourse::InvalidAccess if params[:id].to_i.negative?
theme = Theme.find_by(id: params[:id], component: false)
raise Discourse::NotFound if !theme
diff --git a/app/models/concerns/reports/top_uploads.rb b/app/models/concerns/reports/top_uploads.rb
index 1aff15b4873..09e80bfbbee 100644
--- a/app/models/concerns/reports/top_uploads.rb
+++ b/app/models/concerns/reports/top_uploads.rb
@@ -64,6 +64,7 @@ module Reports::TopUploads
)
builder.where("up.created_at >= :start_date", start_date: report.start_date)
builder.where("up.created_at < :end_date", end_date: report.end_date)
+ builder.where("username != '#{Discourse.system_user.username}'")
if extension_filter
builder.where("up.extension = :extension", extension: extension_filter.sub(/\A\./, ""))
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 8fd6887c3ef..5a69a683b0e 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -82,11 +82,12 @@ class RemoteTheme < ActiveRecord::Base
)
end
- # This is only used in the development and test environment and is currently not supported for other environments
- if Rails.env.test? || Rails.env.development?
- def self.import_theme_from_directory(directory)
- update_theme(ThemeStore::DirectoryImporter.new(directory), update_components: "none")
- end
+ def self.import_theme_from_directory(directory, theme_id: nil)
+ update_theme(
+ ThemeStore::DirectoryImporter.new(directory),
+ update_components: "none",
+ theme_id: theme_id,
+ )
end
def self.update_theme(
@@ -103,7 +104,13 @@ class RemoteTheme < ActiveRecord::Base
existing = true
if theme.blank?
- theme = Theme.new(user_id: user&.id || -1, name: theme_info["name"], auto_update: false)
+ theme =
+ Theme.new(
+ id: theme_id,
+ user_id: user&.id || -1,
+ name: theme_info["name"],
+ auto_update: false,
+ )
existing = false
end
diff --git a/app/models/theme.rb b/app/models/theme.rb
index 6681a1f78cd..cfb9513393a 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -7,6 +7,16 @@ class Theme < ActiveRecord::Base
include GlobalPath
BASE_COMPILER_VERSION = 91
+ CORE_THEMES = { "foundation" => -1 }
+ EDITABLE_SYSTEM_ATTRIBUTES = %w[
+ child_theme_ids
+ color_scheme_id
+ default
+ locale
+ translations
+ user_selectable
+ updated_at
+ ]
class SettingsMigrationError < StandardError
end
@@ -82,6 +92,8 @@ class Theme < ActiveRecord::Base
validate :validate_theme_fields
after_create :update_child_components
+ before_update :check_editable_attributes, if: :system?
+ before_destroy :raise_invalid_parameters, if: :system?
scope :user_selectable, -> { where("user_selectable OR id = ?", SiteSetting.default_theme_id) }
@@ -108,6 +120,9 @@ class Theme < ActiveRecord::Base
)
end
+ scope :not_system, -> { where("id > 0") }
+ scope :system, -> { where("id < 0") }
+
delegate :remote_url, to: :remote_theme, private: true, allow_nil: true
def notify_color_change(color, scheme: nil)
@@ -218,6 +233,10 @@ class Theme < ActiveRecord::Base
Theme.expire_site_cache!
end
+ def self.foundation_theme
+ Theme.find(CORE_THEMES["foundation"])
+ end
+
def self.compiler_version
get_set_cache "compiler_version" do
dependencies = [
@@ -337,6 +356,10 @@ class Theme < ActiveRecord::Base
SiteSetting.default_theme_id == id
end
+ def system?
+ id < 0
+ end
+
def supported?
if minimum_version = remote_theme&.minimum_discourse_version
return false unless Discourse.has_needed_version?(Discourse::VERSION::STRING, minimum_version)
@@ -1087,6 +1110,15 @@ class Theme < ActiveRecord::Base
.first
end
end
+
+ def check_editable_attributes
+ return if (changes.keys - EDITABLE_SYSTEM_ATTRIBUTES).empty?
+ raise_invalid_parameters
+ end
+
+ def raise_invalid_parameters
+ raise Discourse::InvalidParameters
+ end
end
# == Schema Information
diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index ece978d7da4..68cf6c90083 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb
@@ -13,7 +13,8 @@ class ThemeSerializer < BasicThemeSerializer
:enabled?,
:disabled_at,
:theme_fields,
- :screenshot_url
+ :screenshot_url,
+ :system
has_one :color_scheme, serializer: ColorSchemeSerializer, embed: :object
has_one :owned_color_palette, serializer: ColorSchemeSerializer, embed: :object
@@ -93,4 +94,8 @@ class ThemeSerializer < BasicThemeSerializer
def include_disabled_by?
include_disabled_at?
end
+
+ def system
+ object.system?
+ end
end
diff --git a/config/initializers/999-themes.rb b/config/initializers/999-themes.rb
new file mode 100644
index 00000000000..e875b71b9a9
--- /dev/null
+++ b/config/initializers/999-themes.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+Rails.application.config.after_initialize do |config|
+ if Rails.env.development?
+ require "listen"
+
+ class Watcher
+ def watch
+ listener =
+ Listen.to("#{Rails.root}/themes") do |modified, added, removed|
+ filepath = modified.first || added.first || removed.first
+ theme_name = filepath.gsub("#{Rails.root}/themes/", "").split("/").first
+ theme = Theme.find(Theme::CORE_THEMES[theme_name])
+
+ Rails.logger.info "Theme folder changed. Syncing #{theme_name}..."
+ if modified.length == 1 && added.length == 0 && removed.length == 0 &&
+ (
+ (theme_field = find_js_field(theme, filepath)) ||
+ (theme_field = find_scss_field(theme, filepath))
+ )
+ theme_field.update!(value: File.read(modified[0]))
+ theme.update_javascript_cache!
+ Stylesheet::Manager.clear_theme_cache!
+ Theme.expire_site_cache!
+ else
+ SystemThemesManager.sync_theme!(theme_name)
+ end
+ MessageBus.publish "/file-change", ["development-mode-theme-changed"]
+ end
+ listener.start
+ end
+
+ private
+
+ def find_js_field(theme, filepath)
+ theme
+ .theme_fields
+ .where(
+ name: filepath.gsub("#{Rails.root}/themes/#{theme.name.downcase}/javascripts/", ""),
+ type_id: ThemeField.types[:js],
+ )
+ .first
+ end
+
+ def find_scss_field(theme, filepath)
+ theme
+ .theme_fields
+ .where(name: filepath.split("/").last.split(".").first, type_id: ThemeField.types[:scss])
+ .first
+ end
+ end
+
+ Watcher.new.watch
+ end
+end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 2902f949bbd..81a707c779c 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6425,6 +6425,7 @@ en:
copy_created: 'A new copy of "%{name}" has been created'
illegal_character_in_color: "Color must consist of hexadecimal characters only"
invalid_color_length: "Color must in the format #rrggbb or #rgb"
+ blocked_edit_for_system: "Color palette belonging to a system theme cannot be edited."
delete_confirm: "Are you sure you want to delete this color palette?"
plugins:
title: "Plugins"
diff --git a/config/routes.rb b/config/routes.rb
index 0a563b5d82c..8e852bc9b02 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1012,7 +1012,7 @@ Discourse::Application.routes.draw do
:constraints => {
hostname: /[\w\.-]+/,
version: /\h{40}/,
- theme_id: /([0-9]+)?/,
+ theme_id: /(-?[0-9]+)?/,
format: :js,
}
get "svg-sprite/search/:keyword" => "svg_sprite#search",
diff --git a/config/site_settings.yml b/config/site_settings.yml
index f9bd3b4104a..0f82e565679 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -4007,3 +4007,6 @@ experimental:
default: false
client: true
validator: "LanguageSwitcherSettingValidator"
+ experimental_system_themes:
+ default: false
+ hidden: true
diff --git a/db/fixtures/600_themes.rb b/db/fixtures/600_themes.rb
index e2e2faefc32..aad0ec1e7f6 100644
--- a/db/fixtures/600_themes.rb
+++ b/db/fixtures/600_themes.rb
@@ -35,3 +35,5 @@ if !Theme.exists?
SiteSetting.default_dark_mode_color_scheme_id = dark_scheme_id if dark_scheme_id.present?
end
end
+
+SystemThemesManager.sync!
diff --git a/lib/system_themes_manager.rb b/lib/system_themes_manager.rb
new file mode 100644
index 00000000000..9e3603c5cc9
--- /dev/null
+++ b/lib/system_themes_manager.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class SystemThemesManager
+ def self.sync!
+ Theme::CORE_THEMES.keys.each { |theme_name| sync_theme!(theme_name) }
+ end
+
+ def self.sync_theme!(theme_name)
+ theme_id = Theme::CORE_THEMES[theme_name]
+ raise Discourse::InvalidParameters unless theme_id
+
+ theme_dir = "#{Rails.root}/themes/#{theme_name}"
+
+ RemoteTheme.import_theme_from_directory(theme_dir, theme_id: theme_id)
+ Stylesheet::Manager.clear_theme_cache!
+ end
+end
diff --git a/lib/theme_resolver.rb b/lib/theme_resolver.rb
index 8bae0f7018e..21c20091bb3 100644
--- a/lib/theme_resolver.rb
+++ b/lib/theme_resolver.rb
@@ -26,8 +26,7 @@ module ThemeResolver
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])
+ if theme_id.blank? && guardian.allow_themes?([SiteSetting.default_theme_id])
theme_id = SiteSetting.default_theme_id
end
diff --git a/spec/lib/s3_inventory_spec.rb b/spec/lib/s3_inventory_spec.rb
index db2fd449791..25cf6bd9b0b 100644
--- a/spec/lib/s3_inventory_spec.rb
+++ b/spec/lib/s3_inventory_spec.rb
@@ -7,6 +7,13 @@ RSpec.describe S3Inventory do
let(:csv_filename) { "#{Rails.root}/spec/fixtures/csv/s3_inventory.csv" }
+ let(:system_themes_screenshots) do
+ UploadReference
+ .where(target_type: "ThemeField")
+ .map { |screenshot| screenshot.upload.url }
+ .join("\n")
+ end
+
before do
inventory.s3_helper.stub_client_responses!
inventory.stubs(:cleanup!)
@@ -71,8 +78,10 @@ RSpec.describe S3Inventory do
it "should display missing uploads correctly" do
output = capture_stdout { inventory.backfill_etags_and_list_missing }
- expect(output).to eq("#{@upload_1.url}\n#{@no_etag.url}\n2 of 6 uploads are missing\n")
- expect(Discourse.stats.get("missing_s3_uploads")).to eq(2)
+ expect(output).to eq(
+ "#{system_themes_screenshots}\n#{@upload_1.url}\n#{@no_etag.url}\n4 of 8 uploads are missing\n",
+ )
+ expect(Discourse.stats.get("missing_s3_uploads")).to eq(4)
end
it "should detect when a url match exists with a different etag" do
@@ -85,22 +94,23 @@ RSpec.describe S3Inventory do
output = capture_stdout { inventory.backfill_etags_and_list_missing }
expect(output).to eq(<<~TEXT)
+ #{system_themes_screenshots}
#{upload_with_differing_tag_1.url} has different etag
#{upload_with_differing_tag_2.url} has different etag
#{@upload_1.url}
#{@no_etag.url}
- 4 of 6 uploads are missing
+ 6 of 8 uploads are missing
2 of these are caused by differing etags
Null the etag column and re-run for automatic backfill
TEXT
- expect(Discourse.stats.get("missing_s3_uploads")).to eq(4)
+ expect(Discourse.stats.get("missing_s3_uploads")).to eq(6)
end
it "marks missing uploads as not verified and found uploads as verified. uploads not checked will be verified nil" do
expect(
Upload.where(verification_status: Upload.verification_statuses[:unchecked]).count,
- ).to eq(13)
+ ).to eq(15)
output = capture_stdout { inventory.backfill_etags_and_list_missing }
@@ -110,7 +120,7 @@ RSpec.describe S3Inventory do
Upload.where(verification_status: Upload.verification_statuses[:verified]).count,
).to eq(4)
- expect(Upload.with_invalid_etag_verification_status.count).to eq(2)
+ expect(Upload.with_invalid_etag_verification_status.count).to eq(4)
expect(
Upload.where(verification_status: Upload.verification_statuses[:unchecked]).count,
@@ -157,7 +167,7 @@ RSpec.describe S3Inventory do
}.by(-3)
end
- expect(Upload.by_users.order(:url).pluck(:url, :etag)).to eq(files)
+ expect(Upload.where.not(etag: nil).by_users.order(:url).pluck(:url, :etag)).to eq(files)
end
context "when site was restored from a backup" do
@@ -237,7 +247,9 @@ RSpec.describe S3Inventory do
end
end
- expect(output).to eq("#{upload.url}\n#{no_etag.url}\n2 of 6 uploads are missing\n")
- expect(Discourse.stats.get("missing_s3_uploads")).to eq(2)
+ expect(output).to eq(
+ "#{system_themes_screenshots}\n#{upload.url}\n#{no_etag.url}\n4 of 8 uploads are missing\n",
+ )
+ expect(Discourse.stats.get("missing_s3_uploads")).to eq(4)
end
end
diff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rb
index 744c652b5e9..d655ab8d1bc 100644
--- a/spec/lib/stylesheet/manager_spec.rb
+++ b/spec/lib/stylesheet/manager_spec.rb
@@ -999,7 +999,7 @@ RSpec.describe Stylesheet::Manager do
Stylesheet::Manager.precompile_theme_css
results = StylesheetCache.pluck(:target)
- expect(results.size).to eq(44) # 10 core targets + theme rtl/ltr + 32 color schemes (light and dark mode per scheme)
+ expect(results.size).to eq(58) # 10 core targets + theme rtl/ltr + 32 color schemes (light and dark mode per scheme) + 14 Foundation
expect(results).to include("color_definitions_#{scheme1.name}_#{scheme1.id}_#{user_theme.id}")
expect(results).to include(
diff --git a/spec/lib/system_themes_manager_spec.rb b/spec/lib/system_themes_manager_spec.rb
new file mode 100644
index 00000000000..e4d461ee595
--- /dev/null
+++ b/spec/lib/system_themes_manager_spec.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+RSpec.describe SystemThemesManager do
+ it "is idempotent" do
+ Theme.delete_all
+ expect { SystemThemesManager.sync! }.to change { Theme.system.count }.by(1)
+ expect { SystemThemesManager.sync! }.not_to change { Theme.count }
+ end
+end
diff --git a/spec/lib/theme_settings_parser_spec.rb b/spec/lib/theme_settings_parser_spec.rb
index 1350df3a4a1..9351e45e0fe 100644
--- a/spec/lib/theme_settings_parser_spec.rb
+++ b/spec/lib/theme_settings_parser_spec.rb
@@ -17,7 +17,7 @@ RSpec.describe ThemeSettingsParser do
def load_settings
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/valid_settings.yaml")
- field = ThemeField.create!(theme_id: 1, target_id: 3, name: "yaml", value: yaml)
+ field = ThemeField.create!(theme_id: -1, target_id: 3, name: "yaml", value: yaml)
ThemeSettingsParser
.new(field)
diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb
index 233a669e319..3accbb06d1d 100644
--- a/spec/models/draft_spec.rb
+++ b/spec/models/draft_spec.rb
@@ -147,13 +147,13 @@ RSpec.describe Draft do
draft = Draft.first
draft.upload_references << UploadReference.create!(target: draft, upload: Fabricate(:upload))
- expect(UploadReference.count).to eq(1)
+ expect(UploadReference.where(target_type: "Draft").count).to eq(1)
Draft.cleanup!
expect(Draft.count).to eq 0
expect(user.reload.user_stat.draft_count).to eq(0)
- expect(UploadReference.count).to eq(0)
+ expect(UploadReference.where(target_type: "Draft").count).to eq(0)
Draft.set(Fabricate(:user), topic_key_1, 0, "draft")
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index af1dd6a3466..c6c7349575b 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1754,10 +1754,10 @@ RSpec.describe Post do
post.link_post_uploads
post.trash!
- expect(UploadReference.count).to eq(7)
+ expect(UploadReference.where(target_type: "Post").count).to eq(7)
post.destroy!
- expect(UploadReference.count).to eq(0)
+ expect(UploadReference.where(target_type: "Post").count).to eq(0)
end
describe "#link_post_uploads" do
diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rb
index 8e6a5c13146..4e19a322c49 100644
--- a/spec/models/theme_field_spec.rb
+++ b/spec/models/theme_field_spec.rb
@@ -30,7 +30,7 @@ RSpec.describe ThemeField do
it "does not insert a script tag when there are no inline script" do
theme_field =
- ThemeField.create!(theme_id: 1, target_id: 0, name: "body_tag", value: "<div>new div</div>")
+ ThemeField.create!(theme_id: -1, target_id: 0, name: "body_tag", value: "<div>new div</div>")
theme_field.ensure_baked!
expect(theme_field.value_baked).to_not include("<script")
end
@@ -79,10 +79,10 @@ RSpec.describe ThemeField do
<script src="/external-script.js"></script>
HTML
- theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "header", value: html)
+ theme_field = ThemeField.create!(theme_id: -1, target_id: 0, name: "header", value: html)
theme_field.ensure_baked!
expect(theme_field.value_baked).to include(
- "<script defer=\"\" src=\"#{theme_field.javascript_cache.url}\" data-theme-id=\"1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
+ "<script defer=\"\" src=\"#{theme_field.javascript_cache.url}\" data-theme-id=\"-1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
)
expect(theme_field.value_baked).to include("external-script.js")
expect(theme_field.value_baked).to include('<script type="text/template"')
@@ -103,7 +103,7 @@ RSpec.describe ThemeField do
var b = 10
JS
- theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "header", value: html)
+ theme_field = ThemeField.create!(theme_id: -1, target_id: 0, name: "header", value: html)
theme_field.ensure_baked!
expect(theme_field.javascript_cache.content).to include(extracted)
end
@@ -115,13 +115,13 @@ RSpec.describe ThemeField do
</script>
HTML
- field = ThemeField.create!(theme_id: 1, target_id: 0, name: "header", value: html)
+ field = ThemeField.create!(theme_id: -1, target_id: 0, name: "header", value: html)
field.ensure_baked!
expect(field.error).not_to eq(nil)
expect(field.value_baked).to include(
- "<script defer=\"\" src=\"#{field.javascript_cache.url}\" data-theme-id=\"1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
+ "<script defer=\"\" src=\"#{field.javascript_cache.url}\" data-theme-id=\"-1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
)
- expect(field.javascript_cache.content).to include("[THEME 1 'Default'] Compile error")
+ expect(field.javascript_cache.content).to include("[THEME -1 'Foundation'] Compile error")
field.update!(value: "")
field.ensure_baked!
@@ -136,17 +136,17 @@ HTML
HTML
ThemeField.create!(
- theme_id: 1,
+ theme_id: -1,
target_id: 3,
name: "yaml",
value: "string_setting: \"test text \\\" 123!\"",
).ensure_baked!
- theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "head_tag", value: html)
+ theme_field = ThemeField.create!(theme_id: -1, target_id: 0, name: "head_tag", value: html)
theme_field.ensure_baked!
javascript_cache = theme_field.javascript_cache
expect(theme_field.value_baked).to include(
- "<script defer=\"\" src=\"#{javascript_cache.url}\" data-theme-id=\"1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
+ "<script defer=\"\" src=\"#{javascript_cache.url}\" data-theme-id=\"-1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\"></script>",
)
expect(javascript_cache.content).to include("testing-div")
expect(javascript_cache.content).to include("string_setting")
@@ -294,7 +294,7 @@ HTML
def create_upload_theme_field!(name)
ThemeField
.create!(
- theme_id: 1,
+ theme_id: -1,
target_id: 0,
value: "",
type_id: ThemeField.types[:theme_upload_var],
@@ -315,7 +315,7 @@ HTML
def create_yaml_field(value)
field =
ThemeField.create!(
- theme_id: 1,
+ theme_id: -1,
target_id: Theme.targets[:settings],
name: "yaml",
value: value,
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index 3960572a5b4..a0e6c81fc93 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -6,6 +6,7 @@ RSpec.describe Theme do
let(:guardian) { Guardian.new(user) }
let(:child) { Fabricate(:theme, user: user, component: true) }
+ let(:foundation_theme) { Theme.foundation_theme }
before { ThemeJavascriptCompiler.disable_terser! }
@@ -439,7 +440,7 @@ HTML
end
it "correctly caches theme ids" do
- Theme.where.not(id: theme.id).destroy_all
+ Theme.where.not(id: theme.id).delete_all
theme2 = Fabricate(:theme)
@@ -467,7 +468,7 @@ HTML
end
it "correctly caches enabled_theme_and_component_ids" do
- Theme.destroy_all
+ Theme.delete_all
theme2 = Fabricate(:theme)
@@ -508,7 +509,7 @@ HTML
end
it "correctly caches user_themes template" do
- Theme.destroy_all
+ Theme.delete_all
json = Site.json_for(guardian)
user_themes = JSON.parse(json)["user_themes"]
@@ -544,7 +545,7 @@ HTML
end
it "clears color scheme cache correctly" do
- Theme.destroy_all
+ Theme.delete_all
cs =
Fabricate(
@@ -604,7 +605,7 @@ HTML
end
it "includes theme_uploads in settings" do
- Theme.where.not(id: theme.id).destroy_all
+ Theme.where.not(id: theme.id).delete_all
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme.set_field(type: :theme_upload_var, target: :common, name: "bob", upload_id: upload.id)
@@ -616,7 +617,7 @@ HTML
end
it "does not break on missing uploads in settings" do
- Theme.where.not(id: theme.id).destroy_all
+ Theme.where.not(id: theme.id).delete_all
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme.set_field(type: :theme_upload_var, target: :common, name: "bob", upload_id: upload.id)
@@ -631,7 +632,7 @@ HTML
it "uses CDN url for theme_uploads in settings" do
set_cdn_url("http://cdn.localhost")
- Theme.where.not(id: theme.id).destroy_all
+ Theme.where.not(id: theme.id).delete_all
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme.set_field(type: :theme_upload_var, target: :common, name: "bob", upload_id: upload.id)
@@ -644,7 +645,7 @@ HTML
it "uses CDN url for settings of type upload" do
set_cdn_url("http://cdn.localhost")
- Theme.where.not(id: theme.id).destroy_all
+ Theme.where.not(id: theme.id).delete_all
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme.set_field(target: :settings, name: "yaml", value: <<~YAML)
@@ -1763,4 +1764,25 @@ HTML
expect(theme.owned_color_palette).to eq(palette)
end
end
+
+ it "checks if fields can be updated for system themes" do
+ foundation_theme.update!(user_selectable: true)
+ expect(foundation_theme.user_selectable).to be true
+ expect { foundation_theme.update!(name: "edited system name") }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ expect { theme.update!(name: "edited name") }.not_to raise_error
+ end
+
+ it "does not allow system themes to be deleted" do
+ expect { foundation_theme.destroy! }.to raise_error(Discourse::InvalidParameters)
+ expect { theme.destroy! }.not_to raise_error
+ end
+
+ describe "#system?" do
+ it "returns system true for Horizon and Foundation themes" do
+ expect(foundation_theme.system?).to be true
+ expect(theme.system?).to be false
+ end
+ end
end
diff --git a/spec/multisite/s3_store_spec.rb b/spec/multisite/s3_store_spec.rb
index d5268491360..2342ee622a8 100644
--- a/spec/multisite/s3_store_spec.rb
+++ b/spec/multisite/s3_store_spec.rb
@@ -12,8 +12,8 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
Fabricate.build(
:upload,
sha1: upload_sha1,
- id: 1,
original_filename: original_filename,
+ id: Upload.maximum(:id) + 1,
secure: secure,
)
end
diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rb
index c64b9df73c1..7e962a2da97 100644
--- a/spec/requests/admin/themes_controller_spec.rb
+++ b/spec/requests/admin/themes_controller_spec.rb
@@ -433,7 +433,7 @@ RSpec.describe Admin::ThemesController do
it "correctly returns themes" do
ColorScheme.destroy_all
- Theme.destroy_all
+ Theme.not_system.destroy_all
theme = Fabricate(:theme)
theme.set_field(target: :common, name: :scss, value: ".body{color: black;}")
@@ -623,6 +623,16 @@ RSpec.describe Admin::ThemesController do
expect(SiteSetting.default_theme_id).to eq(theme.id)
end
+ it "can set system theme as default" do
+ theme.update_columns(id: -10)
+ SiteSetting.default_theme_id = -1
+
+ put "/admin/themes/#{theme.id}.json", params: { id: theme.id, theme: { default: true } }
+
+ expect(response.status).to eq(200)
+ expect(SiteSetting.default_theme_id).to eq(theme.id)
+ end
+
it "can unset default theme" do
SiteSetting.default_theme_id = theme.id
@@ -689,6 +699,38 @@ RSpec.describe Admin::ThemesController do
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
end
+ it "only allows to update certain fields for system themes" do
+ theme.update_columns(id: -10)
+ child_theme = Fabricate(:theme, component: true)
+ put "/admin/themes/#{theme.id}.json",
+ params: {
+ theme: {
+ child_theme_ids: [child_theme.id],
+ color_scheme_id: 1,
+ user_selectable: true,
+ },
+ }
+ expect(response.status).to eq(200)
+ expect(theme.reload.user_selectable).to be true
+ expect(theme.child_theme_ids).to eq([child_theme.id])
+ expect(theme.color_scheme_id).to eq(1)
+
+ put "/admin/themes/#{theme.id}.json",
+ params: {
+ theme: {
+ child_theme_ids: [child_theme.id],
+ name: "my test name",
+ user_selectable: false,
+ theme_fields: [
+ { name: "scss", target: "common", value: "" },
+ { name: "scss", target: "desktop", value: "body{color: blue;}" },
+ ],
+ },
+ }
+ expect(response.status).to eq(403)
+ expect(theme.reload.user_selectable).to be true
+ end
+
it "prevents theme update when using ember css selectors" do
child_theme = Fabricate(:theme, component: true)
@@ -1379,6 +1421,14 @@ RSpec.describe Admin::ThemesController do
end.to change { Theme.count }.by(-2)
end
+ it "does not destroy if any theme is system" do
+ theme.update_columns(id: -10)
+ expect do
+ delete "/admin/themes/bulk_destroy.json", params: { theme_ids: theme_ids }
+ end.to change { Theme.count }.by(-1)
+ expect { theme_2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
+ end
+
it "logs the theme destroy action for each theme" do
StaffActionLogger.any_instance.expects(:log_theme_destroy).twice
delete "/admin/themes/bulk_destroy.json", params: { theme_ids: theme_ids }
@@ -1582,5 +1632,18 @@ RSpec.describe Admin::ThemesController do
expect(response.status).to eq(404)
end
end
+
+ context "when system theme" do
+ before { theme.update_columns(id: -10) }
+
+ it "returns invalid access" do
+ put "/admin/themes/#{theme.id}/change-colors.json",
+ params: {
+ colors: [{ name: "primary", hex: "ff0000", dark_hex: "0000ff" }],
+ }
+
+ expect(response.status).to eq(403)
+ end
+ end
end
end
diff --git a/spec/system/admin_customize_themes_config_area_spec.rb b/spec/system/admin_customize_themes_config_area_spec.rb
index dc3699af369..7710e140ddf 100644
--- a/spec/system/admin_customize_themes_config_area_spec.rb
+++ b/spec/system/admin_customize_themes_config_area_spec.rb
@@ -2,7 +2,9 @@
describe "Admin Customize Themes Config Area Page", type: :system do
fab!(:admin)
- fab!(:theme) { Theme.where(component: false).first }
+ fab!(:theme)
+ fab!(:default_theme) { Theme.where(component: false, name: "Default").first }
+ fab!(:foundation_theme) { Theme.where(component: false, name: "Foundation").first }
fab!(:theme_child_theme) do
Fabricate(:theme, name: "Child theme", component: true, enabled: true, parent_themes: [theme])
end
@@ -12,7 +14,10 @@ describe "Admin Customize Themes Config Area Page", type: :system do
let(:install_modal) { PageObjects::Modals::InstallTheme.new }
let(:admin_customize_themes_page) { PageObjects::Pages::AdminCustomizeThemes.new }
- before { sign_in(admin) }
+ before do
+ SiteSetting.experimental_system_themes = true
+ sign_in(admin)
+ end
it "has an install button in the subheader" do
config_area.visit
@@ -37,11 +42,11 @@ describe "Admin Customize Themes Config Area Page", type: :system do
it "allows to mark theme as active" do
config_area.visit
- expect(config_area).to have_badge(theme, "--active")
+ expect(config_area).to have_badge(default_theme, "--active")
expect(config_area).to have_no_badge(theme_2, "--active")
config_area.mark_as_active(theme_2)
expect(config_area).to have_badge(theme_2, "--active")
- expect(config_area).to have_no_badge(theme, "--active")
+ expect(config_area).to have_no_badge(foundation_theme, "--active")
end
it "allows to make theme selectable by users" do
diff --git a/spec/system/admin_customize_themes_spec.rb b/spec/system/admin_customize_themes_spec.rb
index 9e2551dab73..d6ff4c5112b 100644
--- a/spec/system/admin_customize_themes_spec.rb
+++ b/spec/system/admin_customize_themes_spec.rb
@@ -63,6 +63,15 @@ describe "Admin Customize Themes", type: :system do
end
end
+ it "cannot edit js, upload files or delete system themes" do
+ theme.update_columns(id: -10)
+ visit("/admin/customize/themes/#{theme.id}")
+ expect(page).not_to have_css(".title button")
+ expect(page).not_to have_css(".edit-code")
+ expect(page).not_to have_css("button.upload")
+ expect(page).not_to have_css(".delete")
+ end
+
describe "when editing theme translations" do
it "should allow admin to edit and save the theme translations" do
theme.set_field(
diff --git a/themes/foundation/.npmrc b/themes/foundation/.npmrc
new file mode 100644
index 00000000000..89ddd358b37
--- /dev/null
+++ b/themes/foundation/.npmrc
@@ -0,0 +1,2 @@
+engine-strict = true
+auto-install-peers = false
diff --git a/themes/foundation/about.json b/themes/foundation/about.json
new file mode 100644
index 00000000000..2943ca2731f
--- /dev/null
+++ b/themes/foundation/about.json
@@ -0,0 +1,11 @@
+{
+ "name": "Foundation",
+ "authors": "Discourse",
+ "theme_version": "1.0.0",
+ "minimum_discourse_version": null,
+ "maximum_discourse_version": null,
+ "assets": {},
+ "modifiers": {},
+ "color_schemes": {},
+ "screenshots": ["screenshots/light.png", "screenshots/dark.png"]
+}
diff --git a/themes/foundation/locales/en.yml b/themes/foundation/locales/en.yml
new file mode 100644
index 00000000000..6c9b0ba34d5
--- /dev/null
+++ b/themes/foundation/locales/en.yml
@@ -0,0 +1,3 @@
+en:
+ theme_metadata:
+ description: "The classic Discourse theme that is well-suited to customization for your community’s specific needs."
diff --git a/themes/foundation/screenshots/dark.png b/themes/foundation/screenshots/dark.png
new file mode 100644
index 00000000000..6d1531bc8b9
Binary files /dev/null and b/themes/foundation/screenshots/dark.png differ
diff --git a/themes/foundation/screenshots/light.png b/themes/foundation/screenshots/light.png
new file mode 100644
index 00000000000..1e567e90020
Binary files /dev/null and b/themes/foundation/screenshots/light.png differ