Skip to content

Commit

Permalink
Add: #883 管理者の設定したカスタムCSSをユーザー単位で無効にするオプション (#902)
Browse files Browse the repository at this point in the history
* Add: #883 管理者の設定したカスタムCSSをユーザー単位で無効にするオプション

* ラベル変更
  • Loading branch information
kmycode authored Nov 1, 2024
1 parent b30086d commit 943db14
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 14 deletions.
12 changes: 1 addition & 11 deletions app/controllers/custom_css_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ def show
def custom_css_styles
Setting.custom_css
end

def user_custom_css?
return false if current_user.nil?

current_user.setting_use_custom_css && current_user.custom_css_text.present?
end

def user_custom_css
current_user.custom_css_text
end
helper_method :custom_css_styles, :user_custom_css?, :user_custom_css
helper_method :custom_css_styles

def set_user_roles
@user_roles = UserRole.providing_styles
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/system_css_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class SystemCssController < ActionController::Base # rubocop:disable Rails/ApplicationController
before_action :set_user_roles

def show
expires_in 3.minutes, public: true
render content_type: 'text/css'
end

private

def set_user_roles
@user_roles = UserRole.providing_styles
end
end
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ def mascot_url
full_asset_url(instance_presenter.mascot&.file&.url || frontend_asset_path('images/elephant_ui_plane.svg'))
end

def server_css?
return true if current_account&.user.nil?

current_account.user.setting_use_server_css
end

def user_custom_css?
return false if current_account&.user.nil?

Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/user/has_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ def setting_hide_favourite_menu
settings['web.hide_favourite_menu']
end

def setting_use_server_css
settings['web.use_server_css']
end

def setting_use_custom_css
settings['web.use_custom_css']
end
Expand Down
1 change: 1 addition & 0 deletions app/models/user_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class KeyError < Error; end
setting :use_blurhash, default: true
setting :use_pending_items, default: false
setting :use_system_font, default: false
setting :use_server_css, default: true
setting :use_custom_css, default: false
setting :content_font_size, default: 'medium', in: %w(medium large x_large xx_large)
setting :bookmark_category_needed, default: false
Expand Down
6 changes: 6 additions & 0 deletions app/views/custom_css/show_system.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%- @user_roles.each do |role| %>
.user-role-<%= role.id %> {
--user-role-accent: <%= role.color %>;
}

<%- end %>
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
= csrf_meta_tags unless skip_csrf_meta_tags?
%meta{ name: 'style-nonce', content: request.content_security_policy_nonce }

= stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'
- if server_css?
= stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'
- else
= stylesheet_link_tag system_css_path, skip_pipeline: true, host: root_url, media: 'all'

- if user_custom_css?
= stylesheet_link_tag user_custom_css_path({ version: user_custom_css_version }), skip_pipeline: true, host: root_url, media: 'all'
Expand Down
7 changes: 7 additions & 0 deletions app/views/settings/preferences/custom_css/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
= render 'shared/error_messages', object: current_user

= f.simple_fields_for :settings, current_user.settings do |ff|
.fields-group
= ff.input :'web.use_server_css',
hint: false,
label: I18n.t('simple_form.labels.defaults.setting_use_server_css'),
kmyblue: true,
wrapper: :with_label

.fields-group
= ff.input :'web.use_custom_css',
hint: false,
Expand Down
6 changes: 6 additions & 0 deletions app/views/system_css/show.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%- @user_roles.each do |role| %>
.user-role-<%= role.id %> {
--user-role-accent: <%= role.color %>;
}

<%- end %>
3 changes: 2 additions & 1 deletion config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ en:
setting_trends: Show today's trends
setting_unfollow_modal: Show confirmation dialog before unfollowing someone
setting_use_blurhash: Show colorful gradients for hidden media
setting_use_custom_css: Enable custom CSS
setting_use_custom_css: Enable custom CSS set by yourself
setting_use_pending_items: Slow mode
setting_use_public_index: Include permitted accounts post to results of search
setting_use_server_css: Enable server CSS set by the administrator
severity: Severity
sign_in_token_attempt: Security code
title: Title
Expand Down
3 changes: 2 additions & 1 deletion config/locales/simple_form.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ ja:
setting_trends: 本日のトレンドタグを表示する
setting_unfollow_modal: フォローを解除する前に確認ダイアログを表示する
setting_use_blurhash: 非表示のメディアを色付きのぼかしで表示する
setting_use_custom_css: カスタムCSSを有効にする
setting_use_custom_css: あなた自身が設定する以下のカスタムCSSを有効にする
setting_use_pending_items: 手動更新モード
setting_use_public_index: Mastodonの標準設定によって検索が許可されたアカウントの公開投稿を検索結果に含める
setting_use_server_css: 管理者の設定したカスタムCSSを有効にする
severity: 重大性
sign_in_token_attempt: セキュリティコード
title: タイトル
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def redirect_with_vary(path)
get 'manifest', to: 'manifests#show', defaults: { format: 'json' }
get 'intent', to: 'intents#show'
get 'custom.css', to: 'custom_css#show', as: :custom_css
get 'system.css', to: 'system_css#show', as: :system_css
get 'user_custom.css', to: 'user_custom_css#show', as: :user_custom_css

get 'remote_interaction_helper', to: 'remote_interaction_helper#index'
Expand Down

0 comments on commit 943db14

Please sign in to comment.