2017-06-26 10:08:06 -04:00
|
|
|
# name: discourse-chat
|
|
|
|
# about: This plugin integrates discourse with a number of chat providers
|
|
|
|
# version: 0.1
|
|
|
|
# url: https://github.com/discourse/discourse-chat
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
enabled_site_setting :chat_enabled
|
2017-06-26 10:08:06 -04:00
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
|
|
|
|
module ::DiscourseChat
|
|
|
|
PLUGIN_NAME = "discourse-chat".freeze
|
|
|
|
|
|
|
|
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-27 14:21:27 -04:00
|
|
|
require_relative "lib/provider"
|
|
|
|
require_relative "lib/manager"
|
2017-06-29 12:01:04 -04:00
|
|
|
require_relative "lib/rule"
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
DiscourseEvent.on(:post_created) do |post|
|
|
|
|
if SiteSetting.chat_enabled?
|
|
|
|
::DiscourseChat::Manager.trigger_notifications(post.id)
|
|
|
|
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?
|
|
|
|
rules = DiscourseChat::Manager.get_all_rules()
|
|
|
|
elsif providers.include? requested_provider
|
|
|
|
rules = DiscourseChat::Manager.get_rules_for_provider(requested_provider)
|
|
|
|
else
|
2017-06-28 10:12:37 -04:00
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
|
2017-06-28 17:32:02 -04:00
|
|
|
render json: rules, root: 'rules'
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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"
|
|
|
|
get '/rules' => "chat#list_rules"
|
|
|
|
|
|
|
|
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
|