2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
class DiscourseChatIntegration::ChatController < ApplicationController
|
|
|
|
requires_plugin DiscourseChatIntegration::PLUGIN_NAME
|
2017-07-13 08:32:11 -04:00
|
|
|
|
|
|
|
def respond
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_providers
|
2022-12-29 07:31:05 -05:00
|
|
|
providers =
|
|
|
|
::DiscourseChatIntegration::Provider.enabled_providers.map do |x|
|
|
|
|
{
|
|
|
|
name: x::PROVIDER_NAME,
|
|
|
|
id: x::PROVIDER_NAME,
|
|
|
|
channel_parameters: (defined?(x::CHANNEL_PARAMETERS)) ? x::CHANNEL_PARAMETERS : [],
|
|
|
|
}
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
render json: providers, root: "providers"
|
2017-07-13 08:32:11 -04:00
|
|
|
end
|
|
|
|
|
2017-07-18 11:17:03 -04:00
|
|
|
def test
|
2017-07-13 08:32:11 -04:00
|
|
|
begin
|
2017-07-18 11:17:03 -04:00
|
|
|
channel_id = params[:channel_id].to_i
|
|
|
|
topic_id = params[:topic_id].to_i
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.find(channel_id)
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
provider = ::DiscourseChatIntegration::Provider.get_by_name(channel.provider)
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::NotFound if !DiscourseChatIntegration::Provider.is_enabled(provider)
|
2017-07-13 08:32:11 -04:00
|
|
|
|
|
|
|
post = Topic.find(topic_id.to_i).posts.first
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
provider.trigger_notification(post, channel, nil)
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: success_json
|
2017-07-13 08:32:11 -04:00
|
|
|
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: { errors: [e.message] }, status: 422
|
2021-07-13 15:36:16 -04:00
|
|
|
rescue DiscourseChatIntegration::ProviderError => e
|
2017-07-19 11:28:02 -04:00
|
|
|
Rails.logger.error("Test provider failed #{e.info}")
|
2017-08-01 15:53:39 -04:00
|
|
|
if e.info.key?(:error_key) && !e.info[:error_key].nil?
|
|
|
|
render json: { error_key: e.info[:error_key] }, status: 422
|
|
|
|
else
|
|
|
|
render json: { errors: [e.message] }, status: 422
|
2017-07-13 08:32:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-18 11:17:03 -04:00
|
|
|
def list_channels
|
2021-07-13 15:36:16 -04:00
|
|
|
providers = ::DiscourseChatIntegration::Provider.enabled_provider_names
|
2017-07-18 11:17:03 -04:00
|
|
|
requested_provider = params[:provider]
|
|
|
|
|
2017-10-03 05:42:07 -04:00
|
|
|
raise Discourse::InvalidParameters if !providers.include?(requested_provider)
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channels = DiscourseChatIntegration::Channel.with_provider(requested_provider)
|
2022-12-29 07:31:05 -05:00
|
|
|
render_serialized channels, DiscourseChatIntegration::ChannelSerializer, root: "channels"
|
2017-07-18 11:17:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_channel
|
|
|
|
begin
|
2022-12-29 07:31:05 -05:00
|
|
|
providers =
|
|
|
|
::DiscourseChatIntegration::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2017-10-03 05:42:07 -04:00
|
|
|
if !defined?(params[:channel]) && defined?(params[:channel][:provider])
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Provider is not valid"
|
2017-07-18 13:23:20 -04:00
|
|
|
end
|
|
|
|
|
2017-07-18 11:17:03 -04:00
|
|
|
requested_provider = params[:channel][:provider]
|
|
|
|
|
2017-10-03 05:42:07 -04:00
|
|
|
if !providers.include?(requested_provider)
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Provider is not valid"
|
2017-07-18 11:17:03 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
allowed_keys =
|
|
|
|
DiscourseChatIntegration::Provider.get_by_name(
|
|
|
|
requested_provider,
|
|
|
|
)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
hash = params.require(:channel).permit(:provider, data: allowed_keys)
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.new(hash)
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Channel is not valid" if !channel.save
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
render_serialized channel, DiscourseChatIntegration::ChannelSerializer, root: "channel"
|
2017-07-18 11:17:03 -04:00
|
|
|
rescue Discourse::InvalidParameters => e
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: { errors: [e.message] }, status: 422
|
2017-07-18 11:17:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_channel
|
|
|
|
begin
|
2021-07-13 15:36:16 -04:00
|
|
|
channel = DiscourseChatIntegration::Channel.find(params[:id].to_i)
|
2017-07-18 18:08:06 -04:00
|
|
|
channel.error_key = nil # Reset any error on the rule
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
allowed_keys =
|
|
|
|
DiscourseChatIntegration::Provider.get_by_name(
|
|
|
|
channel.provider,
|
|
|
|
)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
|
2017-08-01 15:53:39 -04:00
|
|
|
|
|
|
|
hash = params.require(:channel).permit(data: allowed_keys)
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Channel is not valid" if !channel.update(hash)
|
2017-07-18 11:17:03 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
render_serialized channel, DiscourseChatIntegration::ChannelSerializer, root: "channel"
|
2017-07-18 11:17:03 -04:00
|
|
|
rescue Discourse::InvalidParameters => e
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: { errors: [e.message] }, status: 422
|
2017-07-18 11:17:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_channel
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Channel.find_by(id: params[:id])
|
2017-10-03 05:42:07 -04:00
|
|
|
raise Discourse::InvalidParameters unless rule
|
|
|
|
rule.destroy!
|
2017-07-18 11:17:03 -04:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2017-07-13 08:32:11 -04:00
|
|
|
def create_rule
|
|
|
|
begin
|
2022-12-29 07:31:05 -05:00
|
|
|
hash =
|
|
|
|
params.require(:rule).permit(:channel_id, :type, :filter, :group_id, :category_id, tags: [])
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.new(hash)
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Rule is not valid" if !rule.save
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
render_serialized rule, DiscourseChatIntegration::RuleSerializer, root: "rule"
|
2017-07-13 08:32:11 -04:00
|
|
|
rescue Discourse::InvalidParameters => e
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: { errors: [e.message] }, status: 422
|
2017-07-13 08:32:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_rule
|
|
|
|
begin
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.find(params[:id].to_i)
|
2017-08-01 15:53:39 -04:00
|
|
|
hash = params.require(:rule).permit(:type, :filter, :group_id, :category_id, tags: [])
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
raise Discourse::InvalidParameters, "Rule is not valid" if !rule.update(hash)
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
render_serialized rule, DiscourseChatIntegration::RuleSerializer, root: "rule"
|
2017-07-13 08:32:11 -04:00
|
|
|
rescue Discourse::InvalidParameters => e
|
2017-08-01 15:53:39 -04:00
|
|
|
render json: { errors: [e.message] }, status: 422
|
2017-07-13 08:32:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_rule
|
2021-07-13 15:36:16 -04:00
|
|
|
rule = DiscourseChatIntegration::Rule.find_by(id: params[:id])
|
2017-10-03 05:42:07 -04:00
|
|
|
raise Discourse::InvalidParameters.new unless rule
|
|
|
|
rule.destroy!
|
2017-07-13 08:32:11 -04:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|