diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 364bca5..f81f1fb 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -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 diff --git a/lib/discourse_chat/provider/slack/slack_command_controller.rb b/lib/discourse_chat/provider/slack/slack_command_controller.rb new file mode 100644 index 0000000..dbd6c7a --- /dev/null +++ b/lib/discourse_chat/provider/slack/slack_command_controller.rb @@ -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 + + + diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index 8236bf6..a8e368f 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -1,5 +1,3 @@ -require_relative "slack_message_formatter.rb" - module DiscourseChat::Provider::SlackProvider PROVIDER_NAME = "slack".freeze @@ -133,4 +131,7 @@ module DiscourseChat::Provider::SlackProvider end end -end \ No newline at end of file +end + +require_relative "slack_message_formatter.rb" +require_relative "slack_command_controller.rb" \ No newline at end of file diff --git a/lib/discourse_chat/provider/telegram/telegram_command_controller.rb b/lib/discourse_chat/provider/telegram/telegram_command_controller.rb new file mode 100644 index 0000000..c63c22e --- /dev/null +++ b/lib/discourse_chat/provider/telegram/telegram_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 \ No newline at end of file diff --git a/lib/discourse_chat/provider/telegram/telegram_provider.rb b/lib/discourse_chat/provider/telegram/telegram_provider.rb index 5015260..d63aee6 100644 --- a/lib/discourse_chat/provider/telegram/telegram_provider.rb +++ b/lib/discourse_chat/provider/telegram/telegram_provider.rb @@ -7,4 +7,6 @@ module DiscourseChat PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled end end -end \ No newline at end of file +end + +require_relative "telegram_command_controller.rb" \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 1e2c54a..13392b5 100644 --- a/plugin.rb +++ b/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