2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration
|
2017-07-04 14:37:56 -04:00
|
|
|
class ProviderError < StandardError
|
|
|
|
attr_accessor :info
|
|
|
|
|
|
|
|
def initialize(message = nil, info: nil)
|
|
|
|
super(message)
|
|
|
|
self.info = info.nil? ? {} : info
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
module Provider
|
|
|
|
def self.providers
|
|
|
|
constants.select do |constant|
|
|
|
|
constant.to_s =~ /Provider$/
|
|
|
|
end.map(&method(:const_get))
|
|
|
|
end
|
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
def self.enabled_providers
|
|
|
|
self.providers.select do |provider|
|
|
|
|
self.is_enabled(provider)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-12 13:28:45 -04:00
|
|
|
def self.provider_names
|
2017-10-03 05:42:07 -04:00
|
|
|
self.providers.map! { |x| x::PROVIDER_NAME }
|
2017-07-12 13:28:45 -04:00
|
|
|
end
|
|
|
|
|
2017-07-05 10:03:02 -04:00
|
|
|
def self.enabled_provider_names
|
2017-10-03 05:42:07 -04:00
|
|
|
self.enabled_providers.map! { |x| x::PROVIDER_NAME }
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
def self.get_by_name(name)
|
2017-08-01 15:53:39 -04:00
|
|
|
self.providers.find { |p| p::PROVIDER_NAME == name }
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
def self.is_enabled(provider)
|
2017-08-01 15:53:39 -04:00
|
|
|
if defined? provider::PROVIDER_ENABLED_SETTING
|
2019-05-15 20:24:17 -04:00
|
|
|
SiteSetting.public_send(provider::PROVIDER_ENABLED_SETTING)
|
2017-07-03 10:53:26 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-05 10:03:02 -04:00
|
|
|
class HookEngine < ::Rails::Engine
|
2021-07-13 15:36:16 -04:00
|
|
|
engine_name DiscourseChatIntegration::PLUGIN_NAME + "-hooks"
|
|
|
|
isolate_namespace DiscourseChatIntegration::Provider
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class HookController < ::ApplicationController
|
2021-07-13 15:36:16 -04:00
|
|
|
requires_plugin DiscourseChatIntegration::PLUGIN_NAME
|
2017-07-05 10:03:02 -04:00
|
|
|
|
|
|
|
class ProviderDisabled < StandardError; end
|
|
|
|
|
|
|
|
rescue_from ProviderDisabled do
|
|
|
|
rescue_discourse_actions(:not_found, 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.requires_provider(provider_name)
|
2017-09-03 22:44:32 -04:00
|
|
|
before_action do
|
2017-07-05 10:03:02 -04:00
|
|
|
raise ProviderDisabled.new unless Provider.enabled_provider_names.include?(provider_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond
|
2017-08-01 15:53:39 -04:00
|
|
|
render
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Automatically mount each provider's engine inside the HookEngine
|
|
|
|
def self.mount_engines
|
|
|
|
engines = []
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Provider.providers.each do |provider|
|
2017-07-05 10:03:02 -04:00
|
|
|
engine = provider.constants.select do |constant|
|
2017-10-03 05:42:07 -04:00
|
|
|
constant.to_s =~ (/Engine$/) && (constant.to_s != "HookEngine")
|
2017-07-05 10:03:02 -04:00
|
|
|
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
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Provider::HookEngine.routes.draw do
|
2017-07-05 10:03:02 -04:00
|
|
|
engines.each do |engine|
|
|
|
|
mount engine[:engine], at: engine[:name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-10 01:01:46 -04:00
|
|
|
require_relative "provider/slack/slack_provider"
|
|
|
|
require_relative "provider/telegram/telegram_provider"
|
|
|
|
require_relative "provider/discord/discord_provider"
|
|
|
|
require_relative "provider/mattermost/mattermost_provider"
|
|
|
|
require_relative "provider/matrix/matrix_provider"
|
|
|
|
require_relative "provider/zulip/zulip_provider"
|
|
|
|
require_relative "provider/rocketchat/rocketchat_provider"
|
|
|
|
require_relative "provider/gitter/gitter_provider"
|
2017-10-16 10:44:21 -04:00
|
|
|
require_relative "provider/flowdock/flowdock_provider"
|
2020-05-11 15:10:37 -04:00
|
|
|
require_relative "provider/groupme/groupme_provider"
|
2020-07-29 12:20:02 -04:00
|
|
|
require_relative "provider/teams/teams_provider"
|
2020-12-11 04:43:03 -05:00
|
|
|
require_relative "provider/webex/webex_provider"
|
2021-01-26 15:33:03 -05:00
|
|
|
require_relative "provider/google/google_provider"
|