mirror of
https://github.com/discourse/discourse.git
synced 2025-10-03 17:21:20 +08:00
This commit introduces the concept of themeable site settings, which is a new tool for theme authors that lives alongside theme modifiers and theme settings. Here is a quick summary: * Theme settings - These are custom settings used to control UI and functionality within your theme or component and provide configuration options. These cannot change core Discourse functionality. * Theme modifiers - Allows a theme or a component to modify selected server-side functionality of core Discourse as an alternative to building a plugin. * Themeable site settings (new) - Allows a theme (not components) to override a small subset of core site settings, which generally control parts of the UI and other minor functionality. This allows themes to have a greater control over the full site experience. Themeable site settings will be shown for all themes, whether the theme changes the value or not, and have a similar UI to custom theme settings. We are also introducing a new page at `/admin/config/theme-site-settings` that allows admins to see all possible themeable site settings, and which themes are changing the value from the default. ### Configuration Theme authors can configure initial values themeable site settings using a section in the `about.json` file like so: ```json "theme_site_settings": { "search_experience": "search_field" } ``` These values will not change when the theme updates, because we cannot know if admins have manually changed them. ### Limitations Themeable site settings are only really intended to control elements of the UI, and when retrieving their value we require a theme ID, so these limitations apply: - Themeable site settings cannot be used in Sidekiq jobs - Themeable site settings cannot be used in markdown rules - Themeable site settings will be cached separately to client site settings using theme ID as a key - Themeable site settings will override keys on the `siteSettings` service on the client using the application preloader - `SiteSetting.client_settings_json` will not include themeable site settings, instead you can call `SiteSetting.theme_site_settings_json` with a theme ID ### Initial settings There are only two site settings that will be themeable to begin with: * `enable_welcome_banner` * `search_experience` And our new Horizon theme will take advantage of both. Over time, more settings that control elements of the UI will be exposed this way.
194 lines
7.5 KiB
Ruby
194 lines
7.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module SiteSettings
|
|
end
|
|
|
|
module SiteSettings::DeprecatedSettings
|
|
SETTINGS = [
|
|
# [<old setting>, <new_setting>, <override>, <version to drop>]
|
|
["anonymous_posting_min_trust_level", "anonymous_posting_allowed_groups", false, "3.3"],
|
|
["shared_drafts_min_trust_level", "shared_drafts_allowed_groups", false, "3.3"],
|
|
["min_trust_level_for_here_mention", "here_mention_allowed_groups", false, "3.3"],
|
|
["approve_unless_trust_level", "approve_unless_allowed_groups", false, "3.3"],
|
|
[
|
|
"approve_new_topics_unless_trust_level",
|
|
"approve_new_topics_unless_allowed_groups",
|
|
false,
|
|
"3.3",
|
|
],
|
|
["email_in_min_trust", "email_in_allowed_groups", false, "3.3"],
|
|
["min_trust_to_edit_wiki_post", "edit_wiki_post_allowed_groups", false, "3.3"],
|
|
["allow_uploaded_avatars", "uploaded_avatars_allowed_groups", false, "3.3"],
|
|
["min_trust_to_create_topic", "create_topic_allowed_groups", false, "3.3"],
|
|
["min_trust_to_edit_post", "edit_post_allowed_groups", false, "3.3"],
|
|
["min_trust_to_flag_posts", "flag_post_allowed_groups", false, "3.3"],
|
|
["tl4_delete_posts_and_topics", "delete_all_posts_and_topics_allowed_groups", false, "3.3"],
|
|
[
|
|
"min_trust_level_to_allow_user_card_background",
|
|
"user_card_background_allowed_groups",
|
|
false,
|
|
"3.3",
|
|
],
|
|
["min_trust_level_to_allow_invite", "invite_allowed_groups", false, "3.3"],
|
|
["min_trust_level_to_allow_ignore", "ignore_allowed_groups", false, "3.3"],
|
|
["min_trust_to_allow_self_wiki", "self_wiki_allowed_groups", false, "3.3"],
|
|
["min_trust_to_create_tag", "create_tag_allowed_groups", false, "3.3"],
|
|
["min_trust_to_send_email_messages", "send_email_messages_allowed_groups", false, "3.3"],
|
|
["review_media_unless_trust_level", "skip_review_media_groups", false, "3.3"],
|
|
["min_trust_to_post_embedded_media", "embedded_media_post_allowed_groups", false, "3.3"],
|
|
["min_trust_to_post_links", "post_links_allowed_groups", false, "3.3"],
|
|
["min_trust_level_for_user_api_key", "user_api_key_allowed_groups", false, "3.3"],
|
|
["min_trust_level_to_tag_topics", "tag_topic_allowed_groups", false, "3.3"],
|
|
[
|
|
"min_trust_level_to_allow_profile_background",
|
|
"profile_background_allowed_groups",
|
|
false,
|
|
"3.3",
|
|
],
|
|
["min_first_post_typing_time", "fast_typing_threshold", false, "3.4"],
|
|
["twitter_summary_large_image", "x_summary_large_image", false, "3.4"],
|
|
["external_system_avatars_enabled", "external_system_avatars_url", false, "3.5"],
|
|
]
|
|
|
|
OVERRIDE_TL_GROUP_SETTINGS = %w[
|
|
anonymous_posting_min_trust_level
|
|
shared_drafts_min_trust_level
|
|
min_trust_level_for_here_mention
|
|
approve_unless_trust_level
|
|
approve_new_topics_unless_trust_level
|
|
email_in_min_trust
|
|
min_trust_to_edit_wiki_post
|
|
allow_uploaded_avatars
|
|
min_trust_to_create_topic
|
|
min_trust_to_edit_post
|
|
min_trust_to_flag_posts
|
|
tl4_delete_posts_and_topics
|
|
min_trust_level_to_allow_user_card_background
|
|
min_trust_level_to_allow_invite
|
|
min_trust_level_to_allow_ignore
|
|
min_trust_to_create_tag
|
|
min_trust_to_send_email_messages
|
|
review_media_unless_trust_level
|
|
min_trust_to_post_embedded_media
|
|
min_trust_to_post_links
|
|
min_trust_level_for_user_api_key
|
|
min_trust_level_to_tag_topics
|
|
min_trust_level_to_allow_profile_background
|
|
]
|
|
|
|
def group_to_tl(old_setting, new_setting)
|
|
tl_and_staff = is_tl_and_staff_setting?(old_setting)
|
|
|
|
valid_auto_groups =
|
|
self.public_send("#{new_setting}_map") &
|
|
# We only want auto groups, no actual groups because they cannot be
|
|
# mapped to TLs.
|
|
Group.auto_groups_between(tl_and_staff ? :admins : :trust_level_0, :trust_level_4)
|
|
|
|
# We don't want to return nil because this could lead to permission holes;
|
|
# so we return the max available permission in this case.
|
|
return tl_and_staff ? "admin" : TrustLevel[4] if valid_auto_groups.empty?
|
|
|
|
if tl_and_staff
|
|
valid_auto_groups_excluding_staff_and_admins =
|
|
valid_auto_groups -
|
|
[Group::AUTO_GROUPS[:staff], Group::AUTO_GROUPS[:admins], Group::AUTO_GROUPS[:moderators]]
|
|
|
|
if valid_auto_groups_excluding_staff_and_admins.any?
|
|
return valid_auto_groups_excluding_staff_and_admins.min - Group::AUTO_GROUPS[:trust_level_0]
|
|
end
|
|
|
|
if valid_auto_groups.include?(Group::AUTO_GROUPS[:moderators])
|
|
"moderator"
|
|
elsif valid_auto_groups.include?(Group::AUTO_GROUPS[:staff])
|
|
"staff"
|
|
elsif valid_auto_groups.include?(Group::AUTO_GROUPS[:admins])
|
|
"admin"
|
|
end
|
|
else
|
|
valid_auto_groups.min - Group::AUTO_GROUPS[:trust_level_0]
|
|
end
|
|
end
|
|
|
|
def tl_to_group(old_setting, val)
|
|
tl_and_staff = is_tl_and_staff_setting?(old_setting)
|
|
|
|
if val == "admin"
|
|
Group::AUTO_GROUPS[:admins]
|
|
elsif val == "staff"
|
|
Group::AUTO_GROUPS[:staff]
|
|
else
|
|
if tl_and_staff
|
|
"#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:staff]}|#{val.to_i + Group::AUTO_GROUPS[:trust_level_0]}"
|
|
else
|
|
"#{val.to_i + Group::AUTO_GROUPS[:trust_level_0]}"
|
|
end
|
|
end
|
|
end
|
|
|
|
def is_tl_and_staff_setting?(old_setting)
|
|
SiteSetting.type_supervisor.get_type(old_setting.to_sym) == :enum &&
|
|
SiteSetting.type_supervisor.get_enum_class(old_setting.to_sym).name ==
|
|
TrustLevelAndStaffSetting.name
|
|
end
|
|
|
|
def setup_deprecated_methods
|
|
SETTINGS.each do |old_setting, new_setting, override, version|
|
|
SiteSetting.singleton_class.alias_method(:"_#{old_setting}", :"#{old_setting}") if !override
|
|
|
|
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
|
|
define_singleton_method "_group_to_tl_#{old_setting}" do |warn: true|
|
|
group_to_tl(old_setting, new_setting)
|
|
end
|
|
end
|
|
|
|
define_singleton_method old_setting do |scoped_to = nil, warn: true|
|
|
if warn
|
|
Discourse.deprecate(
|
|
"`SiteSetting.#{old_setting}` has been deprecated. Please use `SiteSetting.#{new_setting}` instead.",
|
|
drop_from: version,
|
|
)
|
|
end
|
|
|
|
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
|
|
self.public_send("_group_to_tl_#{old_setting}")
|
|
else
|
|
self.public_send(override ? new_setting : "_#{old_setting}", scoped_to)
|
|
end
|
|
end
|
|
|
|
SiteSetting.singleton_class.alias_method(:"_#{old_setting}?", :"#{old_setting}?") if !override
|
|
|
|
define_singleton_method "#{old_setting}?" do |scoped_to = nil, warn: true|
|
|
if warn
|
|
Discourse.deprecate(
|
|
"`SiteSetting.#{old_setting}?` has been deprecated. Please use `SiteSetting.#{new_setting}?` instead.",
|
|
drop_from: version,
|
|
)
|
|
end
|
|
|
|
self.public_send("#{override ? new_setting : "_" + old_setting}?", scoped_to)
|
|
end
|
|
|
|
SiteSetting.singleton_class.alias_method(:"_#{old_setting}=", :"#{old_setting}=") if !override
|
|
|
|
define_singleton_method "#{old_setting}=" do |val, warn: true|
|
|
if warn
|
|
Discourse.deprecate(
|
|
"`SiteSetting.#{old_setting}=` has been deprecated. Please use `SiteSetting.#{new_setting}=` instead.",
|
|
drop_from: version,
|
|
)
|
|
end
|
|
|
|
if OVERRIDE_TL_GROUP_SETTINGS.include?(old_setting)
|
|
# We want to set both the new group setting here to the equivalent of the
|
|
# TL, as well as setting the TL value in the DB so they remain in sync.
|
|
self.public_send("_#{old_setting}=", val)
|
|
self.public_send("#{new_setting}=", tl_to_group(old_setting, val).to_s)
|
|
else
|
|
self.public_send("#{override ? new_setting : "_" + old_setting}=", val)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|