2017-07-03 06:08:14 -04:00
|
|
|
# name: discourse-chat-integration
|
2017-06-26 10:08:06 -04:00
|
|
|
# about: This plugin integrates discourse with a number of chat providers
|
|
|
|
# version: 0.1
|
2017-07-03 06:08:14 -04:00
|
|
|
# url: https://github.com/discourse/discourse-chat-integration
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-03 06:08:14 -04:00
|
|
|
enabled_site_setting :chat_integration_enabled
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-03 17:11:17 -04:00
|
|
|
register_asset "stylesheets/chat-integration-admin.scss"
|
|
|
|
|
2017-07-03 11:28:26 -04:00
|
|
|
# Site setting validators must be loaded before initialize
|
|
|
|
require_relative "lib/validators/chat_integration_slack_enabled_setting_validator"
|
2017-06-29 12:50:54 -04:00
|
|
|
|
2017-06-26 10:08:06 -04:00
|
|
|
after_initialize do
|
|
|
|
|
|
|
|
module ::DiscourseChat
|
2017-07-03 06:08:14 -04:00
|
|
|
PLUGIN_NAME = "discourse-chat-integration".freeze
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-05 10:03:02 -04:00
|
|
|
class AdminEngine < ::Rails::Engine
|
|
|
|
engine_name DiscourseChat::PLUGIN_NAME+"-admin"
|
2017-06-26 10:08:06 -04:00
|
|
|
isolate_namespace DiscourseChat
|
|
|
|
end
|
2017-06-29 12:01:04 -04:00
|
|
|
|
|
|
|
def self.plugin_name
|
|
|
|
DiscourseChat::PLUGIN_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_get(key)
|
|
|
|
PluginStore.get(self.plugin_name, key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_set(key, value)
|
|
|
|
PluginStore.set(self.plugin_name, key, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_delete(key)
|
|
|
|
PluginStore.remove(self.plugin_name, key)
|
|
|
|
end
|
2017-06-26 10:08:06 -04:00
|
|
|
end
|
2017-06-26 14:19:50 -04:00
|
|
|
|
2017-06-30 06:10:11 -04:00
|
|
|
require_relative "lib/discourse_chat/provider"
|
|
|
|
require_relative "lib/discourse_chat/manager"
|
|
|
|
require_relative "lib/discourse_chat/rule"
|
2017-07-05 18:00:58 -04:00
|
|
|
require_relative "lib/discourse_chat/helper"
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2017-06-30 06:10:11 -04:00
|
|
|
module ::Jobs
|
|
|
|
class NotifyChats < Jobs::Base
|
2017-07-04 14:37:56 -04:00
|
|
|
sidekiq_options retry: false # Don't retry, could result in duplicate notifications for some providers
|
2017-06-30 06:10:11 -04:00
|
|
|
def execute(args)
|
2017-07-03 06:08:14 -04:00
|
|
|
return if not SiteSetting.chat_integration_enabled? # Plugin may have been disabled since job triggered
|
2017-06-30 06:10:11 -04:00
|
|
|
|
|
|
|
::DiscourseChat::Manager.trigger_notifications(args[:post_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseEvent.on(:post_created) do |post|
|
2017-07-03 06:08:14 -04:00
|
|
|
if SiteSetting.chat_integration_enabled?
|
2017-06-30 06:10:11 -04:00
|
|
|
# This will run for every post, even PMs. Don't worry, they're filtered out later.
|
2017-07-03 06:08:14 -04:00
|
|
|
Jobs.enqueue_in(SiteSetting.chat_integration_delay_seconds.seconds,
|
2017-06-30 06:10:11 -04:00
|
|
|
:notify_chats,
|
|
|
|
post_id: post.id
|
|
|
|
)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-26 14:19:50 -04:00
|
|
|
|
|
|
|
class ::DiscourseChat::ChatController < ::ApplicationController
|
|
|
|
requires_plugin DiscourseChat::PLUGIN_NAME
|
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
def respond
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_providers
|
2017-07-03 19:14:01 -04:00
|
|
|
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| {
|
|
|
|
name: x::PROVIDER_NAME,
|
|
|
|
id: x::PROVIDER_NAME,
|
|
|
|
channel_regex: (defined? x::PROVIDER_CHANNEL_REGEX) ? x::PROVIDER_CHANNEL_REGEX : nil
|
|
|
|
}}
|
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
render json:providers, root: 'providers'
|
|
|
|
end
|
2017-06-28 10:12:37 -04:00
|
|
|
|
2017-07-04 18:35:45 -04:00
|
|
|
def test_provider
|
|
|
|
begin
|
|
|
|
requested_provider = params[:provider]
|
|
|
|
channel = params[:channel]
|
|
|
|
topic_id = params[:topic_id]
|
|
|
|
|
|
|
|
provider = ::DiscourseChat::Provider.get_by_name(requested_provider)
|
|
|
|
|
|
|
|
if provider.nil? or not ::DiscourseChat::Provider.is_enabled(provider)
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
|
|
|
|
if defined? provider::PROVIDER_CHANNEL_REGEX
|
|
|
|
channel_regex = Regexp.new provider::PROVIDER_CHANNEL_REGEX
|
|
|
|
raise Discourse::InvalidParameters, 'Channel is not valid' if not channel_regex.match?(channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
post = Topic.find(topic_id.to_i).posts.first
|
|
|
|
|
|
|
|
provider.trigger_notification(post, channel)
|
|
|
|
|
|
|
|
render json:success_json
|
|
|
|
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
|
|
|
|
render json: {errors: [e.message]}, status: 422
|
|
|
|
rescue DiscourseChat::ProviderError => e
|
|
|
|
if e.info.key?(:error_key) and !e.info[:error_key].nil?
|
|
|
|
render json: {error_key: e.info[:error_key]}, status: 422
|
|
|
|
else
|
|
|
|
render json: {errors: [e.message]}, status: 422
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
def list_rules
|
2017-07-03 10:53:26 -04:00
|
|
|
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME}
|
2017-06-28 10:12:37 -04:00
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
requested_provider = params[:provider]
|
2017-06-28 10:12:37 -04:00
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
if providers.include? requested_provider
|
2017-07-12 13:28:45 -04:00
|
|
|
rules = DiscourseChat::Rule.with_provider(requested_provider)
|
2017-06-28 17:32:02 -04:00
|
|
|
else
|
2017-06-28 10:12:37 -04:00
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
|
2017-06-29 12:50:54 -04:00
|
|
|
render_serialized rules, DiscourseChat::RuleSerializer, root: 'rules'
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 15:19:40 -04:00
|
|
|
def create_rule
|
2017-07-03 12:38:13 -04:00
|
|
|
begin
|
2017-07-12 13:28:45 -04:00
|
|
|
hash = params.require(:rule).permit(:provider, :channel, :filter, :category_id, tags:[])
|
2017-06-29 15:19:40 -04:00
|
|
|
|
2017-07-12 13:28:45 -04:00
|
|
|
rule = DiscourseChat::Rule.new(hash)
|
|
|
|
|
|
|
|
if not rule.save(hash)
|
2017-07-03 12:38:13 -04:00
|
|
|
raise Discourse::InvalidParameters, 'Rule is not valid'
|
|
|
|
end
|
2017-06-29 15:19:40 -04:00
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
|
|
|
|
rescue Discourse::InvalidParameters => e
|
|
|
|
render json: {errors: [e.message]}, status: 422
|
|
|
|
end
|
2017-06-29 15:19:40 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 12:50:54 -04:00
|
|
|
def update_rule
|
2017-07-03 12:38:13 -04:00
|
|
|
begin
|
|
|
|
rule = DiscourseChat::Rule.find(params[:id].to_i)
|
2017-07-04 14:37:56 -04:00
|
|
|
rule.error_key = nil # Reset any error on the rule
|
2017-07-12 13:28:45 -04:00
|
|
|
hash = params.require(:rule).permit(:provider, :channel, :filter, :category_id, tags:[])
|
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
if not rule.update(hash)
|
|
|
|
raise Discourse::InvalidParameters, 'Rule is not valid'
|
|
|
|
end
|
2017-06-29 15:19:40 -04:00
|
|
|
|
2017-07-03 12:38:13 -04:00
|
|
|
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
|
|
|
|
rescue Discourse::InvalidParameters => e
|
|
|
|
render json: {errors: [e.message]}, status: 422
|
|
|
|
end
|
2017-06-29 15:19:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_rule
|
|
|
|
rule = DiscourseChat::Rule.find(params[:id].to_i)
|
|
|
|
|
|
|
|
rule.destroy
|
|
|
|
|
|
|
|
render json: success_json
|
2017-06-29 12:50:54 -04:00
|
|
|
end
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 12:50:54 -04:00
|
|
|
class DiscourseChat::RuleSerializer < ActiveModel::Serializer
|
2017-07-04 14:37:56 -04:00
|
|
|
attributes :id, :provider, :channel, :category_id, :tags, :filter, :error_key
|
2017-06-29 12:50:54 -04:00
|
|
|
end
|
2017-06-28 17:32:02 -04:00
|
|
|
|
2017-06-26 14:19:50 -04:00
|
|
|
require_dependency 'admin_constraint'
|
|
|
|
|
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
add_admin_route 'chat_integration.menu_title', 'chat'
|
2017-06-26 14:19:50 -04:00
|
|
|
|
2017-07-05 10:03:02 -04:00
|
|
|
DiscourseChat::AdminEngine.routes.draw do
|
2017-06-28 17:32:02 -04:00
|
|
|
get "" => "chat#respond"
|
|
|
|
get '/providers' => "chat#list_providers"
|
2017-07-04 18:35:45 -04:00
|
|
|
post '/test' => "chat#test_provider"
|
2017-06-29 12:50:54 -04:00
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
get '/rules' => "chat#list_rules"
|
2017-06-29 15:19:40 -04:00
|
|
|
put '/rules' => "chat#create_rule"
|
|
|
|
put '/rules/:id' => "chat#update_rule"
|
|
|
|
delete '/rules/:id' => "chat#destroy_rule"
|
2017-06-28 17:32:02 -04:00
|
|
|
|
|
|
|
get "/:provider" => "chat#respond"
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
2017-07-05 10:03:02 -04:00
|
|
|
mount ::DiscourseChat::AdminEngine, at: '/admin/plugins/chat', constraints: AdminConstraint.new
|
|
|
|
mount ::DiscourseChat::Provider::HookEngine, at: '/chat-integration/'
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2017-07-05 10:03:02 -04:00
|
|
|
DiscourseChat::Provider.mount_engines
|
|
|
|
|
2017-06-26 10:08:06 -04:00
|
|
|
end
|