Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify glitch-soc's theming system #2697

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class BaseController < ApplicationController

layout 'admin'

before_action :set_pack
before_action :set_body_classes
before_action :set_cache_headers

Expand All @@ -19,10 +18,6 @@ def set_body_classes
@body_classes = 'admin'
end

def set_pack
use_pack 'admin'
end

def set_cache_headers
response.cache_control.replace(private: true, no_store: true)
end
Expand Down
11 changes: 3 additions & 8 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ApplicationController < ActionController::Base
helper_method :current_session
helper_method :current_flavour
helper_method :current_skin
helper_method :current_theme
helper_method :single_user_mode?
helper_method :use_seamless_external_login?
helper_method :omniauth_only?
Expand Down Expand Up @@ -164,10 +165,7 @@ def body_class_string

def respond_with_error(code)
respond_to do |format|
format.any do
use_pack 'error'
render "errors/#{code}", layout: 'error', status: code, formats: [:html]
end
format.any { render "errors/#{code}", layout: 'error', status: code, formats: [:html] }
format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code }
end
end
Expand All @@ -176,10 +174,7 @@ def check_self_destruct!
return unless self_destruct?

respond_to do |format|
format.any do
use_pack 'error'
render 'errors/self_destruct', layout: 'auth', status: 410, formats: [:html]
end
format.any { render 'errors/self_destruct', layout: 'auth', status: 410, formats: [:html] }
format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[410] }, status: 410 }
end
end
Expand Down
7 changes: 0 additions & 7 deletions app/controllers/auth/challenges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class Auth::ChallengesController < ApplicationController

layout 'auth'

before_action :set_pack
before_action :authenticate_user!

skip_before_action :check_self_destruct!
Expand All @@ -21,10 +20,4 @@ def create
render_challenge
end
end

private

def set_pack
use_pack 'auth'
end
end
5 changes: 0 additions & 5 deletions app/controllers/auth/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController
layout 'auth'

before_action :set_body_classes
before_action :set_pack
before_action :set_confirmation_user!, only: [:show, :confirm_captcha]
before_action :redirect_confirmed_user, if: :signed_in_confirmed_user?

Expand Down Expand Up @@ -66,10 +65,6 @@ def captcha_user_bypass?
@confirmation_user.nil? || @confirmation_user.confirmed?
end

def set_pack
use_pack 'auth'
end

def redirect_confirmed_user
redirect_to(current_user.approved? ? root_path : edit_user_registration_path)
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class Auth::PasswordsController < Devise::PasswordsController
skip_before_action :check_self_destruct!
before_action :redirect_invalid_reset_token, only: :edit, unless: :reset_password_token_is_valid?
before_action :set_pack
before_action :set_body_classes

layout 'auth'
Expand Down Expand Up @@ -32,8 +31,4 @@ def set_body_classes
def reset_password_token_is_valid?
resource_class.with_reset_password_token(params[:reset_password_token]).present?
end

def set_pack
use_pack 'auth'
end
end
5 changes: 0 additions & 5 deletions app/controllers/auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Auth::RegistrationsController < Devise::RegistrationsController
before_action :set_invite, only: [:new, :create]
before_action :check_enabled_registrations, only: [:new, :create]
before_action :configure_sign_up_params, only: [:create]
before_action :set_pack
before_action :set_sessions, only: [:edit, :update]
before_action :set_strikes, only: [:edit, :update]
before_action :set_body_classes, only: [:new, :create, :edit, :update]
Expand Down Expand Up @@ -97,10 +96,6 @@ def invite_code

private

def set_pack
use_pack %w(edit update).include?(action_name) ? 'admin' : 'auth'
end

def set_body_classes
@body_classes = %w(edit update).include?(action_name) ? 'admin' : 'lighter'
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Auth::SessionsController < Devise::SessionsController
skip_before_action :require_functional!
skip_before_action :update_user_sign_in

prepend_before_action :set_pack
prepend_before_action :check_suspicious!, only: [:create]

include Auth::TwoFactorAuthenticationConcern
Expand Down Expand Up @@ -104,10 +103,6 @@ def require_no_authentication

private

def set_pack
use_pack 'auth'
end

def set_body_classes
@body_classes = 'lighter'
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/auth/setup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class Auth::SetupController < ApplicationController
layout 'auth'

before_action :set_pack
before_action :authenticate_user!
before_action :require_unconfirmed_or_pending!
before_action :set_body_classes
Expand Down Expand Up @@ -43,8 +42,4 @@ def set_body_classes
def user_params
params.require(:user).permit(:email)
end

def set_pack
use_pack 'sign_up'
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def authenticate_with_two_factor_via_otp(user)
def prompt_for_two_factor(user)
register_attempt_in_session(user)

