Automatically mount provider’s engines at /chat-integration/{provider_name}
Provider controllers are automatically disabled when the provider is disabled (works the same way as plugin controllers)
This commit is contained in:
parent
aaaffdf371
commit
2f6d6f0d54
|
@ -21,6 +21,10 @@ module DiscourseChat
|
|||
end
|
||||
end
|
||||
|
||||
def self.enabled_provider_names
|
||||
self.enabled_providers.map {|x| x::PROVIDER_NAME}
|
||||
end
|
||||
|
||||
def self.get_by_name(name)
|
||||
self.providers.find{|p| p::PROVIDER_NAME == name}
|
||||
end
|
||||
|
@ -33,6 +37,51 @@ module DiscourseChat
|
|||
end
|
||||
end
|
||||
|
||||
class HookEngine < ::Rails::Engine
|
||||
engine_name DiscourseChat::PLUGIN_NAME+"-hooks"
|
||||
isolate_namespace DiscourseChat::Provider
|
||||
end
|
||||
|
||||
class HookController < ::ApplicationController
|
||||
requires_plugin DiscourseChat::PLUGIN_NAME
|
||||
|
||||
class ProviderDisabled < StandardError; end
|
||||
|
||||
rescue_from ProviderDisabled do
|
||||
rescue_discourse_actions(:not_found, 404)
|
||||
end
|
||||
|
||||
def self.requires_provider(provider_name)
|
||||
before_filter do
|
||||
raise ProviderDisabled.new unless Provider.enabled_provider_names.include?(provider_name)
|
||||
end
|
||||
end
|
||||
|
||||
def respond
|
||||
render
|
||||
end
|
||||
end
|
||||
|
||||
# Automatically mount each provider's engine inside the HookEngine
|
||||
def self.mount_engines
|
||||
engines = []
|
||||
DiscourseChat::Provider.providers.each do |provider|
|
||||
engine = provider.constants.select do |constant|
|
||||
constant.to_s =~ /Engine$/ and not constant.to_s == "HookEngine"
|
||||
end.map(&provider.method(:const_get)).first
|
||||
|
||||
if engine
|
||||
engines.push({engine: engine, name: provider::PROVIDER_NAME})
|
||||
end
|
||||
end
|
||||
|
||||
DiscourseChat::Provider::HookEngine.routes.draw do
|
||||
engines.each do |engine|
|
||||
mount engine[:engine], at: engine[:name]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
module DiscourseChat::Provider::SlackProvider
|
||||
class SlackCommandController < DiscourseChat::Provider::HookController
|
||||
requires_provider ::DiscourseChat::Provider::SlackProvider::PROVIDER_NAME
|
||||
|
||||
def say_hello
|
||||
|
||||
render json: {hello: "world"}
|
||||
end
|
||||
end
|
||||
|
||||
class SlackEngine < ::Rails::Engine
|
||||
engine_name DiscourseChat::PLUGIN_NAME+"-slack"
|
||||
isolate_namespace DiscourseChat::Provider::SlackProvider
|
||||
end
|
||||
|
||||
SlackEngine.routes.draw do
|
||||
get "command" => "slack_command#say_hello"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
require_relative "slack_message_formatter.rb"
|
||||
|
||||
module DiscourseChat::Provider::SlackProvider
|
||||
PROVIDER_NAME = "slack".freeze
|
||||
|
||||
|
@ -134,3 +132,6 @@ module DiscourseChat::Provider::SlackProvider
|
|||
|
||||
end
|
||||
end
|
||||
|
||||
require_relative "slack_message_formatter.rb"
|
||||
require_relative "slack_command_controller.rb"
|
|
@ -0,0 +1,19 @@
|
|||
module DiscourseChat::Provider::TelegramProvider
|
||||
class TelegramCommandController < DiscourseChat::Provider::HookController
|
||||
requires_provider ::DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
|
||||
|
||||
def say_hello
|
||||
|
||||
render json: {hello: "from telegram"}
|
||||
end
|
||||
end
|
||||
|
||||
class TelegramEngine < ::Rails::Engine
|
||||
engine_name DiscourseChat::PLUGIN_NAME+"-telegram"
|
||||
isolate_namespace DiscourseChat::Provider::TelegramProvider
|
||||
end
|
||||
|
||||
TelegramEngine.routes.draw do
|
||||
get "command" => "telegram_command#say_hello"
|
||||
end
|
||||
end
|
|
@ -8,3 +8,5 @@ module DiscourseChat
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
require_relative "telegram_command_controller.rb"
|
11
plugin.rb
11
plugin.rb
|
@ -15,8 +15,8 @@ after_initialize do
|
|||
module ::DiscourseChat
|
||||
PLUGIN_NAME = "discourse-chat-integration".freeze
|
||||
|
||||
class Engine < ::Rails::Engine
|
||||
engine_name DiscourseChat::PLUGIN_NAME
|
||||
class AdminEngine < ::Rails::Engine
|
||||
engine_name DiscourseChat::PLUGIN_NAME+"-admin"
|
||||
isolate_namespace DiscourseChat
|
||||
end
|
||||
|
||||
|
@ -178,7 +178,7 @@ after_initialize do
|
|||
|
||||
add_admin_route 'chat_integration.menu_title', 'chat'
|
||||
|
||||
DiscourseChat::Engine.routes.draw do
|
||||
DiscourseChat::AdminEngine.routes.draw do
|
||||
get "" => "chat#respond"
|
||||
get '/providers' => "chat#list_providers"
|
||||
post '/test' => "chat#test_provider"
|
||||
|
@ -192,7 +192,10 @@ after_initialize do
|
|||
end
|
||||
|
||||
Discourse::Application.routes.append do
|
||||
mount ::DiscourseChat::Engine, at: '/admin/plugins/chat', constraints: AdminConstraint.new
|
||||
mount ::DiscourseChat::AdminEngine, at: '/admin/plugins/chat', constraints: AdminConstraint.new
|
||||
mount ::DiscourseChat::Provider::HookEngine, at: '/chat-integration/'
|
||||
end
|
||||
|
||||
DiscourseChat::Provider.mount_engines
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue