discourse-chat-integration/app/controllers/chat_controller.rb

157 lines
4.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class DiscourseChat::ChatController < ApplicationController
requires_plugin DiscourseChat::PLUGIN_NAME
def respond
render
end
def list_providers
2017-09-24 23:06:27 -04:00
providers = ::DiscourseChat::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
render json: providers, root: 'providers'
end
def test
begin
channel_id = params[:channel_id].to_i
topic_id = params[:topic_id].to_i
channel = DiscourseChat::Channel.find(channel_id)
provider = ::DiscourseChat::Provider.get_by_name(channel.provider)
2017-10-03 05:42:07 -04:00
if !DiscourseChat::Provider.is_enabled(provider)
raise Discourse::NotFound
end
post = Topic.find(topic_id.to_i).posts.first
provider.trigger_notification(post, channel, nil)
2017-08-01 15:53:39 -04:00
render json: success_json
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
2017-08-01 15:53:39 -04:00
render json: { errors: [e.message] }, status: 422
rescue DiscourseChat::ProviderError => e
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
end
end
end
def list_channels
2017-10-03 05:42:07 -04:00
providers = ::DiscourseChat::Provider.enabled_provider_names
requested_provider = params[:provider]
2017-10-03 05:42:07 -04:00
raise Discourse::InvalidParameters if !providers.include?(requested_provider)
channels = DiscourseChat::Channel.with_provider(requested_provider)
render_serialized channels, DiscourseChat::ChannelSerializer, root: 'channels'
end
def create_channel
begin
2017-08-01 15:53:39 -04:00
providers = ::DiscourseChat::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
2017-10-03 05:42:07 -04:00
if !defined?(params[:channel]) && defined?(params[:channel][:provider])
raise Discourse::InvalidParameters, 'Provider is not valid'
end
requested_provider = params[:channel][:provider]
2017-10-03 05:42:07 -04:00
if !providers.include?(requested_provider)
raise Discourse::InvalidParameters, 'Provider is not valid'
end
2017-08-01 15:53:39 -04:00
allowed_keys = DiscourseChat::Provider.get_by_name(requested_provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
2017-08-01 15:53:39 -04:00
hash = params.require(:channel).permit(:provider, data: allowed_keys)
channel = DiscourseChat::Channel.new(hash)
2017-08-01 15:53:39 -04:00
if !channel.save
raise Discourse::InvalidParameters, 'Channel is not valid'
end
render_serialized channel, DiscourseChat::ChannelSerializer, root: 'channel'
rescue Discourse::InvalidParameters => e
2017-08-01 15:53:39 -04:00
render json: { errors: [e.message] }, status: 422
end
end
def update_channel
begin
channel = DiscourseChat::Channel.find(params[:id].to_i)
channel.error_key = nil # Reset any error on the rule
2017-08-01 15:53:39 -04:00
allowed_keys = DiscourseChat::Provider.get_by_name(channel.provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
hash = params.require(:channel).permit(data: allowed_keys)
2017-10-03 05:42:07 -04:00
if !channel.update(hash)
raise Discourse::InvalidParameters, 'Channel is not valid'
end
render_serialized channel, DiscourseChat::ChannelSerializer, root: 'channel'
rescue Discourse::InvalidParameters => e
2017-08-01 15:53:39 -04:00
render json: { errors: [e.message] }, status: 422
end
end
def destroy_channel
2017-10-03 05:42:07 -04:00
rule = DiscourseChat::Channel.find_by(id: params[:id])
raise Discourse::InvalidParameters unless rule
rule.destroy!
render json: success_json
end
def create_rule
begin
2017-08-01 15:53:39 -04:00
hash = params.require(:rule).permit(:channel_id, :type, :filter, :group_id, :category_id, tags: [])
rule = DiscourseChat::Rule.new(hash)
2017-08-01 15:53:39 -04:00
if !rule.save
raise Discourse::InvalidParameters, 'Rule is not valid'
end
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
rescue Discourse::InvalidParameters => e
2017-08-01 15:53:39 -04:00
render json: { errors: [e.message] }, status: 422
end
end
def update_rule
begin
rule = DiscourseChat::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: [])
2017-10-03 05:42:07 -04:00
if !rule.update(hash)
raise Discourse::InvalidParameters, 'Rule is not valid'
end
render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
rescue Discourse::InvalidParameters => e
2017-08-01 15:53:39 -04:00
render json: { errors: [e.message] }, status: 422
end
end
def destroy_rule
2017-10-03 05:42:07 -04:00
rule = DiscourseChat::Rule.find_by(id: params[:id])
raise Discourse::InvalidParameters.new unless rule
rule.destroy!
render json: success_json
end
2017-08-01 15:53:39 -04:00
end