discourse-chat-integration/lib/discourse_chat/provider.rb

98 lines
2.6 KiB
Ruby
Raw Normal View History

module DiscourseChat
class ProviderError < StandardError
attr_accessor :info
def initialize(message = nil, info: nil)
super(message)
self.info = info.nil? ? {} : info
end
end
module Provider
def self.providers
constants.select do |constant|
constant.to_s =~ /Provider$/
end.map(&method(:const_get))
end
def self.enabled_providers
self.providers.select do |provider|
self.is_enabled(provider)
end
end
def self.provider_names
2017-08-01 15:53:39 -04:00
self.providers.map { |x| x::PROVIDER_NAME }
end
def self.enabled_provider_names
2017-08-01 15:53:39 -04:00
self.enabled_providers.map { |x| x::PROVIDER_NAME }
end
def self.get_by_name(name)
2017-08-01 15:53:39 -04:00
self.providers.find { |p| p::PROVIDER_NAME == name }
end
def self.is_enabled(provider)
2017-08-01 15:53:39 -04:00
if defined? provider::PROVIDER_ENABLED_SETTING
SiteSetting.send(provider::PROVIDER_ENABLED_SETTING)
else
false
end
end
class HookEngine < ::Rails::Engine
2017-08-01 15:53:39 -04:00
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
2017-08-01 15:53:39 -04:00
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|
2017-08-01 15:53:39 -04:00
constant.to_s =~ (/Engine$/) && (not constant.to_s == "HookEngine")
end.map(&provider.method(:const_get)).first
if engine
2017-08-01 15:53:39 -04:00
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
require_relative "provider/slack/slack_provider.rb"
2017-07-21 10:48:57 -04:00
require_relative "provider/telegram/telegram_provider.rb"
2017-07-24 14:22:19 -04:00
require_relative "provider/discord/discord_provider.rb"
require_relative "provider/hipchat/hipchat_provider.rb"
2017-07-25 17:14:48 -04:00
require_relative "provider/mattermost/mattermost_provider.rb"
2017-07-27 12:09:44 -04:00
require_relative "provider/matrix/matrix_provider.rb"