use_pack 'auth'

@body_classes = 'lighter'
@webauthn_enabled = user.webauthn_enabled?
@scheme_type = if user.webauthn_enabled? && user_params[:otp_attempt].blank?
Expand Down
81 changes: 8 additions & 73 deletions app/controllers/concerns/theming_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,22 @@
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 current_flavour
[current_user&.setting_flavour, Setting.flavour, 'glitch', 'vanilla'].find { |flavour| Themes.instance.flavours.include?(flavour) }
@current_flavour ||= [current_user&.setting_flavour, Setting.flavour, 'glitch', 'vanilla'].find { |flavour| Themes.instance.flavours.include?(flavour) }
end

def current_skin
skins = Themes.instance.skins_for(current_flavour)
[current_user&.setting_skin, Setting.skin, 'default'].find { |skin| skins.include?(skin) }
end

def valid_pack_data?(data, pack_name)
data['pack'].is_a?(Hash) && data['pack'][pack_name].present?
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)
elsif data['pack'][pack_name]['stylesheet']
pack_data[:skin] = 'default'
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']]
@current_skin ||= begin
skins = Themes.instance.skins_for(current_flavour)
[current_user&.setting_skin, Setting.skin, 'default'].find { |skin| skins.include?(skin) }
end

fallbacks.each do |fallback|
return resolve_pack(Themes.instance.flavour(fallback), pack_name, skin) 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
def current_theme
# NOTE: this is slightly different from upstream, as it's a derived value used
# for the sole purpose of pointing to the appropriate stylesheet pack
"skins/#{current_flavour}/#{current_skin}"
end
end
5 changes: 0 additions & 5 deletions app/controllers/concerns/web_app_controller_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module WebAppControllerConcern
vary_by 'Accept, Accept-Language, Cookie'

before_action :redirect_unauthenticated_to_permalinks!
before_action :set_pack
before_action :set_app_body_class
end

Expand Down Expand Up @@ -37,8 +36,4 @@ def redirect_unauthenticated_to_permalinks!
end
end
end

def set_pack
use_pack 'home'
end
end
5 changes: 0 additions & 5 deletions app/controllers/disputes/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ class Disputes::BaseController < ApplicationController

before_action :set_body_classes
before_action :authenticate_user!
before_action :set_pack
before_action :set_cache_headers

private

def set_pack
use_pack 'admin'
end

def set_body_classes
@body_classes = 'admin'
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/filters/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Filters::StatusesController < ApplicationController
before_action :authenticate_user!
before_action :set_filter
before_action :set_status_filters
before_action :set_pack
before_action :set_body_classes
before_action :set_cache_headers

Expand All @@ -27,10 +26,6 @@ def batch

private

def set_pack
use_pack 'admin'
end

def set_filter
@filter = current_account.custom_filters.find(params[:filter_id])
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class FiltersController < ApplicationController

before_action :authenticate_user!
before_action :set_filter, only: [:edit, :update, :destroy]
before_action :set_pack
before_action :set_body_classes
before_action :set_cache_headers

Expand Down Expand Up @@ -45,10 +44,6 @@ def destroy

private

def set_pack
use_pack 'settings'
end

def set_filter
@filter = current_account.custom_filters.find(params[:id])
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/invites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class InvitesController < ApplicationController
layout 'admin'

before_action :authenticate_user!
before_action :set_pack
before_action :set_body_classes
before_action :set_cache_headers

Expand Down Expand Up @@ -40,10 +39,6 @@ def destroy

private

def set_pack
use_pack 'settings'
end

def invites
current_user.invites.order(id: :desc)
end
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class MediaController < ApplicationController
before_action :verify_permitted_status!
before_action :check_playable, only: :player
before_action :allow_iframing, only: :player
before_action :set_pack, only: :player

content_security_policy only: :player do |policy|
policy.frame_ancestors(false)
Expand Down Expand Up @@ -48,8 +47,4 @@ def check_playable
def allow_iframing
response.headers.delete('X-Frame-Options')
end

def set_pack
use_pack 'public'
end
end
5 changes: 0 additions & 5 deletions app/controllers/oauth/authorizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController

before_action :store_current_location
before_action :authenticate_resource_owner!
before_action :set_pack
before_action :set_cache_headers

content_security_policy do |p|
Expand All @@ -20,10 +19,6 @@ def store_current_location
store_location_for(:user, request.url)
end

def set_pack
use_pack 'auth'
end

def render_success
if skip_authorization? || (matching_token? && !truthy_param?('force_login'))
redirect_or_render authorize_response
Expand Down
Loading
Loading