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-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
|
|
|
|
|
|
|
class Engine < ::Rails::Engine
|
|
|
|
engine_name DiscourseChat::PLUGIN_NAME
|
|
|
|
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-06-27 14:21:27 -04:00
|
|
|
|
2017-06-30 06:10:11 -04:00
|
|
|
module ::Jobs
|
|
|
|
class NotifyChats < Jobs::Base
|
|
|
|
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
|
|
|
|
providers = ::DiscourseChat::Provider.providers.map {|x| {name: x::PROVIDER_NAME, id: x::PROVIDER_NAME}}
|
|
|
|
render json:providers, root: 'providers'
|
|
|
|
end
|
2017-06-28 10:12:37 -04:00
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
def list_rules
|
2017-06-28 10:12:37 -04:00
|
|
|
providers = ::DiscourseChat::Provider.providers.map {|x| x::PROVIDER_NAME}
|
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
requested_provider = params[:provider]
|
2017-06-28 10:12:37 -04:00
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
if requested_provider.nil?
|
2017-06-29 12:50:54 -04:00
|
|
|
rules = DiscourseChat::Rule.all
|
2017-06-28 17:32:02 -04:00
|
|
|
elsif providers.include? requested_provider
|
2017-06-29 12:50:54 -04:00
|
|
|
rules = DiscourseChat::Rule.all_for_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
|
|
|
|
rule = DiscourseChat::Rule.new()
|
|
|
|
hash = params.require(:rule)
|
|
|
|
|
|
|
|
rule.update(hash)
|
|
|
|
|
|
|
|
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
|
|
|
|
end
|
|
|
|
|
2017-06-29 12:50:54 -04:00
|
|
|
def update_rule
|
2017-06-29 15:19:40 -04:00
|
|
|
rule = DiscourseChat::Rule.find(params[:id].to_i)
|
|
|
|
hash = params.require(:rule)
|
|
|
|
|
|
|
|
rule.update(hash)
|
|
|
|
|
|
|
|
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
|
|
|
|
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
|
|
|
|
attributes :id, :provider, :channel, :category_id, :tags, :filter
|
|
|
|
end
|
2017-06-28 17:32:02 -04:00
|
|
|
|
2017-06-26 14:19:50 -04:00
|
|
|
require_dependency 'admin_constraint'
|
|
|
|
|
|
|
|
|
|
|
|
add_admin_route 'chat.menu_title', 'chat'
|
|
|
|
|
|
|
|
DiscourseChat::Engine.routes.draw do
|
2017-06-28 17:32:02 -04:00
|
|
|
get "" => "chat#respond"
|
|
|
|
get '/providers' => "chat#list_providers"
|
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-06-28 10:12:37 -04:00
|
|
|
mount ::DiscourseChat::Engine, at: '/admin/plugins/chat', constraints: AdminConstraint.new
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2017-06-26 10:08:06 -04:00
|
|
|
end
|