2021-03-11 17:30:15 +11:00
|
|
|
# frozen_string_literal: true
|
2017-09-23 10:34:07 +08:00
|
|
|
# name: discourse-custom-wizard
|
2017-10-05 08:36:46 +08:00
|
|
|
# about: Create custom wizards
|
2021-07-20 15:32:18 +08:00
|
|
|
# version: 0.8.1
|
2017-09-23 10:34:07 +08:00
|
|
|
# authors: Angus McLeod
|
2020-11-09 15:14:45 +11:00
|
|
|
# url: https://github.com/paviliondev/discourse-custom-wizard
|
2021-03-10 20:45:45 +11:00
|
|
|
# contact emails: angus@thepavilion.io
|
2017-09-23 10:34:07 +08:00
|
|
|
|
2021-04-09 11:04:42 +05:30
|
|
|
gem 'liquid', '5.0.1', require: true
|
2021-09-24 17:58:42 +08:00
|
|
|
register_asset 'stylesheets/admin/admin.scss', :desktop
|
2019-12-05 17:48:32 +11:00
|
|
|
enabled_site_setting :custom_wizard_enabled
|
|
|
|
|
2017-09-25 22:47:40 +08:00
|
|
|
config = Rails.application.config
|
2019-12-05 19:05:21 +11:00
|
|
|
plugin_asset_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets"
|
|
|
|
config.assets.paths << "#{plugin_asset_path}/javascripts"
|
|
|
|
config.assets.paths << "#{plugin_asset_path}/stylesheets/wizard"
|
2017-09-25 22:47:40 +08:00
|
|
|
|
2017-11-01 19:47:27 +08:00
|
|
|
if Rails.env.production?
|
2017-12-03 15:57:54 +08:00
|
|
|
config.assets.precompile += %w{
|
2019-10-09 10:09:30 +11:00
|
|
|
wizard-custom-guest.js
|
2020-10-02 15:03:26 +10:00
|
|
|
wizard-custom-globals.js
|
2017-12-03 15:57:54 +08:00
|
|
|
wizard-custom.js
|
2019-01-18 11:34:52 +11:00
|
|
|
wizard-custom-start.js
|
2020-10-02 18:07:16 +10:00
|
|
|
wizard-plugin.js.erb
|
2019-01-29 11:42:23 +11:00
|
|
|
wizard-raw-templates.js.erb
|
2017-12-03 15:57:54 +08:00
|
|
|
}
|
2017-11-01 19:47:27 +08:00
|
|
|
end
|
|
|
|
|
2019-02-20 17:27:17 +11:00
|
|
|
if respond_to?(:register_svg_icon)
|
2020-03-30 10:53:28 +11:00
|
|
|
register_svg_icon "far-calendar"
|
2019-02-20 17:27:17 +11:00
|
|
|
register_svg_icon "chevron-right"
|
|
|
|
register_svg_icon "chevron-left"
|
2020-11-10 11:56:11 +11:00
|
|
|
register_svg_icon "save"
|
2021-08-30 16:23:33 -07:00
|
|
|
register_svg_icon "sliders-h"
|
|
|
|
register_svg_icon "calendar"
|
|
|
|
register_svg_icon "check"
|
|
|
|
register_svg_icon "times"
|
|
|
|
register_svg_icon "clock"
|
|
|
|
register_svg_icon "link"
|
|
|
|
register_svg_icon "comment-alt"
|
2021-08-09 14:44:44 -07:00
|
|
|
register_svg_icon "far-life-ring"
|
2021-07-20 15:32:18 +08:00
|
|
|
register_svg_icon "arrow-right"
|
2021-10-05 20:54:06 +08:00
|
|
|
register_svg_icon "bolt"
|
2019-02-20 17:27:17 +11:00
|
|
|
end
|
2019-01-14 13:03:42 +11:00
|
|
|
|
2021-05-12 06:33:19 +05:30
|
|
|
class ::Sprockets::DirectiveProcessor
|
|
|
|
def process_require_tree_discourse_directive(path = ".")
|
|
|
|
raise CustomWizard::SprocketsEmptyPath, "path cannot be empty" if path == "."
|
2021-05-12 06:17:01 +05:30
|
|
|
|
2021-05-12 06:33:19 +05:30
|
|
|
discourse_asset_path = "#{Rails.root}/app/assets/javascripts/"
|
|
|
|
path = File.expand_path(path, discourse_asset_path)
|
|
|
|
stat = @environment.stat(path)
|
2021-05-12 06:17:01 +05:30
|
|
|
|
2021-05-12 06:33:19 +05:30
|
|
|
if stat && stat.directory?
|
|
|
|
require_paths(*@environment.stat_sorted_tree_with_dependencies(path))
|
|
|
|
else
|
|
|
|
raise CustomWizard::SprocketsFileNotFound, "#{path} not found in discourse core"
|
|
|
|
end
|
2021-05-12 06:17:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-23 10:34:07 +08:00
|
|
|
after_initialize do
|
2020-03-30 17:16:03 +11:00
|
|
|
%w[
|
|
|
|
../lib/custom_wizard/engine.rb
|
|
|
|
../config/routes.rb
|
2020-04-13 22:17:22 +10:00
|
|
|
../controllers/custom_wizard/admin/admin.rb
|
|
|
|
../controllers/custom_wizard/admin/wizard.rb
|
|
|
|
../controllers/custom_wizard/admin/submissions.rb
|
|
|
|
../controllers/custom_wizard/admin/api.rb
|
2020-04-15 10:46:44 +10:00
|
|
|
../controllers/custom_wizard/admin/logs.rb
|
2020-11-09 14:32:36 +11:00
|
|
|
../controllers/custom_wizard/admin/manager.rb
|
2020-10-17 12:31:07 +11:00
|
|
|
../controllers/custom_wizard/admin/custom_fields.rb
|
2021-09-24 17:58:42 +08:00
|
|
|
../controllers/custom_wizard/admin/subscription.rb
|
|
|
|
../controllers/custom_wizard/admin/notice.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../controllers/custom_wizard/wizard.rb
|
|
|
|
../controllers/custom_wizard/steps.rb
|
2021-02-05 18:29:30 +05:30
|
|
|
../controllers/custom_wizard/realtime_validations.rb
|
2021-08-10 14:45:23 +08:00
|
|
|
../jobs/regular/refresh_api_access_token.rb
|
|
|
|
../jobs/regular/set_after_time_wizard.rb
|
2021-09-24 17:58:42 +08:00
|
|
|
../jobs/scheduled/custom_wizard/update_subscription.rb
|
|
|
|
../jobs/scheduled/custom_wizard/update_notices.rb
|
2020-11-26 14:05:50 +11:00
|
|
|
../lib/custom_wizard/validators/template.rb
|
|
|
|
../lib/custom_wizard/validators/update.rb
|
2020-05-04 19:02:49 +10:00
|
|
|
../lib/custom_wizard/action_result.rb
|
2020-04-15 10:46:44 +10:00
|
|
|
../lib/custom_wizard/action.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/builder.rb
|
2020-12-04 18:05:56 +11:00
|
|
|
../lib/custom_wizard/cache.rb
|
2020-10-20 16:40:23 +11:00
|
|
|
../lib/custom_wizard/custom_field.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/field.rb
|
2021-01-27 10:38:26 +05:30
|
|
|
../lib/custom_wizard/realtime_validation.rb
|
2021-02-16 11:43:00 +11:00
|
|
|
../lib/custom_wizard/realtime_validations/result.rb
|
|
|
|
../lib/custom_wizard/realtime_validations/similar_topics.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/mapper.rb
|
2020-04-15 10:46:44 +10:00
|
|
|
../lib/custom_wizard/log.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/step_updater.rb
|
2021-04-21 03:58:19 +10:00
|
|
|
../lib/custom_wizard/step.rb
|
2021-06-17 17:50:22 +10:00
|
|
|
../lib/custom_wizard/submission.rb
|
2020-10-31 18:05:50 +11:00
|
|
|
../lib/custom_wizard/template.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/wizard.rb
|
2021-09-24 17:58:42 +08:00
|
|
|
../lib/custom_wizard/notice.rb
|
2021-10-05 20:54:06 +08:00
|
|
|
../lib/custom_wizard/notice/connection_error.rb
|
2021-09-24 17:58:42 +08:00
|
|
|
../lib/custom_wizard/subscription.rb
|
|
|
|
../lib/custom_wizard/subscription/subscription.rb
|
|
|
|
../lib/custom_wizard/subscription/authentication.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../lib/custom_wizard/api/api.rb
|
|
|
|
../lib/custom_wizard/api/authorization.rb
|
|
|
|
../lib/custom_wizard/api/endpoint.rb
|
|
|
|
../lib/custom_wizard/api/log_entry.rb
|
2021-04-09 11:04:42 +05:30
|
|
|
../lib/custom_wizard/liquid_extensions/first_non_empty.rb
|
2021-05-12 06:14:09 +05:30
|
|
|
../lib/custom_wizard/exceptions/exceptions.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../serializers/custom_wizard/api/authorization_serializer.rb
|
|
|
|
../serializers/custom_wizard/api/basic_endpoint_serializer.rb
|
|
|
|
../serializers/custom_wizard/api/endpoint_serializer.rb
|
|
|
|
../serializers/custom_wizard/api/log_serializer.rb
|
|
|
|
../serializers/custom_wizard/api_serializer.rb
|
|
|
|
../serializers/custom_wizard/basic_api_serializer.rb
|
2020-04-13 22:17:22 +10:00
|
|
|
../serializers/custom_wizard/basic_wizard_serializer.rb
|
2020-10-20 16:40:23 +11:00
|
|
|
../serializers/custom_wizard/custom_field_serializer.rb
|
2020-03-30 17:16:03 +11:00
|
|
|
../serializers/custom_wizard/wizard_field_serializer.rb
|
|
|
|
../serializers/custom_wizard/wizard_step_serializer.rb
|
|
|
|
../serializers/custom_wizard/wizard_serializer.rb
|
2020-04-15 10:46:44 +10:00
|
|
|
../serializers/custom_wizard/log_serializer.rb
|
2021-06-23 16:13:58 +10:00
|
|
|
../serializers/custom_wizard/submission_serializer.rb
|
2021-02-16 11:43:00 +11:00
|
|
|
../serializers/custom_wizard/realtime_validation/similar_topics_serializer.rb
|
2021-09-24 17:58:42 +08:00
|
|
|
../serializers/custom_wizard/subscription/authentication_serializer.rb
|
|
|
|
../serializers/custom_wizard/subscription/subscription_serializer.rb
|
|
|
|
../serializers/custom_wizard/subscription_serializer.rb
|
|
|
|
../serializers/custom_wizard/notice_serializer.rb
|
2020-11-23 10:11:45 +11:00
|
|
|
../extensions/extra_locales_controller.rb
|
2020-04-19 16:06:18 +10:00
|
|
|
../extensions/invites_controller.rb
|
2020-09-16 23:35:07 +10:00
|
|
|
../extensions/users_controller.rb
|
2020-11-09 17:50:17 +11:00
|
|
|
../extensions/custom_field/preloader.rb
|
|
|
|
../extensions/custom_field/serializer.rb
|
2021-06-08 21:39:49 +10:00
|
|
|
../extensions/custom_field/extension.rb
|
2019-12-05 17:48:32 +11:00
|
|
|
].each do |path|
|
|
|
|
load File.expand_path(path, __FILE__)
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2021-04-09 11:04:42 +05:30
|
|
|
Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty)
|
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
add_class_method(:wizard, :user_requires_completion?) do |user|
|
|
|
|
wizard_result = self.new(user).requires_completion?
|
|
|
|
return wizard_result if wizard_result
|
2019-12-05 17:48:32 +11:00
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
custom_redirect = false
|
2019-12-05 17:48:32 +11:00
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
if user &&
|
|
|
|
user.first_seen_at.blank? &&
|
2020-03-30 17:16:03 +11:00
|
|
|
wizard = CustomWizard::Wizard.after_signup(user)
|
2017-10-15 11:58:22 +08:00
|
|
|
|
2020-03-30 17:16:03 +11:00
|
|
|
if !wizard.completed?
|
2019-12-05 19:05:21 +11:00
|
|
|
custom_redirect = true
|
2021-06-17 17:50:22 +10:00
|
|
|
CustomWizard::Wizard.set_user_redirect(wizard.id, user)
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
!!custom_redirect
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
add_to_class(:users_controller, :wizard_path) do
|
|
|
|
if custom_wizard_redirect = current_user.custom_fields['redirect_to_wizard']
|
|
|
|
"#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
|
|
|
|
else
|
|
|
|
"#{Discourse.base_url}/wizard"
|
2017-11-01 12:21:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-05 19:05:21 +11:00
|
|
|
add_to_serializer(:current_user, :redirect_to_wizard) do
|
|
|
|
object.custom_fields['redirect_to_wizard']
|
2017-11-03 21:43:57 +08:00
|
|
|
end
|
2018-01-16 15:13:50 +08:00
|
|
|
|
2020-03-30 10:53:28 +11:00
|
|
|
on(:user_approved) do |user|
|
2020-04-19 16:42:44 +10:00
|
|
|
if wizard = CustomWizard::Wizard.after_signup(user)
|
2021-06-17 17:50:22 +10:00
|
|
|
CustomWizard::Wizard.set_user_redirect(wizard.id, user)
|
2018-06-05 12:25:44 +10:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-19 16:06:18 +10:00
|
|
|
add_to_class(:application_controller, :redirect_to_wizard_if_required) do
|
|
|
|
wizard_id = current_user.custom_fields['redirect_to_wizard']
|
|
|
|
@excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split('|') + ['/w/']
|
|
|
|
url = request.referer || request.original_url
|
|
|
|
|
2021-03-11 17:30:15 +11:00
|
|
|
if request.format === 'text/html' && !@excluded_routes.any? { |str| /#{str}/ =~ url } && wizard_id
|
2020-04-19 16:06:18 +10:00
|
|
|
if request.referer !~ /\/w\// && request.referer !~ /\/invites\//
|
2021-06-17 17:50:22 +10:00
|
|
|
CustomWizard::Wizard.set_wizard_redirect(current_user, wizard_id, request.referer)
|
2020-04-19 16:06:18 +10:00
|
|
|
end
|
2020-11-03 11:24:20 +11:00
|
|
|
if CustomWizard::Template.exists?(wizard_id)
|
2020-04-19 16:06:18 +10:00
|
|
|
redirect_to "/w/#{wizard_id.dasherize}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-19 16:06:18 +10:00
|
|
|
add_to_serializer(:site, :include_wizard_required?) do
|
|
|
|
scope.is_admin? && Wizard.new(scope.user).requires_completion?
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-19 16:06:18 +10:00
|
|
|
add_to_serializer(:site, :complete_custom_wizard) do
|
|
|
|
if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user)
|
2021-03-11 17:30:15 +11:00
|
|
|
requires_completion.map { |w| { name: w[:name], url: "/w/#{w[:id]}" } }
|
2020-04-19 16:06:18 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:site, :include_complete_custom_wizard?) do
|
|
|
|
complete_custom_wizard.present?
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-04-19 16:06:18 +10:00
|
|
|
add_model_callback(:application_controller, :before_action) do
|
|
|
|
redirect_to_wizard_if_required if current_user
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-23 10:11:45 +11:00
|
|
|
::ExtraLocalesController.prepend ExtraLocalesControllerCustomWizard
|
2020-04-30 10:54:30 +10:00
|
|
|
::InvitesController.prepend InvitesControllerCustomWizard
|
2020-09-16 23:35:07 +10:00
|
|
|
::UsersController.prepend CustomWizardUsersController
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-10-31 18:05:50 +11:00
|
|
|
full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss"
|
2021-03-13 02:57:18 -05:00
|
|
|
if Stylesheet::Importer.respond_to?(:plugin_assets)
|
|
|
|
Stylesheet::Importer.plugin_assets['wizard_custom'] = Set[full_path]
|
|
|
|
else
|
|
|
|
# legacy method, Discourse 2.7.0.beta5 and below
|
|
|
|
DiscoursePluginRegistry.register_asset(full_path, {}, "wizard_custom")
|
|
|
|
Stylesheet::Importer.register_import("wizard_custom") do
|
|
|
|
import_files(DiscoursePluginRegistry.stylesheets["wizard_custom"])
|
|
|
|
end
|
2020-10-31 18:05:50 +11:00
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-08 14:24:20 +11:00
|
|
|
CustomWizard::CustomField::CLASSES.keys.each do |klass|
|
2021-06-08 21:39:49 +10:00
|
|
|
class_constant = klass.to_s.classify.constantize
|
|
|
|
|
2020-11-08 14:24:20 +11:00
|
|
|
add_model_callback(klass, :after_initialize) do
|
2020-12-01 18:20:02 +11:00
|
|
|
if CustomWizard::CustomField.enabled?
|
|
|
|
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
|
2021-06-08 21:39:49 +10:00
|
|
|
class_constant.register_custom_field_type(field[:name], field[:type].to_sym)
|
2020-12-01 18:20:02 +11:00
|
|
|
end
|
2020-10-23 11:53:39 +11:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2021-06-08 21:39:49 +10:00
|
|
|
class_constant.singleton_class.prepend CustomWizardCustomFieldPreloader
|
|
|
|
class_constant.singleton_class.prepend CustomWizardCustomFieldExtension
|
2020-11-08 14:24:20 +11:00
|
|
|
end
|
2021-03-11 17:30:15 +11:00
|
|
|
|
2020-11-08 14:24:20 +11:00
|
|
|
CustomWizard::CustomField.serializers.each do |serializer_klass|
|
2020-11-09 17:50:17 +11:00
|
|
|
"#{serializer_klass}_serializer".classify.constantize.prepend CustomWizardCustomFieldSerializer
|
2020-10-23 11:53:39 +11:00
|
|
|
end
|
|
|
|
|
2021-09-24 17:58:42 +08:00
|
|
|
AdminDashboardData.add_problem_check do
|
2021-10-05 20:54:06 +08:00
|
|
|
warning_notices = CustomWizard::Notice.list(type: CustomWizard::Notice.types[:plugin_status_warning])
|
2021-09-24 17:58:42 +08:00
|
|
|
warning_notices.any? ? ActionView::Base.full_sanitizer.sanitize(warning_notices.first.message, tags: %w(a)) : nil
|
|
|
|
end
|
|
|
|
|
2018-01-16 15:13:50 +08:00
|
|
|
DiscourseEvent.trigger(:custom_wizard_ready)
|
2017-09-23 10:34:07 +08:00
|
|
|
end
|