2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'fileutils'
|
|
|
|
require_dependency 'plugin/metadata'
|
2018-08-06 21:04:29 -04:00
|
|
|
require_dependency 'auth'
|
2013-08-23 02:21:52 -04:00
|
|
|
|
2016-07-22 12:59:43 -04:00
|
|
|
class Plugin::CustomEmoji
|
2020-03-30 14:16:10 -04:00
|
|
|
CACHE_KEY ||= "plugin-emoji"
|
2016-07-22 12:59:43 -04:00
|
|
|
def self.cache_key
|
2020-03-30 14:16:10 -04:00
|
|
|
@@cache_key ||= CACHE_KEY
|
2016-07-22 12:59:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.emojis
|
|
|
|
@@emojis ||= {}
|
|
|
|
end
|
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
def self.clear_cache
|
|
|
|
@@cache_key = CACHE_KEY
|
|
|
|
@@emojis = {}
|
2020-05-27 14:11:52 -04:00
|
|
|
@@translations = {}
|
2016-07-22 12:59:43 -04:00
|
|
|
end
|
2019-01-04 09:14:16 -05:00
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
def self.register(name, url, group = Emoji::DEFAULT_GROUP)
|
|
|
|
@@cache_key = Digest::SHA1.hexdigest(cache_key + name + group)[0..10]
|
|
|
|
new_group = emojis[group] || {}
|
|
|
|
new_group[name] = url
|
|
|
|
emojis[group] = new_group
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unregister(name, group = Emoji::DEFAULT_GROUP)
|
|
|
|
emojis[group].delete(name)
|
2019-07-03 03:23:40 -04:00
|
|
|
end
|
2020-05-27 06:08:24 -04:00
|
|
|
|
|
|
|
def self.translations
|
|
|
|
@@translations ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.translate(from, to)
|
|
|
|
@@cache_key = Digest::SHA1.hexdigest(cache_key + from)[0..10]
|
|
|
|
translations[from] = to
|
|
|
|
end
|
2016-07-22 12:59:43 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
class Plugin::Instance
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
attr_accessor :path, :metadata
|
2015-02-06 17:32:59 -05:00
|
|
|
attr_reader :admin_route
|
2013-08-23 02:21:52 -04:00
|
|
|
|
2015-02-04 12:59:18 -05:00
|
|
|
# Memoized array readers
|
2017-01-12 15:43:09 -05:00
|
|
|
[:assets,
|
|
|
|
:color_schemes,
|
2018-11-30 11:58:18 -05:00
|
|
|
:before_auth_initializers,
|
2017-01-12 15:43:09 -05:00
|
|
|
:initializers,
|
|
|
|
:javascripts,
|
2018-01-25 06:09:18 -05:00
|
|
|
:locales,
|
2017-11-22 20:02:01 -05:00
|
|
|
:service_workers,
|
2017-01-12 15:43:09 -05:00
|
|
|
:styles,
|
2018-11-30 09:51:45 -05:00
|
|
|
:themes,
|
|
|
|
:csp_extensions,
|
2020-03-13 11:30:31 -04:00
|
|
|
:asset_filters
|
2018-11-30 09:51:45 -05:00
|
|
|
].each do |att|
|
2015-02-04 12:59:18 -05:00
|
|
|
class_eval %Q{
|
|
|
|
def #{att}
|
|
|
|
@#{att} ||= []
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-04-13 15:05:46 -04:00
|
|
|
# If plugins provide `transpile_js: true` in their metadata we will
|
|
|
|
# transpile regular JS files in the assets folders. Going forward,
|
|
|
|
# all plugins should do this.
|
|
|
|
def transpile_js
|
|
|
|
metadata.try(:transpile_js) == "true"
|
|
|
|
end
|
|
|
|
|
2016-07-22 12:59:43 -04:00
|
|
|
def seed_data
|
|
|
|
@seed_data ||= HashWithIndifferentAccess.new({})
|
2015-06-04 15:56:17 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
def self.find_all(parent_path)
|
|
|
|
[].tap { |plugins|
|
2013-10-21 05:18:24 -04:00
|
|
|
# also follows symlinks - http://stackoverflow.com/q/357754
|
2017-05-16 17:28:45 -04:00
|
|
|
Dir["#{parent_path}/*/plugin.rb"].sort.each do |path|
|
2016-04-25 15:55:15 -04:00
|
|
|
|
|
|
|
# tagging is included in core, so don't load it
|
|
|
|
next if path =~ /discourse-tagging/
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
source = File.read(path)
|
|
|
|
metadata = Plugin::Metadata.parse(source)
|
|
|
|
plugins << self.new(metadata, path)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(metadata = nil, path = nil)
|
2013-08-23 02:21:52 -04:00
|
|
|
@metadata = metadata
|
|
|
|
@path = path
|
2015-08-21 11:28:17 -04:00
|
|
|
@idx = 0
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2019-12-05 14:57:18 -05:00
|
|
|
def register_anonymous_cache_key(key, &block)
|
|
|
|
key_method = "key_#{key}"
|
2019-12-11 09:07:22 -05:00
|
|
|
add_to_class(Middleware::AnonymousCache::Helper, key_method, &block)
|
2019-12-05 14:57:18 -05:00
|
|
|
Middleware::AnonymousCache.cache_key_segments[key] = key_method
|
|
|
|
Middleware::AnonymousCache.compile_key_builder
|
|
|
|
end
|
|
|
|
|
2015-02-06 17:32:59 -05:00
|
|
|
def add_admin_route(label, location)
|
2017-07-27 21:20:09 -04:00
|
|
|
@admin_route = { label: label, location: location }
|
2015-02-06 17:32:59 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
def enabled?
|
2019-05-06 21:00:09 -04:00
|
|
|
@enabled_site_setting ? SiteSetting.get(@enabled_site_setting) : true
|
2013-08-25 21:04:16 -04:00
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
delegate :name, to: :metadata
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def add_to_serializer(serializer, attr, define_include_method = true, &block)
|
2017-08-09 16:22:18 -04:00
|
|
|
reloadable_patch do |plugin|
|
2019-08-27 04:21:53 -04:00
|
|
|
base = "#{serializer.to_s.classify}Serializer".constantize rescue "#{serializer.to_s}Serializer".constantize
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
# we have to work through descendants cause serializers may already be baked and cached
|
|
|
|
([base] + base.descendants).each do |klass|
|
|
|
|
unless attr.to_s.start_with?("include_")
|
|
|
|
klass.attributes(attr)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
if define_include_method
|
|
|
|
# Don't include serialized methods if the plugin is disabled
|
|
|
|
klass.public_send(:define_method, "include_#{attr}?") { plugin.enabled? }
|
|
|
|
end
|
2017-08-09 16:22:18 -04:00
|
|
|
end
|
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
klass.public_send(:define_method, attr, &block)
|
|
|
|
end
|
2017-08-09 12:28:32 -04:00
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2018-06-19 09:00:11 -04:00
|
|
|
def add_report(name, &block)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
Report.add_report(name, &block)
|
2018-06-19 09:00:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2019-12-27 06:41:50 -05:00
|
|
|
def replace_flags(settings: ::FlagSettings.new)
|
2020-01-17 09:59:38 -05:00
|
|
|
next_flag_id = ReviewableScore.types.values.max + 1
|
|
|
|
|
|
|
|
yield(settings, next_flag_id)
|
2017-10-17 13:31:45 -04:00
|
|
|
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::PostActionType.replace_flag_settings(settings)
|
2020-01-17 09:59:38 -05:00
|
|
|
::ReviewableScore.reload_types
|
2017-10-17 13:31:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 15:52:18 -05:00
|
|
|
def whitelist_staff_user_custom_field(field)
|
2020-05-15 09:04:38 -04:00
|
|
|
DiscoursePluginRegistry.register_staff_user_custom_field(field, self)
|
2016-03-11 15:52:18 -05:00
|
|
|
end
|
|
|
|
|
2018-10-17 05:33:27 -04:00
|
|
|
def whitelist_public_user_custom_field(field)
|
2020-05-15 09:04:38 -04:00
|
|
|
DiscoursePluginRegistry.register_public_user_custom_field(field, self)
|
2018-10-17 05:33:27 -04:00
|
|
|
end
|
|
|
|
|
2019-10-11 04:57:55 -04:00
|
|
|
def register_editable_user_custom_field(field, staff_only: false)
|
2020-05-15 09:04:38 -04:00
|
|
|
if staff_only
|
|
|
|
DiscoursePluginRegistry.register_staff_editable_user_custom_field(field, self)
|
|
|
|
else
|
|
|
|
DiscoursePluginRegistry.register_self_editable_user_custom_field(field, self)
|
2018-09-04 06:45:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-05 22:05:33 -04:00
|
|
|
def register_editable_group_custom_field(field)
|
2020-05-15 09:04:38 -04:00
|
|
|
DiscoursePluginRegistry.register_editable_group_custom_field(field, self)
|
2019-06-05 22:05:33 -04:00
|
|
|
end
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
# Request a new size for topic thumbnails
|
|
|
|
# Will respect plugin enabled setting is enabled
|
|
|
|
# Size should be an array with two elements [max_width, max_height]
|
|
|
|
def register_topic_thumbnail_size(size)
|
|
|
|
if !(size.kind_of?(Array) && size.length == 2)
|
|
|
|
raise ArgumentError.new("Topic thumbnail dimension is not valid")
|
|
|
|
end
|
|
|
|
DiscoursePluginRegistry.register_topic_thumbnail_size(size, self)
|
|
|
|
end
|
|
|
|
|
2017-08-30 14:24:03 -04:00
|
|
|
def custom_avatar_column(column)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
AvatarLookup.lookup_columns << column
|
|
|
|
AvatarLookup.lookup_columns.uniq!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-09-28 13:16:51 -04:00
|
|
|
def add_body_class(class_name)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::ApplicationHelper.extra_body_classes << class_name
|
2017-09-28 13:16:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-02 12:04:59 -04:00
|
|
|
def rescue_from(exception, &block)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
::ApplicationController.rescue_from(exception, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
# Extend a class but check that the plugin is enabled
|
2015-08-21 11:28:17 -04:00
|
|
|
# for class methods use `add_class_method`
|
2017-08-09 12:28:32 -04:00
|
|
|
def add_to_class(class_name, attr, &block)
|
2017-08-09 16:22:18 -04:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 12:28:32 -04:00
|
|
|
klass = class_name.to_s.classify.constantize rescue class_name.to_s.constantize
|
|
|
|
hidden_method_name = :"#{attr}_without_enable_check"
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_method, hidden_method_name, &block)
|
2017-08-09 12:28:32 -04:00
|
|
|
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_method, attr) do |*args|
|
|
|
|
public_send(hidden_method_name, *args) if plugin.enabled?
|
2017-08-09 12:28:32 -04:00
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
2015-01-12 10:52:55 -05:00
|
|
|
end
|
|
|
|
|
2015-08-21 11:28:17 -04:00
|
|
|
# Adds a class method to a class, respecting if plugin is enabled
|
2017-08-09 12:28:32 -04:00
|
|
|
def add_class_method(klass_name, attr, &block)
|
2017-08-09 16:22:18 -04:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 12:28:32 -04:00
|
|
|
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
|
2015-08-21 11:28:17 -04:00
|
|
|
|
2017-08-09 12:28:32 -04:00
|
|
|
hidden_method_name = :"#{attr}_without_enable_check"
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_singleton_method, hidden_method_name, &block)
|
2015-08-21 11:28:17 -04:00
|
|
|
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_singleton_method, attr) do |*args|
|
|
|
|
public_send(hidden_method_name, *args) if plugin.enabled?
|
2017-08-09 12:28:32 -04:00
|
|
|
end
|
2015-08-21 11:28:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 12:28:32 -04:00
|
|
|
def add_model_callback(klass_name, callback, options = {}, &block)
|
2017-08-09 16:22:18 -04:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 12:28:32 -04:00
|
|
|
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
|
2015-08-21 11:28:17 -04:00
|
|
|
|
2017-08-09 12:28:32 -04:00
|
|
|
# generate a unique method name
|
|
|
|
method_name = "#{plugin.name}_#{klass.name}_#{callback}#{@idx}".underscore
|
|
|
|
@idx += 1
|
|
|
|
hidden_method_name = :"#{method_name}_without_enable_check"
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_method, hidden_method_name, &block)
|
2015-08-21 11:28:17 -04:00
|
|
|
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(callback, options) do |*args|
|
|
|
|
public_send(hidden_method_name, *args) if plugin.enabled?
|
2017-08-09 12:28:32 -04:00
|
|
|
end
|
2015-08-21 11:28:17 -04:00
|
|
|
|
2017-08-09 12:28:32 -04:00
|
|
|
hidden_method_name
|
|
|
|
end
|
2015-08-21 11:28:17 -04:00
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Add a post_custom_fields_whitelister block to the TopicView, respecting if the plugin is enabled
|
2017-08-11 22:21:02 -04:00
|
|
|
def topic_view_post_custom_fields_whitelister(&block)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::TopicView.add_post_custom_fields_whitelister do |user|
|
2018-07-26 10:52:39 -04:00
|
|
|
plugin.enabled? ? block.call(user) : []
|
2018-07-25 11:44:09 -04:00
|
|
|
end
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-11 22:21:02 -04:00
|
|
|
def add_preloaded_group_custom_field(field)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::Group.preloaded_custom_field_names << field
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-11 22:21:02 -04:00
|
|
|
def add_preloaded_topic_list_custom_field(field)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::TopicList.preloaded_custom_fields << field
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Add a permitted_create_param to Post, respecting if the plugin is enabled
|
2019-12-20 11:37:12 -05:00
|
|
|
def add_permitted_post_create_param(name, type = :string)
|
2017-08-11 22:21:02 -04:00
|
|
|
reloadable_patch do |plugin|
|
2019-12-20 11:37:12 -05:00
|
|
|
::Post.plugin_permitted_create_params[name] = { plugin: plugin, type: type }
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
# Add validation method but check that the plugin is enabled
|
2015-04-25 18:12:19 -04:00
|
|
|
def validate(klass, name, &block)
|
2015-04-23 13:33:29 -04:00
|
|
|
klass = klass.to_s.classify.constantize
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_method, name, &block)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
plugin = self
|
2015-04-25 18:12:19 -04:00
|
|
|
klass.validate(name, if: -> { plugin.enabled? })
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
# will make sure all the assets this plugin needs are registered
|
|
|
|
def generate_automatic_assets!
|
|
|
|
paths = []
|
2015-11-06 09:02:40 -05:00
|
|
|
assets = []
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
automatic_assets.each do |path, contents|
|
2015-11-06 09:02:40 -05:00
|
|
|
write_asset(path, contents)
|
|
|
|
paths << path
|
2019-09-16 12:06:34 -04:00
|
|
|
assets << [path, nil, directory_name]
|
2015-11-06 09:02:40 -05:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
delete_extra_automatic_assets(paths)
|
|
|
|
|
2015-11-06 09:02:40 -05:00
|
|
|
assets
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_extra_automatic_assets(good_paths)
|
2013-09-20 17:39:14 -04:00
|
|
|
return unless Dir.exists? auto_generated_path
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
filenames = good_paths.map { |f| File.basename(f) }
|
2013-08-23 02:21:52 -04:00
|
|
|
# nuke old files
|
|
|
|
Dir.foreach(auto_generated_path) do |p|
|
|
|
|
next if [".", ".."].include?(p)
|
|
|
|
next if filenames.include?(p)
|
|
|
|
File.delete(auto_generated_path + "/#{p}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_directory(path)
|
|
|
|
dirname = File.dirname(path)
|
|
|
|
unless File.directory?(dirname)
|
|
|
|
FileUtils.mkdir_p(dirname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 15:43:09 -05:00
|
|
|
def directory
|
|
|
|
File.dirname(path)
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
def auto_generated_path
|
|
|
|
File.dirname(path) << "/auto_generated"
|
|
|
|
end
|
|
|
|
|
2013-09-16 20:23:21 -04:00
|
|
|
def after_initialize(&block)
|
2015-02-04 12:59:18 -05:00
|
|
|
initializers << block
|
2013-09-16 20:23:21 -04:00
|
|
|
end
|
|
|
|
|
2018-11-30 11:58:18 -05:00
|
|
|
def before_auth(&block)
|
|
|
|
raise "Auth providers must be registered before omniauth middleware. after_initialize is too late!" if @before_auth_complete
|
|
|
|
before_auth_initializers << block
|
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
# A proxy to `DiscourseEvent.on` which does nothing if the plugin is disabled
|
|
|
|
def on(event_name, &block)
|
|
|
|
DiscourseEvent.on(event_name) do |*args|
|
|
|
|
block.call(*args) if enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-16 20:23:21 -04:00
|
|
|
def notify_after_initialize
|
2014-06-03 12:36:34 -04:00
|
|
|
color_schemes.each do |c|
|
2017-08-11 22:21:02 -04:00
|
|
|
unless ColorScheme.where(name: c[:name]).exists?
|
|
|
|
ColorScheme.create_from_base(name: c[:name], colors: c[:colors])
|
|
|
|
end
|
2014-06-03 12:36:34 -04:00
|
|
|
end
|
|
|
|
|
2015-02-04 12:59:18 -05:00
|
|
|
initializers.each do |callback|
|
2015-08-25 16:38:25 -04:00
|
|
|
begin
|
|
|
|
callback.call(self)
|
|
|
|
rescue ActiveRecord::StatementInvalid => e
|
2017-08-11 22:21:02 -04:00
|
|
|
# When running `db:migrate` for the first time on a new database,
|
|
|
|
# plugin initializers might try to use models.
|
|
|
|
# Tolerate it.
|
2015-08-25 16:38:25 -04:00
|
|
|
raise e unless e.message.try(:include?, "PG::UndefinedTable")
|
|
|
|
end
|
2013-09-16 20:23:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-30 11:58:18 -05:00
|
|
|
def notify_before_auth
|
|
|
|
before_auth_initializers.each do |callback|
|
|
|
|
callback.call(self)
|
|
|
|
end
|
|
|
|
@before_auth_complete = true
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-17 02:59:31 -04:00
|
|
|
def register_category_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
Category.register_custom_field_type(name, type)
|
2017-08-17 02:59:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-11 22:21:02 -04:00
|
|
|
def register_topic_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::Topic.register_custom_field_type(name, type)
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-11 22:21:02 -04:00
|
|
|
def register_post_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::Post.register_custom_field_type(name, type)
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 11:44:09 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
2017-08-11 22:21:02 -04:00
|
|
|
def register_group_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2018-07-25 11:44:09 -04:00
|
|
|
::Group.register_custom_field_type(name, type)
|
2017-08-11 22:21:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-24 12:38:44 -04:00
|
|
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
|
|
|
def register_user_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
::User.register_custom_field_type(name, type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-25 02:55:53 -04:00
|
|
|
def register_seedfu_fixtures(paths)
|
|
|
|
paths = [paths] if !paths.kind_of?(Array)
|
|
|
|
SeedFu.fixture_paths.concat(paths)
|
|
|
|
end
|
|
|
|
|
2014-12-11 11:08:47 -05:00
|
|
|
def listen_for(event_name)
|
|
|
|
return unless self.respond_to?(event_name)
|
|
|
|
DiscourseEvent.on(event_name, &self.method(event_name))
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
def register_css(style)
|
2015-02-04 12:59:18 -05:00
|
|
|
styles << style
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_javascript(js)
|
2015-02-04 12:59:18 -05:00
|
|
|
javascripts << js
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2018-11-26 16:49:57 -05:00
|
|
|
def register_svg_icon(icon)
|
|
|
|
DiscoursePluginRegistry.register_svg_icon(icon)
|
|
|
|
end
|
|
|
|
|
2018-11-30 09:51:45 -05:00
|
|
|
def extend_content_security_policy(extension)
|
|
|
|
csp_extensions << extension
|
|
|
|
end
|
|
|
|
|
2020-03-13 11:30:31 -04:00
|
|
|
# Register a block to run when adding css and js assets
|
|
|
|
# Two arguments will be passed: (type, request)
|
|
|
|
# Type is :css or :js. `request` is an instance of Rack::Request
|
|
|
|
# When using this, make sure to consider the effect on AnonymousCache
|
|
|
|
def register_asset_filter(&blk)
|
|
|
|
asset_filters << blk
|
|
|
|
end
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
# @option opts [String] :name
|
|
|
|
# @option opts [String] :nativeName
|
|
|
|
# @option opts [String] :fallbackLocale
|
|
|
|
# @option opts [Hash] :plural
|
|
|
|
def register_locale(locale, opts = {})
|
|
|
|
locales << [locale, opts]
|
|
|
|
end
|
|
|
|
|
2014-06-04 21:39:33 -04:00
|
|
|
def register_custom_html(hash)
|
|
|
|
DiscoursePluginRegistry.custom_html.merge!(hash)
|
|
|
|
end
|
|
|
|
|
2017-04-17 15:47:21 -04:00
|
|
|
def register_html_builder(name, &block)
|
2019-12-04 12:26:23 -05:00
|
|
|
plugin = self
|
|
|
|
DiscoursePluginRegistry.register_html_builder(name) do |*args|
|
|
|
|
block.call(*args) if plugin.enabled?
|
|
|
|
end
|
2017-04-17 15:47:21 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def register_asset(file, opts = nil)
|
2018-04-10 02:37:16 -04:00
|
|
|
if opts && opts == :vendored_core_pretty_text
|
|
|
|
full_path = DiscoursePluginRegistry.core_asset_for_name(file)
|
|
|
|
else
|
|
|
|
full_path = File.dirname(path) << "/assets/" << file
|
|
|
|
end
|
|
|
|
|
2019-08-20 12:39:52 -04:00
|
|
|
assets << [full_path, opts, directory_name]
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2017-11-22 20:02:01 -05:00
|
|
|
def register_service_worker(file, opts = nil)
|
|
|
|
service_workers << [
|
|
|
|
File.join(File.dirname(path), 'assets', file),
|
|
|
|
opts
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2014-06-03 12:36:34 -04:00
|
|
|
def register_color_scheme(name, colors)
|
2017-07-27 21:20:09 -04:00
|
|
|
color_schemes << { name: name, colors: colors }
|
2015-06-04 15:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_seed_data(key, value)
|
|
|
|
seed_data[key] = value
|
|
|
|
end
|
2014-06-03 12:36:34 -04:00
|
|
|
|
2017-11-16 14:42:38 -05:00
|
|
|
def register_seed_path_builder(&block)
|
|
|
|
DiscoursePluginRegistry.register_seed_path_builder(&block)
|
|
|
|
end
|
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
def register_emoji(name, url, group = Emoji::DEFAULT_GROUP)
|
|
|
|
Plugin::CustomEmoji.register(name, url, group)
|
|
|
|
Emoji.clear_cache
|
2015-11-05 11:25:26 -05:00
|
|
|
end
|
|
|
|
|
2020-05-27 06:08:24 -04:00
|
|
|
def translate_emoji(from, to)
|
|
|
|
Plugin::CustomEmoji.translate(from, to)
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
def automatic_assets
|
2015-02-04 12:59:18 -05:00
|
|
|
css = styles.join("\n")
|
|
|
|
js = javascripts.join("\n")
|
2013-08-23 02:21:52 -04:00
|
|
|
|
2015-11-06 09:02:40 -05:00
|
|
|
# Generate an IIFE for the JS
|
|
|
|
js = "(function(){#{js}})();" if js.present?
|
|
|
|
|
|
|
|
result = []
|
|
|
|
result << [css, 'css'] if css.present?
|
|
|
|
result << [js, 'js'] if js.present?
|
|
|
|
|
|
|
|
result.map do |asset, extension|
|
|
|
|
hash = Digest::SHA1.hexdigest asset
|
|
|
|
["#{auto_generated_path}/plugin_#{hash}.#{extension}", asset]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
# note, we need to be able to parse seperately to activation.
|
|
|
|
# this allows us to present information about a plugin in the UI
|
|
|
|
# prior to activations
|
|
|
|
def activate!
|
2015-04-27 13:06:53 -04:00
|
|
|
|
|
|
|
if @path
|
2020-04-13 15:05:46 -04:00
|
|
|
root_dir_name = File.dirname(@path)
|
|
|
|
|
2015-04-27 13:06:53 -04:00
|
|
|
# Automatically include all ES6 JS and hbs files
|
2020-04-13 15:05:46 -04:00
|
|
|
root_path = "#{root_dir_name}/assets/javascripts"
|
2015-04-27 13:06:53 -04:00
|
|
|
DiscoursePluginRegistry.register_glob(root_path, 'js.es6')
|
|
|
|
DiscoursePluginRegistry.register_glob(root_path, 'hbs')
|
2020-02-11 14:38:12 -05:00
|
|
|
DiscoursePluginRegistry.register_glob(root_path, 'hbr')
|
2015-08-17 15:03:55 -04:00
|
|
|
|
2020-04-13 15:05:46 -04:00
|
|
|
admin_path = "#{root_dir_name}/admin/assets/javascripts"
|
2015-08-17 15:03:55 -04:00
|
|
|
DiscoursePluginRegistry.register_glob(admin_path, 'js.es6', admin: true)
|
|
|
|
DiscoursePluginRegistry.register_glob(admin_path, 'hbs', admin: true)
|
2020-02-11 14:38:12 -05:00
|
|
|
DiscoursePluginRegistry.register_glob(admin_path, 'hbr', admin: true)
|
2020-04-13 15:05:46 -04:00
|
|
|
|
|
|
|
if transpile_js
|
|
|
|
DiscourseJsProcessor.plugin_transpile_paths << root_path.sub(Rails.root.to_s, '').sub(/^\/*/, '')
|
2020-05-08 11:10:34 -04:00
|
|
|
DiscourseJsProcessor.plugin_transpile_paths << admin_path.sub(Rails.root.to_s, '').sub(/^\/*/, '')
|
2020-04-13 15:05:46 -04:00
|
|
|
end
|
2015-04-27 13:06:53 -04:00
|
|
|
end
|
|
|
|
|
2013-09-11 21:27:13 -04:00
|
|
|
self.instance_eval File.read(path), path
|
2013-08-23 02:21:52 -04:00
|
|
|
if auto_assets = generate_automatic_assets!
|
2015-11-06 09:02:40 -05:00
|
|
|
assets.concat(auto_assets)
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
2014-12-30 16:29:28 -05:00
|
|
|
|
|
|
|
register_assets! unless assets.blank?
|
2018-01-25 06:09:18 -05:00
|
|
|
register_locales!
|
2017-11-22 20:02:01 -05:00
|
|
|
register_service_workers!
|
|
|
|
|
2015-06-04 15:56:17 -04:00
|
|
|
seed_data.each do |key, value|
|
|
|
|
DiscoursePluginRegistry.register_seed_data(key, value)
|
|
|
|
end
|
|
|
|
|
2015-05-04 10:01:57 -04:00
|
|
|
# TODO: possibly amend this to a rails engine
|
|
|
|
|
|
|
|
# Automatically include assets
|
2014-12-30 16:29:28 -05:00
|
|
|
Rails.configuration.assets.paths << auto_generated_path
|
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/assets"
|
2015-08-17 15:03:55 -04:00
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/admin/assets"
|
2015-08-27 16:59:36 -04:00
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/test/javascripts"
|
2013-08-23 02:21:52 -04:00
|
|
|
|
2015-05-04 10:01:57 -04:00
|
|
|
# Automatically include rake tasks
|
|
|
|
Rake.add_rakelib(File.dirname(path) + "/lib/tasks")
|
|
|
|
|
|
|
|
# Automatically include migrations
|
2019-12-16 14:11:55 -05:00
|
|
|
migration_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
2018-10-09 01:11:45 -04:00
|
|
|
migration_paths << File.dirname(path) + "/db/migrate"
|
|
|
|
|
|
|
|
unless Discourse.skip_post_deployment_migrations?
|
|
|
|
migration_paths << "#{File.dirname(path)}/#{Discourse::DB_POST_MIGRATE_PATH}"
|
|
|
|
end
|
2015-05-04 10:01:57 -04:00
|
|
|
|
2013-11-19 22:38:21 -05:00
|
|
|
public_data = File.dirname(path) + "/public"
|
|
|
|
if Dir.exists?(public_data)
|
|
|
|
target = Rails.root.to_s + "/public/plugins/"
|
2017-03-17 02:21:30 -04:00
|
|
|
|
|
|
|
Discourse::Utils.execute_command('mkdir', '-p', target)
|
2017-07-27 21:20:09 -04:00
|
|
|
target << name.gsub(/\s/, "_")
|
2020-03-04 12:28:26 -05:00
|
|
|
|
|
|
|
Discourse::Utils.atomic_ln_s(public_data, target)
|
2013-11-19 22:38:21 -05:00
|
|
|
end
|
2019-07-15 10:52:54 -04:00
|
|
|
|
2020-03-04 12:28:26 -05:00
|
|
|
ensure_directory(js_file_path)
|
2019-07-15 10:52:54 -04:00
|
|
|
|
|
|
|
contents = []
|
|
|
|
handlebars_includes.each { |hb| contents << "require_asset('#{hb}')" }
|
|
|
|
javascript_includes.each { |js| contents << "require_asset('#{js}')" }
|
|
|
|
|
|
|
|
each_globbed_asset do |f, is_dir|
|
|
|
|
contents << (is_dir ? "depend_on('#{f}')" : "require_asset('#{f}')")
|
|
|
|
end
|
|
|
|
|
|
|
|
if contents.present?
|
|
|
|
contents.insert(0, "<%")
|
|
|
|
contents << "%>"
|
2020-03-04 12:28:26 -05:00
|
|
|
Discourse::Utils.atomic_write_file(js_file_path, contents.join("\n"))
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
File.delete(js_file_path)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
2019-07-15 10:52:54 -04:00
|
|
|
end
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2013-08-25 22:52:36 -04:00
|
|
|
def auth_provider(opts)
|
2018-11-30 11:58:18 -05:00
|
|
|
before_auth do
|
|
|
|
provider = Auth::AuthProvider.new
|
2015-09-25 11:29:05 -04:00
|
|
|
|
2018-11-30 11:58:18 -05:00
|
|
|
Auth::AuthProvider.auth_attributes.each do |sym|
|
2019-05-06 22:05:58 -04:00
|
|
|
provider.public_send("#{sym}=", opts.delete(sym)) if opts.has_key?(sym)
|
2018-11-30 11:58:18 -05:00
|
|
|
end
|
2018-07-23 11:51:57 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
provider.authenticator.enabled?
|
|
|
|
rescue NotImplementedError
|
|
|
|
provider.authenticator.define_singleton_method(:enabled?) do
|
2018-11-22 10:59:47 -05:00
|
|
|
Discourse.deprecate("#{provider.authenticator.class.name} should define an `enabled?` function. Patching for now.")
|
2019-05-06 21:00:09 -04:00
|
|
|
return SiteSetting.get(provider.enabled_setting) if provider.enabled_setting
|
2018-11-22 10:59:47 -05:00
|
|
|
Discourse.deprecate("#{provider.authenticator.class.name} has not defined an enabled_setting. Defaulting to true.")
|
2018-07-23 11:51:57 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-30 11:58:18 -05:00
|
|
|
DiscoursePluginRegistry.register_auth_provider(provider)
|
|
|
|
end
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# shotgun approach to gem loading, in future we need to hack bundler
|
|
|
|
# to at least determine dependencies do not clash before loading
|
|
|
|
#
|
|
|
|
# Additionally we want to support multiple ruby versions correctly and so on
|
|
|
|
#
|
|
|
|
# This is a very rough initial implementation
|
|
|
|
def gem(name, version, opts = {})
|
2017-01-09 17:10:14 -05:00
|
|
|
PluginGem.load(path, name, version, opts)
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2018-05-08 01:24:58 -04:00
|
|
|
def hide_plugin
|
|
|
|
Discourse.hidden_plugins << self
|
|
|
|
end
|
|
|
|
|
2018-05-07 22:30:33 -04:00
|
|
|
def enabled_site_setting_filter(filter = nil)
|
2020-05-10 08:05:23 -04:00
|
|
|
STDERR.puts("`enabled_site_setting_filter` is deprecated")
|
2018-05-07 22:30:33 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def enabled_site_setting(setting = nil)
|
2015-07-02 12:45:17 -04:00
|
|
|
if setting
|
|
|
|
@enabled_site_setting = setting
|
|
|
|
else
|
|
|
|
@enabled_site_setting
|
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 19:42:55 -05:00
|
|
|
def handlebars_includes
|
|
|
|
assets.map do |asset, opts|
|
|
|
|
next if opts == :admin
|
|
|
|
next unless asset =~ DiscoursePluginRegistry::HANDLEBARS_REGEX
|
|
|
|
asset
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_includes
|
|
|
|
assets.map do |asset, opts|
|
2018-04-10 02:37:16 -04:00
|
|
|
next if opts == :vendored_core_pretty_text
|
2016-11-14 19:42:55 -05:00
|
|
|
next if opts == :admin
|
|
|
|
next unless asset =~ DiscoursePluginRegistry::JS_REGEX
|
|
|
|
asset
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_globbed_asset
|
|
|
|
if @path
|
|
|
|
# Automatically include all ES6 JS and hbs files
|
|
|
|
root_path = "#{File.dirname(@path)}/assets/javascripts"
|
2020-05-08 11:10:34 -04:00
|
|
|
admin_path = "#{File.dirname(@path)}/admin/assets/javascripts"
|
2016-11-14 19:42:55 -05:00
|
|
|
|
2020-05-08 11:10:34 -04:00
|
|
|
Dir.glob(["#{root_path}/**/*", "#{admin_path}/**/*"]) do |f|
|
2020-04-13 15:05:46 -04:00
|
|
|
f_str = f.to_s
|
2016-11-14 19:42:55 -05:00
|
|
|
if File.directory?(f)
|
2017-07-27 21:20:09 -04:00
|
|
|
yield [f, true]
|
2020-04-13 15:05:46 -04:00
|
|
|
elsif f_str.ends_with?(".js.es6") || f_str.ends_with?(".hbs") || f_str.ends_with?(".hbr")
|
|
|
|
yield [f, false]
|
|
|
|
elsif transpile_js && f_str.ends_with?(".js")
|
2017-07-27 21:20:09 -04:00
|
|
|
yield [f, false]
|
2016-11-14 19:42:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def register_reviewable_type(reviewable_type_class)
|
2019-04-08 13:42:36 -04:00
|
|
|
extend_list_method Reviewable, :types, [reviewable_type_class.name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extend_list_method(klass, method, new_attributes)
|
2019-05-06 21:27:05 -04:00
|
|
|
current_list = klass.public_send(method)
|
2019-04-08 13:42:36 -04:00
|
|
|
current_list.concat(new_attributes)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
reloadable_patch do
|
2019-05-06 21:27:05 -04:00
|
|
|
klass.public_send(:define_singleton_method, method) { current_list }
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-20 12:39:52 -04:00
|
|
|
def directory_name
|
|
|
|
@directory_name ||= File.dirname(path).split("/").last
|
|
|
|
end
|
|
|
|
|
2019-08-21 23:09:10 -04:00
|
|
|
def css_asset_exists?(target = nil)
|
|
|
|
DiscoursePluginRegistry.stylesheets_exists?(directory_name, target)
|
2019-07-15 10:52:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def js_asset_exists?
|
|
|
|
File.exists?(js_file_path)
|
|
|
|
end
|
|
|
|
|
2019-11-22 14:33:10 -05:00
|
|
|
# Receives an array with two elements:
|
|
|
|
# 1. A symbol that represents the name of the value to filter.
|
|
|
|
# 2. A Proc that takes the existing ActiveRecord::Relation and the value received from the front-end.
|
|
|
|
def add_custom_reviewable_filter(filter)
|
|
|
|
reloadable_patch do
|
|
|
|
Reviewable.add_custom_filter(filter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-10 02:30:22 -04:00
|
|
|
protected
|
|
|
|
|
2019-07-15 10:52:54 -04:00
|
|
|
def self.js_path
|
|
|
|
File.expand_path "#{Rails.root}/app/assets/javascripts/plugins"
|
|
|
|
end
|
|
|
|
|
|
|
|
def js_file_path
|
2019-08-20 12:39:52 -04:00
|
|
|
@file_path ||= "#{Plugin::Instance.js_path}/#{directory_name}.js.erb"
|
2019-07-15 10:52:54 -04:00
|
|
|
end
|
|
|
|
|
2014-04-10 02:30:22 -04:00
|
|
|
def register_assets!
|
2019-08-20 12:39:52 -04:00
|
|
|
assets.each do |asset, opts, plugin_directory_name|
|
|
|
|
DiscoursePluginRegistry.register_asset(asset, opts, plugin_directory_name)
|
2014-04-10 02:30:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 20:02:01 -05:00
|
|
|
def register_service_workers!
|
|
|
|
service_workers.each do |asset, opts|
|
|
|
|
DiscoursePluginRegistry.register_service_worker(asset, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
def register_locales!
|
|
|
|
root_path = File.dirname(@path)
|
|
|
|
|
|
|
|
locales.each do |locale, opts|
|
|
|
|
opts = opts.dup
|
|
|
|
opts[:client_locale_file] = File.join(root_path, "config/locales/client.#{locale}.yml")
|
|
|
|
opts[:server_locale_file] = File.join(root_path, "config/locales/server.#{locale}.yml")
|
|
|
|
opts[:js_locale_file] = File.join(root_path, "assets/locales/#{locale}.js.erb")
|
|
|
|
|
|
|
|
locale_chain = opts[:fallbackLocale] ? [locale, opts[:fallbackLocale]] : [locale]
|
|
|
|
lib_locale_path = File.join(root_path, "lib/javascripts/locale")
|
|
|
|
|
|
|
|
path = File.join(lib_locale_path, "message_format")
|
|
|
|
opts[:message_format] = find_locale_file(locale_chain, path)
|
2019-02-19 08:42:58 -05:00
|
|
|
opts[:message_format] = JsLocaleHelper.find_message_format_locale(locale_chain, fallback_to_english: false) unless opts[:message_format]
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
path = File.join(lib_locale_path, "moment_js")
|
|
|
|
opts[:moment_js] = find_locale_file(locale_chain, path)
|
|
|
|
opts[:moment_js] = JsLocaleHelper.find_moment_locale(locale_chain) unless opts[:moment_js]
|
|
|
|
|
2019-02-25 14:40:02 -05:00
|
|
|
path = File.join(lib_locale_path, "moment_js_timezones")
|
|
|
|
opts[:moment_js_timezones] = find_locale_file(locale_chain, path)
|
|
|
|
opts[:moment_js_timezones] = JsLocaleHelper.find_moment_locale(locale_chain, timezone_names: true) unless opts[:moment_js_timezones]
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
if valid_locale?(opts)
|
|
|
|
DiscoursePluginRegistry.register_locale(locale, opts)
|
|
|
|
Rails.configuration.assets.precompile << "locales/#{locale}.js"
|
2018-04-20 15:29:03 -04:00
|
|
|
else
|
2018-06-22 10:20:20 -04:00
|
|
|
msg = "Invalid locale! #{opts.inspect}"
|
|
|
|
# The logger isn't always present during boot / parsing locales from plugins
|
|
|
|
if Rails.logger.present?
|
|
|
|
Rails.logger.error(msg)
|
|
|
|
else
|
|
|
|
puts msg
|
|
|
|
end
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-29 10:56:46 -04:00
|
|
|
def allow_new_queued_post_payload_attribute(attribute_name)
|
|
|
|
reloadable_patch do
|
|
|
|
NewPostManager.add_plugin_payload_attribute(attribute_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-06 09:02:40 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def write_asset(path, contents)
|
|
|
|
unless File.exists?(path)
|
|
|
|
ensure_directory(path)
|
2017-07-27 21:20:09 -04:00
|
|
|
File.open(path, "w") { |f| f.write(contents) }
|
2015-11-06 09:02:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 16:22:18 -04:00
|
|
|
def reloadable_patch(plugin = self)
|
2017-08-31 00:06:56 -04:00
|
|
|
if Rails.env.development? && defined?(ActiveSupport::Reloader)
|
|
|
|
ActiveSupport::Reloader.to_prepare do
|
2017-08-09 16:22:18 -04:00
|
|
|
# reload the patch
|
2017-08-09 12:28:32 -04:00
|
|
|
yield plugin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# apply the patch
|
|
|
|
yield plugin
|
|
|
|
end
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
def valid_locale?(custom_locale)
|
|
|
|
File.exist?(custom_locale[:client_locale_file]) &&
|
|
|
|
File.exist?(custom_locale[:server_locale_file]) &&
|
|
|
|
File.exist?(custom_locale[:js_locale_file]) &&
|
|
|
|
custom_locale[:message_format] && custom_locale[:moment_js]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_locale_file(locale_chain, path)
|
|
|
|
locale_chain.each do |locale|
|
|
|
|
filename = File.join(path, "#{locale}.js")
|
|
|
|
return [locale, filename] if File.exist?(filename)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|