discourse/lib/site_setting_extension.rb

475 lines
11 KiB
Ruby
Raw Normal View History

require_dependency 'enum'
require_dependency 'site_settings/db_provider'
require 'site_setting_validations'
2013-02-05 14:16:51 -05:00
module SiteSettingExtension
include SiteSettingValidations
2013-02-05 14:16:51 -05:00
# For plugins, so they can tell if a feature is supported
def supported_types
[:email, :username, :list, :enum]
end
# part 1 of refactor, centralizing the dependency here
def provider=(val)
@provider = val
refresh!
end
def provider
@provider ||= SiteSettings::DbProvider.new(SiteSetting)
end
def types
@types ||= Enum.new(:string, :time, :fixnum, :float, :bool, :null, :enum, :list, :url_list, :host_list, :category_list)
2013-02-05 14:16:51 -05:00
end
def mutex
@mutex ||= Mutex.new
end
def current
@containers ||= {}
@containers[provider.current_site] ||= {}
2013-02-05 14:16:51 -05:00
end
def defaults
2013-02-25 11:42:20 -05:00
@defaults ||= {}
2013-02-05 14:16:51 -05:00
end
2013-11-13 14:02:47 -05:00
def categories
@categories ||= {}
end
def enums
@enums ||= {}
end
2015-03-02 12:12:19 -05:00
def static_types
@static_types ||= {}
2014-03-30 01:32:33 -04:00
end
def choices
@choices ||= {}
end
def shadowed_settings
@shadowed_settings ||= []
end
def hidden_settings
@hidden_settings ||= []
end
def refresh_settings
@refresh_settings ||= []
end
def previews
@previews ||= {}
end
def validators
@validators ||= {}
end
def setting(name_arg, default = nil, opts = {})
2013-11-13 14:02:47 -05:00
name = name_arg.to_sym
2013-02-25 11:42:20 -05:00
mutex.synchronize do
2013-02-05 14:16:51 -05:00
self.defaults[name] = default
categories[name] = opts[:category] || :uncategorized
2013-02-05 14:16:51 -05:00
current_value = current.has_key?(name) ? current[name] : default
2015-03-02 12:12:19 -05:00
if enum = opts[:enum]
enums[name] = enum.is_a?(String) ? enum.constantize : enum
2015-03-02 12:12:19 -05:00
opts[:type] ||= :enum
end
if new_choices = opts[:choices]
if String === new_choices
new_choices = eval(new_choices)
end
choices.has_key?(name) ?
choices[name].concat(new_choices) :
choices[name] = new_choices
end
2015-03-02 12:12:19 -05:00
if type = opts[:type]
static_types[name.to_sym] = type.to_sym
2014-03-30 01:32:33 -04:00
end
2014-03-30 01:32:33 -04:00
if opts[:hidden]
hidden_settings << name
end
# You can "shadow" a site setting with a GlobalSetting. If the GlobalSetting
# exists it will be used instead of the setting and the setting will be hidden.
# Useful for things like API keys on multisite.
if opts[:shadowed_by_global] && GlobalSetting.respond_to?(name)
hidden_settings << name
shadowed_settings << name
current_value = GlobalSetting.send(name)
end
2014-03-30 01:32:33 -04:00
if opts[:refresh]
refresh_settings << name
end
if opts[:preview]
previews[name] = opts[:preview]
end
opts[:validator] = opts[:validator].try(:constantize)
type = opts[:type] || get_data_type(name, defaults[name])
if validator_type = opts[:validator] || validator_for(type)
validators[name] = { class: validator_type, opts: opts }
end
current[name] = current_value
setup_methods(name)
2013-02-05 14:16:51 -05:00
end
end
2013-02-25 11:42:20 -05:00
# just like a setting, except that it is available in javascript via DiscourseSession
def client_setting(name, default = nil, opts = {})
setting(name, default, opts)
@client_settings ||= []
@client_settings << name
2013-02-05 14:16:51 -05:00
end
def client_settings
@client_settings ||= []
2013-02-05 14:16:51 -05:00
end
def settings_hash
result = {}
@defaults.each do |s, _|
result[s] = send(s).to_s
end
result
end
2013-02-05 14:16:51 -05:00
def client_settings_json
Rails.cache.fetch(SiteSettingExtension.client_settings_cache_key, expires_in: 30.minutes) do
client_settings_json_uncached
2013-02-05 14:16:51 -05:00
end
end
def client_settings_json_uncached
MultiJson.dump(Hash[*@client_settings.map{|n| [n, self.send(n)]}.flatten])
end
2013-02-05 14:16:51 -05:00
# Retrieve all settings
def all_settings(include_hidden=false)
@defaults
.reject{|s, _| hidden_settings.include?(s) && !include_hidden}
.map do |s, v|
value = send(s)
type = types[get_data_type(s, value)]
2014-06-05 16:42:26 -04:00
opts = {
setting: s,
description: description(s),
default: v.to_s,
2014-06-05 16:42:26 -04:00
type: type.to_s,
value: value.to_s,
category: categories[s],
preview: previews[s]
}
if type == :enum && enum_class(s)
opts.merge!({valid_values: enum_class(s).values, translate_names: enum_class(s).translate_names?})
elsif type == :enum
opts.merge!({valid_values: choices[s].map{|c| {name: c, value: c}}, translate_names: false})
end
opts[:choices] = choices[s] if choices.has_key? s
opts
end
2013-02-05 14:16:51 -05:00
end
def description(setting)
I18n.t("site_settings.#{setting}")
end
def self.client_settings_cache_key
# NOTE: we use the git version in the key to ensure
# that we don't end up caching the incorrect version
# in cases where we are cycling unicorns
"client_settings_json_#{Discourse.git_version}"
2013-02-05 14:16:51 -05:00
end
# refresh all the site settings
2013-02-25 11:42:20 -05:00
def refresh!
mutex.synchronize do
2013-02-05 14:16:51 -05:00
ensure_listen_for_changes
old = current
new_hash = Hash[*(provider.all.map{ |s|
[s.name.intern, convert(s.value,s.data_type)]
}.to_a.flatten)]
2013-02-05 14:16:51 -05:00
# add defaults, cause they are cached
2013-02-05 14:16:51 -05:00
new_hash = defaults.merge(new_hash)
# add shadowed
shadowed_settings.each do |ss|
new_hash[ss] = GlobalSetting.send(ss)
2013-02-05 14:16:51 -05:00
end
changes, deletions = diff_hash(new_hash, old)
changes.each do |name, val|
current[name] = val
end
deletions.each do |name, val|
current[name] = defaults[name]
end
clear_cache!
2013-02-05 14:16:51 -05:00
end
end
def ensure_listen_for_changes
unless @subscribed
MessageBus.subscribe("/site_settings") do |message|
2013-06-12 22:41:27 -04:00
process_message(message)
2013-02-05 14:16:51 -05:00
end
@subscribed = true
end
end
2013-06-12 22:41:27 -04:00
def process_message(message)
data = message.data
if data["process"] != process_id
begin
@last_message_processed = message.global_id
MessageBus.on_connect.call(message.site_id)
refresh!
2013-06-12 22:41:27 -04:00
ensure
MessageBus.on_disconnect.call(message.site_id)
2013-06-12 22:41:27 -04:00
end
end
end
def diags
{
last_message_processed: @last_message_processed
}
end
2013-02-05 14:16:51 -05:00
def process_id
@process_id ||= SecureRandom.uuid
2013-02-05 14:16:51 -05:00
end
def after_fork
@process_id = nil
ensure_listen_for_changes
end
2013-02-05 14:16:51 -05:00
def remove_override!(name)
provider.destroy(name)
current[name] = defaults[name]
clear_cache!
2013-02-05 14:16:51 -05:00
end
2015-03-02 12:12:19 -05:00
def add_override!(name, val)
type = get_data_type(name, defaults[name])
2013-02-25 11:42:20 -05:00
if type == types[:bool] && val != true && val != false
val = (val == "t" || val == "true") ? 't' : 'f'
2013-02-05 14:16:51 -05:00
end
2014-03-26 15:20:41 -04:00
if type == types[:fixnum] && !val.is_a?(Fixnum)
2013-02-05 14:16:51 -05:00
val = val.to_i
end
if type == types[:null] && val != ''
type = get_data_type(name, val)
end
if type == types[:enum]
val = val.to_i if Fixnum === defaults[name.to_sym]
if enum_class(name)
raise Discourse::InvalidParameters.new(:value) unless enum_class(name).valid_value?(val)
else
raise Discourse::InvalidParameters.new(:value) unless choices[name].include?(val)
end
end
if v = validators[name]
validator = v[:class].new(v[:opts])
unless validator.valid_value?(val)
raise Discourse::InvalidParameters.new(validator.error_message)
end
end
if self.respond_to? "validate_#{name}"
send("validate_#{name}", val)
end
provider.save(name, val, type)
current[name] = convert(val, type)
clear_cache!
end
def notify_changed!
MessageBus.publish('/site_settings', {process: process_id})
2013-02-05 14:16:51 -05:00
end
2014-01-27 13:05:35 -05:00
def has_setting?(name)
defaults.has_key?(name.to_sym) || defaults.has_key?("#{name}?".to_sym)
end
def requires_refresh?(name)
refresh_settings.include?(name.to_sym)
end
2015-04-23 08:40:12 -04:00
def is_valid_data?(name, value)
valid = true
type = get_data_type(name, defaults[name.to_sym])
if type == types[:fixnum]
# validate fixnum
valid = false unless value.to_i.is_a?(Fixnum)
2015-04-23 08:40:12 -04:00
end
valid
2015-04-23 08:40:12 -04:00
end
2014-07-24 08:00:15 -04:00
def filter_value(name, value)
# filter domain name
if %w[disabled_image_download_domains onebox_domains_whitelist exclude_rel_nofollow_domains email_domains_blacklist email_domains_whitelist white_listed_spam_host_domains].include? name
domain_array = []
value.split('|').each { |url|
domain_array.push(get_hostname(url))
}
value = domain_array.join("|")
end
value
2014-07-24 08:00:15 -04:00
end
2014-01-27 13:05:35 -05:00
def set(name, value)
2015-04-23 08:40:12 -04:00
if has_setting?(name) && is_valid_data?(name, value)
2014-07-24 08:00:15 -04:00
value = filter_value(name, value)
2014-01-27 13:05:35 -05:00
self.send("#{name}=", value)
Discourse.request_refresh! if requires_refresh?(name)
2014-01-27 13:05:35 -05:00
else
2015-04-23 08:40:12 -04:00
raise ArgumentError.new("Either no setting named '#{name}' exists or value provided is invalid")
2014-01-27 13:05:35 -05:00
end
end
2013-02-25 11:42:20 -05:00
protected
2013-02-05 14:16:51 -05:00
def clear_cache!
SiteText.text_for_cache.clear
Rails.cache.delete(SiteSettingExtension.client_settings_cache_key)
end
2013-06-12 22:41:27 -04:00
def diff_hash(new_hash, old)
changes = []
deletions = []
new_hash.each do |name, value|
changes << [name,value] if !old.has_key?(name) || old[name] != value
end
old.each do |name,value|
deletions << [name,value] unless new_hash.has_key?(name)
end
[changes,deletions]
end
2015-03-02 12:12:19 -05:00
def get_data_type(name, val)
return types[:null] if val.nil?
2015-03-02 12:12:19 -05:00
# Some types are just for validations like email. Only consider
# it valid if includes in `types`
if static_type = static_types[name.to_sym]
return types[static_type] if types.keys.include?(static_type)
end
2013-02-05 14:16:51 -05:00
2013-06-12 22:41:27 -04:00
case val
when String
types[:string]
2013-06-12 22:41:27 -04:00
when Fixnum
types[:fixnum]
when Float
types[:float]
2013-06-12 22:41:27 -04:00
when TrueClass, FalseClass
types[:bool]
2013-02-25 11:42:20 -05:00
else
2013-02-05 14:16:51 -05:00
raise ArgumentError.new :val
end
end
def convert(value, type)
2013-02-25 11:42:20 -05:00
case type
when types[:float]
value.to_f
when types[:fixnum]
2013-02-05 14:16:51 -05:00
value.to_i
when types[:bool]
value == true || value == "t" || value == "true"
when types[:null]
2013-02-05 14:16:51 -05:00
nil
2014-03-30 01:32:33 -04:00
else
2015-03-02 12:12:19 -05:00
return value if types[type]
# Otherwise it's a type error
2014-03-30 01:32:33 -04:00
raise ArgumentError.new :type
2013-02-05 14:16:51 -05:00
end
end
def validator_for(type_name)
@validator_mapping ||= {
'email' => EmailSettingValidator,
'username' => UsernameSettingValidator,
types[:fixnum] => IntegerSettingValidator,
types[:string] => StringSettingValidator,
'list' => StringSettingValidator,
'enum' => StringSettingValidator
}
@validator_mapping[type_name]
end
2013-02-05 14:16:51 -05:00
def setup_methods(name)
2015-02-11 23:07:17 -05:00
clean_name = name.to_s.sub("?", "").to_sym
2013-02-05 14:16:51 -05:00
define_singleton_method clean_name do
c = @containers[provider.current_site]
if c
c[name]
else
refresh!
current[name]
end
end
2013-02-05 14:16:51 -05:00
2015-02-11 23:07:17 -05:00
define_singleton_method "#{clean_name}?" do
self.send clean_name
2013-02-05 14:16:51 -05:00
end
2013-02-25 11:42:20 -05:00
2015-02-11 23:07:17 -05:00
define_singleton_method "#{clean_name}=" do |val|
add_override!(name, val)
2013-02-05 14:16:51 -05:00
end
end
def enum_class(name)
enums[name]
end
2013-02-05 14:16:51 -05:00
2014-07-24 08:00:15 -04:00
def get_hostname(url)
unless (URI.parse(url).scheme rescue nil).nil?
url = "http://#{url}" if URI.parse(url).scheme.nil?
url = URI.parse(url).host
end
url
2014-07-24 08:00:15 -04:00
end
2013-02-05 14:16:51 -05:00
end