2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Admin::SiteSettingsController < Admin::AdminController
|
2017-08-06 21:43:09 -04:00
|
|
|
rescue_from Discourse::InvalidParameters do |e|
|
|
|
|
render_json_error e.message, status: 422
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def index
|
2023-03-01 05:31:57 -05:00
|
|
|
render_json_dump(site_settings: SiteSetting.all_settings)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def update
|
2014-01-06 07:03:53 -05:00
|
|
|
params.require(:id)
|
|
|
|
id = params[:id]
|
|
|
|
value = params[id]
|
2014-04-08 04:59:48 -04:00
|
|
|
value.strip! if value.is_a?(String)
|
2021-02-22 13:10:54 -05:00
|
|
|
|
2022-10-10 21:14:13 -04:00
|
|
|
new_setting_name =
|
|
|
|
SiteSettings::DeprecatedSettings::SETTINGS.find do |old_name, new_name, override, _|
|
|
|
|
if old_name == id
|
|
|
|
if !override
|
|
|
|
raise Discourse::InvalidParameters,
|
|
|
|
"You cannot change this site setting because it is deprecated, use #{new_name} instead."
|
|
|
|
end
|
2022-10-26 18:38:50 -04:00
|
|
|
|
2022-10-10 21:14:13 -04:00
|
|
|
break new_name
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2022-10-10 21:14:13 -04:00
|
|
|
end
|
2022-10-26 18:38:50 -04:00
|
|
|
|
2021-02-22 13:10:54 -05:00
|
|
|
id = new_setting_name if new_setting_name
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
raise_access_hidden_setting(id)
|
2018-11-14 02:03:02 -05:00
|
|
|
|
2020-10-13 09:17:06 -04:00
|
|
|
if SiteSetting.type_supervisor.get_type(id) == :uploaded_image_list
|
|
|
|
value = Upload.get_from_urls(value.split("|")).to_a
|
|
|
|
end
|
|
|
|
|
2018-11-14 02:03:02 -05:00
|
|
|
value = Upload.get_from_url(value) || "" if SiteSetting.type_supervisor.get_type(id) == :upload
|
|
|
|
|
2020-08-31 23:07:41 -04:00
|
|
|
update_existing_users = params[:update_existing_user].present?
|
2023-04-18 02:32:18 -04:00
|
|
|
previous_value = value_or_default(SiteSetting.get(id)) if update_existing_users
|
2019-10-15 09:11:27 -04:00
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
SiteSetting.set_and_log(id, value, current_user)
|
2019-10-15 09:11:27 -04:00
|
|
|
|
|
|
|
if update_existing_users
|
2022-05-23 09:20:51 -04:00
|
|
|
new_value = value_or_default(value)
|
2019-10-30 21:18:03 -04:00
|
|
|
|
|
|
|
if (user_option = user_options[id.to_sym]).present?
|
|
|
|
if user_option == "text_size_key"
|
|
|
|
previous_value = UserOption.text_sizes[previous_value.to_sym]
|
|
|
|
new_value = UserOption.text_sizes[new_value.to_sym]
|
|
|
|
elsif user_option == "title_count_mode_key"
|
|
|
|
previous_value = UserOption.title_count_modes[previous_value.to_sym]
|
|
|
|
new_value = UserOption.title_count_modes[new_value.to_sym]
|
|
|
|
end
|
2019-10-15 09:11:27 -04:00
|
|
|
|
2020-03-19 15:25:47 -04:00
|
|
|
attrs = { user_option => new_value }
|
|
|
|
attrs[:email_digests] = (new_value.to_i != 0) if id == "default_email_digest_frequency"
|
2019-10-15 09:11:27 -04:00
|
|
|
|
2022-05-23 09:20:51 -04:00
|
|
|
UserOption.human_users.where(user_option => previous_value).update_all(attrs)
|
2019-10-30 21:18:03 -04:00
|
|
|
elsif id.start_with?("default_categories_")
|
|
|
|
previous_category_ids = previous_value.split("|")
|
|
|
|
new_category_ids = new_value.split("|")
|
|
|
|
|
2022-05-22 23:21:45 -04:00
|
|
|
notification_level = category_notification_level(id)
|
2019-10-15 09:11:27 -04:00
|
|
|
|
2020-08-31 23:07:41 -04:00
|
|
|
categories_to_unwatch = previous_category_ids - new_category_ids
|
|
|
|
CategoryUser.where(
|
|
|
|
category_id: categories_to_unwatch,
|
|
|
|
notification_level: notification_level,
|
|
|
|
).delete_all
|
|
|
|
TopicUser
|
|
|
|
.joins(:topic)
|
|
|
|
.where(
|
|
|
|
notification_level: TopicUser.notification_levels[:watching],
|
|
|
|
notifications_reason_id: TopicUser.notification_reasons[:auto_watch_category],
|
|
|
|
topics: {
|
|
|
|
category_id: categories_to_unwatch,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.update_all(notification_level: TopicUser.notification_levels[:regular])
|
2019-10-15 09:11:27 -04:00
|
|
|
|
2019-10-30 21:18:03 -04:00
|
|
|
(new_category_ids - previous_category_ids).each do |category_id|
|
|
|
|
skip_user_ids = CategoryUser.where(category_id: category_id).pluck(:user_id)
|
|
|
|
|
2020-06-18 14:09:54 -04:00
|
|
|
User
|
|
|
|
.real
|
|
|
|
.where(staged: false)
|
|
|
|
.where.not(id: skip_user_ids)
|
|
|
|
.select(:id)
|
|
|
|
.find_in_batches do |users|
|
2019-10-30 21:18:03 -04:00
|
|
|
category_users = []
|
|
|
|
users.each do |user|
|
|
|
|
category_users << {
|
|
|
|
category_id: category_id,
|
|
|
|
user_id: user.id,
|
|
|
|
notification_level: notification_level,
|
|
|
|
}
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-10-30 21:18:03 -04:00
|
|
|
CategoryUser.insert_all!(category_users)
|
|
|
|
end
|
2019-11-01 03:10:13 -04:00
|
|
|
end
|
|
|
|
elsif id.start_with?("default_tags_")
|
|
|
|
previous_tag_ids = Tag.where(name: previous_value.split("|")).pluck(:id)
|
|
|
|
new_tag_ids = Tag.where(name: new_value.split("|")).pluck(:id)
|
|
|
|
now = Time.zone.now
|
2019-10-30 21:18:03 -04:00
|
|
|
|
2022-05-22 23:21:45 -04:00
|
|
|
notification_level = tag_notification_level(id)
|
2019-11-01 03:10:13 -04:00
|
|
|
|
2019-11-18 01:36:13 -05:00
|
|
|
TagUser.where(
|
|
|
|
tag_id: (previous_tag_ids - new_tag_ids),
|
|
|
|
notification_level: notification_level,
|
|
|
|
).delete_all
|
2019-11-01 03:10:13 -04:00
|
|
|
|
|
|
|
(new_tag_ids - previous_tag_ids).each do |tag_id|
|
|
|
|
skip_user_ids = TagUser.where(tag_id: tag_id).pluck(:user_id)
|
|
|
|
|
2020-06-18 14:09:54 -04:00
|
|
|
User
|
|
|
|
.real
|
|
|
|
.where(staged: false)
|
|
|
|
.where.not(id: skip_user_ids)
|
|
|
|
.select(:id)
|
|
|
|
.find_in_batches do |users|
|
2019-11-01 03:10:13 -04:00
|
|
|
tag_users = []
|
|
|
|
users.each do |user|
|
|
|
|
tag_users << {
|
|
|
|
tag_id: tag_id,
|
|
|
|
user_id: user.id,
|
|
|
|
notification_level: notification_level,
|
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
}
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-11-01 03:10:13 -04:00
|
|
|
TagUser.insert_all!(tag_users)
|
|
|
|
end
|
2019-10-30 21:18:03 -04:00
|
|
|
end
|
2022-10-26 18:38:50 -04:00
|
|
|
elsif is_sidebar_default_setting?(id)
|
|
|
|
Jobs.enqueue(
|
|
|
|
:backfill_sidebar_site_settings,
|
|
|
|
setting_name: id,
|
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value,
|
|
|
|
)
|
2019-10-15 09:11:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
2019-11-17 13:39:38 -05:00
|
|
|
def user_count
|
|
|
|
params.require(:site_setting_id)
|
|
|
|
id = params[:site_setting_id]
|
|
|
|
raise Discourse::NotFound unless id.start_with?("default_")
|
2022-05-23 09:20:51 -04:00
|
|
|
new_value = value_or_default(params[id])
|
2019-11-17 13:39:38 -05:00
|
|
|
|
|
|
|
raise_access_hidden_setting(id)
|
2022-05-23 09:20:51 -04:00
|
|
|
previous_value = value_or_default(SiteSetting.public_send(id))
|
2019-11-17 13:39:38 -05:00
|
|
|
json = {}
|
|
|
|
|
|
|
|
if (user_option = user_options[id.to_sym]).present?
|
|
|
|
if user_option == "text_size_key"
|
|
|
|
previous_value = UserOption.text_sizes[previous_value.to_sym]
|
|
|
|
elsif user_option == "title_count_mode_key"
|
|
|
|
previous_value = UserOption.title_count_modes[previous_value.to_sym]
|
|
|
|
end
|
|
|
|
|
2022-05-23 09:20:51 -04:00
|
|
|
json[:user_count] = UserOption.human_users.where(user_option => previous_value).count
|
2019-11-17 13:39:38 -05:00
|
|
|
elsif id.start_with?("default_categories_")
|
|
|
|
previous_category_ids = previous_value.split("|")
|
|
|
|
new_category_ids = new_value.split("|")
|
|
|
|
|
2022-05-22 23:21:45 -04:00
|
|
|
notification_level = category_notification_level(id)
|
2019-11-17 13:39:38 -05:00
|
|
|
|
|
|
|
user_ids =
|
|
|
|
CategoryUser
|
|
|
|
.where(
|
|
|
|
category_id: previous_category_ids - new_category_ids,
|
|
|
|
notification_level: notification_level,
|
2023-01-09 07:20:10 -05:00
|
|
|
)
|
2019-11-17 13:39:38 -05:00
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
|
|
|
user_ids +=
|
|
|
|
User
|
2020-06-18 14:09:54 -04:00
|
|
|
.real
|
2019-11-17 13:39:38 -05:00
|
|
|
.joins("CROSS JOIN categories c")
|
|
|
|
.joins("LEFT JOIN category_users cu ON users.id = cu.user_id AND c.id = cu.category_id")
|
2020-06-18 14:09:54 -04:00
|
|
|
.where(staged: false)
|
2019-11-17 13:39:38 -05:00
|
|
|
.where(
|
|
|
|
"c.id IN (?) AND cu.notification_level IS NULL",
|
|
|
|
new_category_ids - previous_category_ids,
|
|
|
|
)
|
|
|
|
.distinct
|
|
|
|
.pluck("users.id")
|
|
|
|
|
|
|
|
json[:user_count] = user_ids.uniq.count
|
|
|
|
elsif id.start_with?("default_tags_")
|
|
|
|
previous_tag_ids = Tag.where(name: previous_value.split("|")).pluck(:id)
|
|
|
|
new_tag_ids = Tag.where(name: new_value.split("|")).pluck(:id)
|
|
|
|
|
2022-05-22 23:21:45 -04:00
|
|
|
notification_level = tag_notification_level(id)
|
2019-11-17 13:39:38 -05:00
|
|
|
|
|
|
|
user_ids =
|
|
|
|
TagUser
|
|
|
|
.where(tag_id: previous_tag_ids - new_tag_ids, notification_level: notification_level)
|
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
|
|
|
user_ids +=
|
|
|
|
User
|
2020-06-18 14:09:54 -04:00
|
|
|
.real
|
2019-11-17 13:39:38 -05:00
|
|
|
.joins("CROSS JOIN tags t")
|
|
|
|
.joins("LEFT JOIN tag_users tu ON users.id = tu.user_id AND t.id = tu.tag_id")
|
2020-06-18 14:09:54 -04:00
|
|
|
.where(staged: false)
|
2019-11-17 13:39:38 -05:00
|
|
|
.where("t.id IN (?) AND tu.notification_level IS NULL", new_tag_ids - previous_tag_ids)
|
|
|
|
.distinct
|
|
|
|
.pluck("users.id")
|
|
|
|
|
|
|
|
json[:user_count] = user_ids.uniq.count
|
2022-10-26 18:38:50 -04:00
|
|
|
elsif is_sidebar_default_setting?(id)
|
|
|
|
json[:user_count] = SidebarSiteSettingsBackfiller.new(
|
|
|
|
id,
|
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value,
|
|
|
|
).number_of_users_to_backfill
|
2019-11-17 13:39:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
private
|
|
|
|
|
2022-10-26 18:38:50 -04:00
|
|
|
def is_sidebar_default_setting?(setting_name)
|
2023-06-14 19:31:28 -04:00
|
|
|
%w[default_navigation_menu_categories default_navigation_menu_tags].include?(setting_name.to_s)
|
2022-10-26 18:38:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-17 13:39:38 -05:00
|
|
|
def user_options
|
|
|
|
{
|
|
|
|
default_email_mailing_list_mode: "mailing_list_mode",
|
|
|
|
default_email_mailing_list_mode_frequency: "mailing_list_mode_frequency",
|
|
|
|
default_email_level: "email_level",
|
|
|
|
default_email_messages_level: "email_messages_level",
|
|
|
|
default_topics_automatic_unpin: "automatically_unpin_topics",
|
|
|
|
default_email_previous_replies: "email_previous_replies",
|
|
|
|
default_email_in_reply_to: "email_in_reply_to",
|
|
|
|
default_other_enable_quoting: "enable_quoting",
|
|
|
|
default_other_enable_defer: "enable_defer",
|
|
|
|
default_other_external_links_in_new_tab: "external_links_in_new_tab",
|
|
|
|
default_other_dynamic_favicon: "dynamic_favicon",
|
|
|
|
default_other_new_topic_duration_minutes: "new_topic_duration_minutes",
|
|
|
|
default_other_auto_track_topics_after_msecs: "auto_track_topics_after_msecs",
|
|
|
|
default_other_notification_level_when_replying: "notification_level_when_replying",
|
|
|
|
default_other_like_notification_frequency: "like_notification_frequency",
|
2020-08-14 09:40:56 -04:00
|
|
|
default_other_skip_new_user_tips: "skip_new_user_tips",
|
2019-11-17 13:39:38 -05:00
|
|
|
default_email_digest_frequency: "digest_after_minutes",
|
|
|
|
default_include_tl0_in_digests: "include_tl0_in_digests",
|
|
|
|
default_text_size: "text_size_key",
|
2022-11-06 06:14:17 -05:00
|
|
|
default_title_count_mode: "title_count_mode_key",
|
|
|
|
default_hide_profile_and_presence: "hide_profile_and_presence",
|
2019-11-17 13:39:38 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
def raise_access_hidden_setting(id)
|
2023-05-10 09:21:48 -04:00
|
|
|
id = id.to_sym
|
|
|
|
|
|
|
|
if SiteSetting.hidden_settings.include?(id)
|
2017-08-06 21:43:09 -04:00
|
|
|
raise Discourse::InvalidParameters, "You are not allowed to change hidden settings"
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
2023-05-10 09:21:48 -04:00
|
|
|
|
|
|
|
if SiteSetting.plugins[id] && !Discourse.plugins_by_name[SiteSetting.plugins[id]].configurable?
|
|
|
|
raise Discourse::InvalidParameters, "You are not allowed to change unconfigurable settings"
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2022-05-22 23:21:45 -04:00
|
|
|
def tag_notification_level(id)
|
|
|
|
case id
|
|
|
|
when "default_tags_watching"
|
|
|
|
NotificationLevels.all[:watching]
|
|
|
|
when "default_tags_tracking"
|
|
|
|
NotificationLevels.all[:tracking]
|
|
|
|
when "default_tags_muted"
|
|
|
|
NotificationLevels.all[:muted]
|
|
|
|
when "default_tags_watching_first_post"
|
|
|
|
NotificationLevels.all[:watching_first_post]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_notification_level(id)
|
|
|
|
case id
|
|
|
|
when "default_categories_watching"
|
|
|
|
NotificationLevels.all[:watching]
|
|
|
|
when "default_categories_tracking"
|
|
|
|
NotificationLevels.all[:tracking]
|
|
|
|
when "default_categories_muted"
|
|
|
|
NotificationLevels.all[:muted]
|
|
|
|
when "default_categories_watching_first_post"
|
|
|
|
NotificationLevels.all[:watching_first_post]
|
2022-06-19 23:49:33 -04:00
|
|
|
when "default_categories_normal"
|
2022-05-22 23:21:45 -04:00
|
|
|
NotificationLevels.all[:regular]
|
|
|
|
end
|
|
|
|
end
|
2022-05-23 09:20:51 -04:00
|
|
|
|
|
|
|
def value_or_default(value)
|
|
|
|
value.nil? ? "" : value
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|