From 30d9abdfc9b7e99ba516e9baeba699ff319f9f10 Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 16 Jan 2022 20:21:43 +0100 Subject: [PATCH 1/7] Refactor glitch-soc's theme handling --- app/controllers/application_controller.rb | 128 +++++++++++----------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7c36bc6b86..881c23f940 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -73,70 +73,6 @@ class ApplicationController < ActionController::Base new_user_session_path end - def pack(data, pack_name, skin = 'default') - return nil unless pack?(data, pack_name) - pack_data = { - common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), - flavour: data['name'], - pack: pack_name, - preload: nil, - skin: nil, - supported_locales: data['locales'], - } - if data['pack'][pack_name].is_a?(Hash) - pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false - pack_data[:pack] = nil unless data['pack'][pack_name]['filename'] - if data['pack'][pack_name]['preload'] - pack_data[:preload] = [data['pack'][pack_name]['preload']] if data['pack'][pack_name]['preload'].is_a?(String) - pack_data[:preload] = data['pack'][pack_name]['preload'] if data['pack'][pack_name]['preload'].is_a?(Array) - end - if skin != 'default' && data['skin'][skin] - pack_data[:skin] = skin if data['skin'][skin].include?(pack_name) - else # default skin - pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet'] - end - end - pack_data - end - - def pack?(data, pack_name) - if data['pack'].is_a?(Hash) && data['pack'].key?(pack_name) - return true if data['pack'][pack_name].is_a?(String) || data['pack'][pack_name].is_a?(Hash) - end - false - end - - def nil_pack(data, pack_name, skin = 'default') - { - common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), - flavour: data['name'], - pack: nil, - preload: nil, - skin: nil, - supported_locales: data['locales'], - } - end - - def resolve_pack(data, pack_name, skin = 'default') - result = pack(data, pack_name, skin) - unless result - if data['name'] && data.key?('fallback') - if data['fallback'].nil? - return nil_pack(data, pack_name, skin) - elsif data['fallback'].is_a?(String) && Themes.instance.flavour(data['fallback']) - return resolve_pack(Themes.instance.flavour(data['fallback']), pack_name) - elsif data['fallback'].is_a?(Array) - data['fallback'].each do |fallback| - return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback) - end - end - return nil_pack(data, pack_name, skin) - end - return data.key?('name') && data['name'] != Setting.default_settings['flavour'] ? resolve_pack(Themes.instance.flavour(Setting.default_settings['flavour']), pack_name) : nil_pack(data, pack_name, skin) - end - result - end - def use_pack(pack_name) @core = resolve_pack(Themes.instance.core, pack_name) @theme = resolve_pack(Themes.instance.flavour(current_flavour), pack_name, current_skin) @@ -223,4 +159,68 @@ class ApplicationController < ActionController::Base format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code } end end + + private + + def valid_pack?(data, pack_name) + data['pack'].is_a?(Hash) && [String, Hash].any? { |c| data['pack'][pack_name].is_a?(c) } + end + + def nil_pack(data, pack_name, skin) + { + common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), + flavour: data['name'], + pack: nil, + preload: nil, + skin: nil, + supported_locales: data['locales'], + } + end + + def pack(data, pack_name, skin) + pack_data = { + common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), + flavour: data['name'], + pack: pack_name, + preload: nil, + skin: nil, + supported_locales: data['locales'], + } + + return pack_data unless data['pack'][pack_name].is_a?(Hash) + + pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false + pack_data[:pack] = nil unless data['pack'][pack_name]['filename'] + + preloads = data['pack'][pack_name]['preload'] + pack_data[:preload] = [preloads] if preloads.is_a?(String) + pack_data[:preload] = preloads if preloads.is_a?(Array) + + if skin != 'default' && data['skin'][skin] + pack_data[:skin] = skin if data['skin'][skin].include?(pack_name) + else # default skin + pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet'] + end + + pack_data + end + + def resolve_pack(data, pack_name, skin = 'default') + return pack(data, pack_name, skin) if valid_pack?(data, pack_name) + return nil_pack(data, pack_name, skin) if data['name'].blank? + + fallbacks = [] + if data.key?('fallback') + fallbacks = data['fallback'] if data['fallback'].is_a?(Array) + fallbacks = [data['fallback']] if data['fallback'].is_a?(String) + elsif data['name'] != Setting.default_settings['flavour'] + fallbacks = [Setting.default_settings['flavour']] + end + + fallbacks.each do |fallback| + return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback) + end + + nil_pack(data, pack_name, skin) + end end From c61b29ebfdde6d0bafdd898d9b723d0444a81ca1 Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 16 Jan 2022 21:25:11 +0100 Subject: [PATCH 2/7] Refactor some more --- app/controllers/application_controller.rb | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 881c23f940..0b78e36d6c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -74,8 +74,8 @@ class ApplicationController < ActionController::Base end def use_pack(pack_name) - @core = resolve_pack(Themes.instance.core, pack_name) - @theme = resolve_pack(Themes.instance.flavour(current_flavour), pack_name, current_skin) + @core = resolve_pack_with_common(Themes.instance.core, pack_name) + @theme = resolve_pack_with_common(Themes.instance.flavour(current_flavour), pack_name, current_skin) end protected @@ -162,13 +162,13 @@ class ApplicationController < ActionController::Base private - def valid_pack?(data, pack_name) + def valid_pack_data?(data, pack_name) data['pack'].is_a?(Hash) && [String, Hash].any? { |c| data['pack'][pack_name].is_a?(c) } end - def nil_pack(data, pack_name, skin) + def nil_pack(data) { - common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), + use_common: true, flavour: data['name'], pack: nil, preload: nil, @@ -179,7 +179,7 @@ class ApplicationController < ActionController::Base def pack(data, pack_name, skin) pack_data = { - common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin), + use_common: true, flavour: data['name'], pack: pack_name, preload: nil, @@ -189,7 +189,7 @@ class ApplicationController < ActionController::Base return pack_data unless data['pack'][pack_name].is_a?(Hash) - pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false + pack_data[:use_common] = false if data['pack'][pack_name]['use_common'] == false pack_data[:pack] = nil unless data['pack'][pack_name]['filename'] preloads = data['pack'][pack_name]['preload'] @@ -205,9 +205,9 @@ class ApplicationController < ActionController::Base pack_data end - def resolve_pack(data, pack_name, skin = 'default') - return pack(data, pack_name, skin) if valid_pack?(data, pack_name) - return nil_pack(data, pack_name, skin) if data['name'].blank? + def resolve_pack(data, pack_name, skin) + return pack(data, pack_name, skin) if valid_pack_data?(data, pack_name) + return if data['name'].blank? fallbacks = [] if data.key?('fallback') @@ -221,6 +221,12 @@ class ApplicationController < ActionController::Base return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback) end - nil_pack(data, pack_name, skin) + nil + end + + def resolve_pack_with_common(data, pack_name, skin = 'default') + result = resolve_pack(data, pack_name, skin) || nil_pack(data) + result[:common] = resolve_pack(data, 'common', skin) if result.delete(:use_common) + result end end From 1b386c881c1a426c69a0f06fa6918fea3fcc2aaf Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 16 Jan 2022 21:46:48 +0100 Subject: [PATCH 3/7] Refactor theme config loading --- app/lib/themes.rb | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/app/lib/themes.rb b/app/lib/themes.rb index 2147904e4e..81e016d4a7 100644 --- a/app/lib/themes.rb +++ b/app/lib/themes.rb @@ -14,16 +14,20 @@ class Themes result = Hash.new Dir.glob(Rails.root.join('app', 'javascript', 'flavours', '*', 'theme.yml')) do |path| data = YAML.load_file(path) + next unless data['pack'] + dir = File.dirname(path) name = File.basename(dir) locales = [] screenshots = [] + if data['locales'] Dir.glob(File.join(dir, data['locales'], '*.{js,json}')) do |locale| localeName = File.basename(locale, File.extname(locale)) locales.push(localeName) unless localeName.match(/defaultMessages|whitelist|index/) end end + if data['screenshot'] if data['screenshot'].is_a? Array screenshots = data['screenshot'] @@ -31,38 +35,37 @@ class Themes screenshots.push(data['screenshot']) end end - if data['pack'] - data['name'] = name - data['locales'] = locales - data['screenshot'] = screenshots - data['skin'] = { 'default' => [] } - result[name] = data - end + + data['name'] = name + data['locales'] = locales + data['screenshot'] = screenshots + data['skin'] = { 'default' => [] } + result[name] = data end Dir.glob(Rails.root.join('app', 'javascript', 'skins', '*', '*')) do |path| ext = File.extname(path) skin = File.basename(path) name = File.basename(File.dirname(path)) - if result[name] - if File.directory?(path) - pack = [] - Dir.glob(File.join(path, '*.{css,scss}')) do |sheet| - pack.push(File.basename(sheet, File.extname(sheet))) - end - elsif ext.match(/^\.s?css$/i) - skin = File.basename(path, ext) - pack = ['common'] - end - if skin != 'default' - result[name]['skin'][skin] = pack + next unless result[name] + + if File.directory?(path) + pack = [] + Dir.glob(File.join(path, '*.{css,scss}')) do |sheet| + pack.push(File.basename(sheet, File.extname(sheet))) end + elsif ext.match(/^\.s?css$/i) + skin = File.basename(path, ext) + pack = ['common'] + end + + if skin != 'default' + result[name]['skin'][skin] = pack end end @core = core @conf = result - end def core From c789bcc844a1b2fd4747b694509a22cf84e4a718 Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 16 Jan 2022 22:03:43 +0100 Subject: [PATCH 4/7] Refactor theming HAML template a bit --- app/views/layouts/_theme.html.haml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/_theme.html.haml b/app/views/layouts/_theme.html.haml index 92de64b0d0..fe4e59280e 100644 --- a/app/views/layouts/_theme.html.haml +++ b/app/views/layouts/_theme.html.haml @@ -2,10 +2,11 @@ - if theme[:pack] != 'common' && theme[:common] = render partial: 'layouts/theme', object: theme[:common] - if theme[:pack] - = javascript_pack_tag theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}", crossorigin: 'anonymous' + - pack_path = theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}" + = javascript_pack_tag pack_path, crossorigin: 'anonymous' - if theme[:skin] - if !theme[:flavour] || theme[:skin] == 'default' - = stylesheet_pack_tag theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}", media: 'all', crossorigin: 'anonymous' + = stylesheet_pack_tag pack_path, media: 'all', crossorigin: 'anonymous' - else = stylesheet_pack_tag "skins/#{theme[:flavour]}/#{theme[:skin]}/#{theme[:pack]}", crossorigin: 'anonymous' - if theme[:preload] From de4c67c5fadc85dc0e66aafb6d5a6e24b41586cf Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 16 Jan 2022 22:25:53 +0100 Subject: [PATCH 5/7] Fix missing media: 'all' on default skins --- app/views/layouts/_theme.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/_theme.html.haml b/app/views/layouts/_theme.html.haml index fe4e59280e..5dba77621b 100644 --- a/app/views/layouts/_theme.html.haml +++ b/app/views/layouts/_theme.html.haml @@ -8,7 +8,7 @@ - if !theme[:flavour] || theme[:skin] == 'default' = stylesheet_pack_tag pack_path, media: 'all', crossorigin: 'anonymous' - else - = stylesheet_pack_tag "skins/#{theme[:flavour]}/#{theme[:skin]}/#{theme[:pack]}", crossorigin: 'anonymous' + = stylesheet_pack_tag "skins/#{theme[:flavour]}/#{theme[:skin]}/#{theme[:pack]}", media: 'all', crossorigin: 'anonymous' - if theme[:preload] - theme[:preload].each do |link| %link{ href: asset_pack_path("#{link}.js"), crossorigin: 'anonymous', rel: 'preload', as: 'script' }/ From c7e2b9cf64ab2db13e85470ae695b3b4c03fae79 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 17 Jan 2022 12:50:02 +0100 Subject: [PATCH 6/7] Move controller theming code to concern --- app/controllers/application_controller.rb | 76 +------------------- app/controllers/concerns/theming_concern.rb | 80 +++++++++++++++++++++ 2 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 app/controllers/concerns/theming_concern.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b78e36d6c..08cca0734f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base include SessionTrackingConcern include CacheConcern include DomainControlHelper + include ThemingConcern helper_method :current_account helper_method :current_session @@ -73,11 +74,6 @@ class ApplicationController < ActionController::Base new_user_session_path end - def use_pack(pack_name) - @core = resolve_pack_with_common(Themes.instance.core, pack_name) - @theme = resolve_pack_with_common(Themes.instance.flavour(current_flavour), pack_name, current_skin) - end - protected def truthy_param?(key) @@ -159,74 +155,4 @@ class ApplicationController < ActionController::Base format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code } end end - - private - - def valid_pack_data?(data, pack_name) - data['pack'].is_a?(Hash) && [String, Hash].any? { |c| data['pack'][pack_name].is_a?(c) } - end - - def nil_pack(data) - { - use_common: true, - flavour: data['name'], - pack: nil, - preload: nil, - skin: nil, - supported_locales: data['locales'], - } - end - - def pack(data, pack_name, skin) - pack_data = { - use_common: true, - flavour: data['name'], - pack: pack_name, - preload: nil, - skin: nil, - supported_locales: data['locales'], - } - - return pack_data unless data['pack'][pack_name].is_a?(Hash) - - pack_data[:use_common] = false if data['pack'][pack_name]['use_common'] == false - pack_data[:pack] = nil unless data['pack'][pack_name]['filename'] - - preloads = data['pack'][pack_name]['preload'] - pack_data[:preload] = [preloads] if preloads.is_a?(String) - pack_data[:preload] = preloads if preloads.is_a?(Array) - - if skin != 'default' && data['skin'][skin] - pack_data[:skin] = skin if data['skin'][skin].include?(pack_name) - else # default skin - pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet'] - end - - pack_data - end - - def resolve_pack(data, pack_name, skin) - return pack(data, pack_name, skin) if valid_pack_data?(data, pack_name) - return if data['name'].blank? - - fallbacks = [] - if data.key?('fallback') - fallbacks = data['fallback'] if data['fallback'].is_a?(Array) - fallbacks = [data['fallback']] if data['fallback'].is_a?(String) - elsif data['name'] != Setting.default_settings['flavour'] - fallbacks = [Setting.default_settings['flavour']] - end - - fallbacks.each do |fallback| - return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback) - end - - nil - end - - def resolve_pack_with_common(data, pack_name, skin = 'default') - result = resolve_pack(data, pack_name, skin) || nil_pack(data) - result[:common] = resolve_pack(data, 'common', skin) if result.delete(:use_common) - result - end end diff --git a/app/controllers/concerns/theming_concern.rb b/app/controllers/concerns/theming_concern.rb new file mode 100644 index 0000000000..6622bcff6b --- /dev/null +++ b/app/controllers/concerns/theming_concern.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +module ThemingConcern + extend ActiveSupport::Concern + + def use_pack(pack_name) + @core = resolve_pack_with_common(Themes.instance.core, pack_name) + @theme = resolve_pack_with_common(Themes.instance.flavour(current_flavour), pack_name, current_skin) + end + + private + + def valid_pack_data?(data, pack_name) + data['pack'].is_a?(Hash) && [String, Hash].any? { |c| data['pack'][pack_name].is_a?(c) } + end + + def nil_pack(data) + { + use_common: true, + flavour: data['name'], + pack: nil, + preload: nil, + skin: nil, + supported_locales: data['locales'], + } + end + + def pack(data, pack_name, skin) + pack_data = { + use_common: true, + flavour: data['name'], + pack: pack_name, + preload: nil, + skin: nil, + supported_locales: data['locales'], + } + + return pack_data unless data['pack'][pack_name].is_a?(Hash) + + pack_data[:use_common] = false if data['pack'][pack_name]['use_common'] == false + pack_data[:pack] = nil unless data['pack'][pack_name]['filename'] + + preloads = data['pack'][pack_name]['preload'] + pack_data[:preload] = [preloads] if preloads.is_a?(String) + pack_data[:preload] = preloads if preloads.is_a?(Array) + + if skin != 'default' && data['skin'][skin] + pack_data[:skin] = skin if data['skin'][skin].include?(pack_name) + else # default skin + pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet'] + end + + pack_data + end + + def resolve_pack(data, pack_name, skin) + return pack(data, pack_name, skin) if valid_pack_data?(data, pack_name) + return if data['name'].blank? + + fallbacks = [] + if data.key?('fallback') + fallbacks = data['fallback'] if data['fallback'].is_a?(Array) + fallbacks = [data['fallback']] if data['fallback'].is_a?(String) + elsif data['name'] != Setting.default_settings['flavour'] + fallbacks = [Setting.default_settings['flavour']] + end + + fallbacks.each do |fallback| + return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback) + end + + nil + end + + def resolve_pack_with_common(data, pack_name, skin = 'default') + result = resolve_pack(data, pack_name, skin) || nil_pack(data) + result[:common] = resolve_pack(data, 'common', skin) if result.delete(:use_common) + result + end +end From b9ed7e0f55f082de429d5e7d545a5424de0052dd Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 17 Jan 2022 13:06:06 +0100 Subject: [PATCH 7/7] Please CodeClimate --- app/controllers/concerns/theming_concern.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/concerns/theming_concern.rb b/app/controllers/concerns/theming_concern.rb index 6622bcff6b..1ee3256c04 100644 --- a/app/controllers/concerns/theming_concern.rb +++ b/app/controllers/concerns/theming_concern.rb @@ -46,8 +46,8 @@ module ThemingConcern if skin != 'default' && data['skin'][skin] pack_data[:skin] = skin if data['skin'][skin].include?(pack_name) - else # default skin - pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet'] + elsif data['pack'][pack_name]['stylesheet'] + pack_data[:skin] = 'default' end pack_data