Skip to content

Commit

Permalink
Rename DiscourseChat to DiscourseChatIntegration (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanlan committed Jul 13, 2021
1 parent c0bd802 commit a73f5da
Show file tree
Hide file tree
Showing 64 changed files with 482 additions and 482 deletions.
46 changes: 23 additions & 23 deletions app/controllers/chat_controller.rb
@@ -1,14 +1,14 @@
# frozen_string_literal: true

class DiscourseChat::ChatController < ApplicationController
requires_plugin DiscourseChat::PLUGIN_NAME
class DiscourseChatIntegration::ChatController < ApplicationController
requires_plugin DiscourseChatIntegration::PLUGIN_NAME

def respond
render
end

def list_providers
providers = ::DiscourseChat::Provider.enabled_providers.map do |x|
providers = ::DiscourseChatIntegration::Provider.enabled_providers.map do |x|
{
name: x::PROVIDER_NAME,
id: x::PROVIDER_NAME,
Expand All @@ -24,11 +24,11 @@ def test
channel_id = params[:channel_id].to_i
topic_id = params[:topic_id].to_i

channel = DiscourseChat::Channel.find(channel_id)
channel = DiscourseChatIntegration::Channel.find(channel_id)

provider = ::DiscourseChat::Provider.get_by_name(channel.provider)
provider = ::DiscourseChatIntegration::Provider.get_by_name(channel.provider)

if !DiscourseChat::Provider.is_enabled(provider)
if !DiscourseChatIntegration::Provider.is_enabled(provider)
raise Discourse::NotFound
end

Expand All @@ -39,7 +39,7 @@ def test
render json: success_json
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
render json: { errors: [e.message] }, status: 422
rescue DiscourseChat::ProviderError => e
rescue DiscourseChatIntegration::ProviderError => e
Rails.logger.error("Test provider failed #{e.info}")
if e.info.key?(:error_key) && !e.info[:error_key].nil?
render json: { error_key: e.info[:error_key] }, status: 422
Expand All @@ -50,18 +50,18 @@ def test
end

def list_channels
providers = ::DiscourseChat::Provider.enabled_provider_names
providers = ::DiscourseChatIntegration::Provider.enabled_provider_names
requested_provider = params[:provider]

raise Discourse::InvalidParameters if !providers.include?(requested_provider)

channels = DiscourseChat::Channel.with_provider(requested_provider)
render_serialized channels, DiscourseChat::ChannelSerializer, root: 'channels'
channels = DiscourseChatIntegration::Channel.with_provider(requested_provider)
render_serialized channels, DiscourseChatIntegration::ChannelSerializer, root: 'channels'
end

def create_channel
begin
providers = ::DiscourseChat::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
providers = ::DiscourseChatIntegration::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }

if !defined?(params[:channel]) && defined?(params[:channel][:provider])
raise Discourse::InvalidParameters, 'Provider is not valid'
Expand All @@ -73,43 +73,43 @@ def create_channel
raise Discourse::InvalidParameters, 'Provider is not valid'
end

allowed_keys = DiscourseChat::Provider.get_by_name(requested_provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
allowed_keys = DiscourseChatIntegration::Provider.get_by_name(requested_provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }

hash = params.require(:channel).permit(:provider, data: allowed_keys)

channel = DiscourseChat::Channel.new(hash)
channel = DiscourseChatIntegration::Channel.new(hash)

if !channel.save
raise Discourse::InvalidParameters, 'Channel is not valid'
end

render_serialized channel, DiscourseChat::ChannelSerializer, root: 'channel'
render_serialized channel, DiscourseChatIntegration::ChannelSerializer, root: 'channel'
rescue Discourse::InvalidParameters => e
render json: { errors: [e.message] }, status: 422
end
end

def update_channel
begin
channel = DiscourseChat::Channel.find(params[:id].to_i)
channel = DiscourseChatIntegration::Channel.find(params[:id].to_i)
channel.error_key = nil # Reset any error on the rule

allowed_keys = DiscourseChat::Provider.get_by_name(channel.provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }
allowed_keys = DiscourseChatIntegration::Provider.get_by_name(channel.provider)::CHANNEL_PARAMETERS.map { |p| p[:key].to_sym }

hash = params.require(:channel).permit(data: allowed_keys)

if !channel.update(hash)
raise Discourse::InvalidParameters, 'Channel is not valid'
end

render_serialized channel, DiscourseChat::ChannelSerializer, root: 'channel'
render_serialized channel, DiscourseChatIntegration::ChannelSerializer, root: 'channel'
rescue Discourse::InvalidParameters => e
render json: { errors: [e.message] }, status: 422
end
end

def destroy_channel
rule = DiscourseChat::Channel.find_by(id: params[:id])
rule = DiscourseChatIntegration::Channel.find_by(id: params[:id])
raise Discourse::InvalidParameters unless rule
rule.destroy!

Expand All @@ -119,35 +119,35 @@ def destroy_channel
def create_rule
begin
hash = params.require(:rule).permit(:channel_id, :type, :filter, :group_id, :category_id, tags: [])
rule = DiscourseChat::Rule.new(hash)
rule = DiscourseChatIntegration::Rule.new(hash)

if !rule.save
raise Discourse::InvalidParameters, 'Rule is not valid'
end

render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
render_serialized rule, DiscourseChatIntegration::RuleSerializer, root: 'rule'
rescue Discourse::InvalidParameters => e
render json: { errors: [e.message] }, status: 422
end
end

def update_rule
begin
rule = DiscourseChat::Rule.find(params[:id].to_i)
rule = DiscourseChatIntegration::Rule.find(params[:id].to_i)
hash = params.require(:rule).permit(:type, :filter, :group_id, :category_id, tags: [])

if !rule.update(hash)
raise Discourse::InvalidParameters, 'Rule is not valid'
end

render_serialized rule, DiscourseChat::RuleSerializer, root: 'rule'
render_serialized rule, DiscourseChatIntegration::RuleSerializer, root: 'rule'
rescue Discourse::InvalidParameters => e
render json: { errors: [e.message] }, status: 422
end
end

def destroy_rule
rule = DiscourseChat::Rule.find_by(id: params[:id])
rule = DiscourseChatIntegration::Rule.find_by(id: params[:id])
raise Discourse::InvalidParameters.new unless rule
rule.destroy!

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/public_controller.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class DiscourseChat::PublicController < ApplicationController
requires_plugin DiscourseChat::PLUGIN_NAME
class DiscourseChatIntegration::PublicController < ApplicationController
requires_plugin DiscourseChatIntegration::PLUGIN_NAME

def post_transcript
params.require(:secret)
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/helper.rb
@@ -1,10 +1,10 @@
# frozen_string_literal: true

module DiscourseChat
module DiscourseChatIntegration
module Helper

def self.process_command(channel, tokens)
guardian = DiscourseChat::Manager.guardian
guardian = DiscourseChatIntegration::Manager.guardian

provider = channel.provider

Expand Down Expand Up @@ -47,7 +47,7 @@ def self.process_command(channel, tokens)
end

category_id = category.nil? ? nil : category.id
case DiscourseChat::Helper.smart_create_rule(channel: channel, filter: cmd, category_id: category_id, tags: tags)
case DiscourseChatIntegration::Helper.smart_create_rule(channel: channel, filter: cmd, category_id: category_id, tags: tags)
when :created
I18n.t("chat_integration.provider.#{provider}.create.created")
when :updated
Expand All @@ -61,13 +61,13 @@ def self.process_command(channel, tokens)
rule_number = tokens[0].to_i
return error_text unless rule_number.to_s == tokens[0] # Check we were given a number

if DiscourseChat::Helper.delete_by_index(channel, rule_number)
if DiscourseChatIntegration::Helper.delete_by_index(channel, rule_number)
I18n.t("chat_integration.provider.#{provider}.delete.success")
else
I18n.t("chat_integration.provider.#{provider}.delete.error")
end
when "status"
DiscourseChat::Helper.status_for_channel(channel)
DiscourseChatIntegration::Helper.status_for_channel(channel)
when "help"
I18n.t("chat_integration.provider.#{provider}.help")
else
Expand Down Expand Up @@ -144,7 +144,7 @@ def self.delete_by_index(channel, index)
# :created if a new rule has been created
# false if there was an error
def self.smart_create_rule(channel:, filter:, category_id: nil, tags: nil)
existing_rules = DiscourseChat::Rule.with_channel(channel).with_type('normal')
existing_rules = DiscourseChatIntegration::Rule.with_channel(channel).with_type('normal')

# Select the ones that have the same category
same_category = existing_rules.select { |rule| rule.category_id == category_id }
Expand Down
@@ -1,20 +1,20 @@
# frozen_string_literal: true

module ::DiscourseChat
module ::DiscourseChatIntegration
PLUGIN_NAME = "discourse-chat-integration".freeze

class AdminEngine < ::Rails::Engine
engine_name "#{DiscourseChat::PLUGIN_NAME}-admin"
isolate_namespace DiscourseChat
engine_name "#{::DiscourseChatIntegration::PLUGIN_NAME}-admin"
isolate_namespace ::DiscourseChatIntegration
end

class PublicEngine < ::Rails::Engine
engine_name "#{DiscourseChat::PLUGIN_NAME}-public"
isolate_namespace DiscourseChat
engine_name "#{::DiscourseChatIntegration::PLUGIN_NAME}-public"
isolate_namespace ::DiscourseChatIntegration
end

def self.plugin_name
DiscourseChat::PLUGIN_NAME
::DiscourseChatIntegration::PLUGIN_NAME
end

def self.pstore_get(key)
Expand All @@ -40,7 +40,7 @@ def self.pstore_delete(key)
require_relative "../controllers/chat_controller"
require_relative "../controllers/public_controller"

require_relative "../routes/discourse_chat"
require_relative "../routes/discourse_chat_integration"
require_relative "../routes/discourse"

require_relative "../helpers/helper"
Expand All @@ -49,7 +49,7 @@ def self.pstore_delete(key)

require_relative "../jobs/regular/notify_chats"

require_relative "../../lib/discourse_chat/provider"
require_relative "../../lib/discourse_chat_integration/provider"

require_relative "../jobs/onceoff/add_type_field"
require_relative "../jobs/onceoff/migrate_from_slack_official"
2 changes: 1 addition & 1 deletion app/jobs/onceoff/add_type_field.rb
Expand Up @@ -3,7 +3,7 @@
module Jobs
class DiscourseChatAddTypeField < ::Jobs::Onceoff
def execute_onceoff(args)
DiscourseChat::Rule.find_each do |rule|
DiscourseChatIntegration::Rule.find_each do |rule|
rule.save(validate: false)
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/jobs/onceoff/migrate_from_slack_official.rb
Expand Up @@ -6,7 +6,7 @@ def execute_onceoff(args)
slack_installed = PluginStoreRow.where(plugin_name: 'discourse-slack-official').exists?

if slack_installed
already_setup_rules = DiscourseChat::Channel.with_provider('slack').exists?
already_setup_rules = DiscourseChatIntegration::Channel.with_provider('slack').exists?

already_setup_sitesettings =
SiteSetting.chat_integration_slack_enabled ||
Expand Down Expand Up @@ -65,17 +65,17 @@ def migrate_data
rows.each do |row|
# Load an existing channel with this identifier. If none, create it
row[:channel] = "##{row[:channel]}" unless row[:channel].start_with?("#")
channel = DiscourseChat::Channel.with_provider('slack').with_data_value('identifier', row[:channel]).first
channel = DiscourseChatIntegration::Channel.with_provider('slack').with_data_value('identifier', row[:channel]).first
if !channel
channel = DiscourseChat::Channel.create(provider: 'slack', data: { identifier: row[:channel] })
channel = DiscourseChatIntegration::Channel.create(provider: 'slack', data: { identifier: row[:channel] })
if !channel.id
Rails.logger.warn("Error creating channel for #{row}")
next
end
end

# Create the rule, with clever logic for avoiding duplicates
success = DiscourseChat::Helper.smart_create_rule(channel: channel, filter: row[:filter], category_id: row[:category_id], tags: row[:tags])
success = DiscourseChatIntegration::Helper.smart_create_rule(channel: channel, filter: row[:filter], category_id: row[:category_id], tags: row[:tags])
end

end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/regular/notify_chats.rb
Expand Up @@ -6,7 +6,7 @@ class NotifyChats < ::Jobs::Base

def execute(args)
return if !SiteSetting.chat_integration_enabled?
::DiscourseChat::Manager.trigger_notifications(args[:post_id])
::DiscourseChatIntegration::Manager.trigger_notifications(args[:post_id])
end
end
end
12 changes: 6 additions & 6 deletions app/models/channel.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class DiscourseChat::Channel < DiscourseChat::PluginModel
class DiscourseChatIntegration::Channel < DiscourseChatIntegration::PluginModel
# Setup ActiveRecord::Store to use the JSON field to read/write these values
store :value, accessors: [ :provider, :error_key, :error_info, :data ], coder: JSON

Expand All @@ -17,7 +17,7 @@ def self.key_prefix
end

def rules
DiscourseChat::Rule.with_channel_id(id).order_by_precedence
DiscourseChatIntegration::Rule.with_channel_id(id).order_by_precedence
end

private
Expand All @@ -31,24 +31,24 @@ def destroy_rules
end

def provider_valid?
if !DiscourseChat::Provider.provider_names.include?(provider)
if !DiscourseChatIntegration::Provider.provider_names.include?(provider)
errors.add(:provider, "#{provider} is not a valid provider")
end
end

def data_valid?
# If provider is invalid, don't try and check data
return unless ::DiscourseChat::Provider.provider_names.include? provider
return unless ::DiscourseChatIntegration::Provider.provider_names.include? provider

params = ::DiscourseChat::Provider.get_by_name(provider)::CHANNEL_PARAMETERS
params = ::DiscourseChatIntegration::Provider.get_by_name(provider)::CHANNEL_PARAMETERS

unless params.map { |p| p[:key] }.sort == data.keys.sort
errors.add(:data, "data does not match the required structure for provider #{provider}")
return
end

check_unique = false
matching_channels = DiscourseChat::Channel.with_provider(provider).where.not(id: id)
matching_channels = DiscourseChatIntegration::Channel.with_provider(provider).where.not(id: id)

data.each do |key, value|
regex_string = params.find { |p| p[:key] == key }[:regex]
Expand Down
2 changes: 1 addition & 1 deletion app/models/plugin_model.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class DiscourseChat::PluginModel < PluginStoreRow
class DiscourseChatIntegration::PluginModel < PluginStoreRow
PLUGIN_NAME = 'discourse-chat-integration'

default_scope { self.default_scope }
Expand Down
6 changes: 3 additions & 3 deletions app/models/rule.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class DiscourseChat::Rule < DiscourseChat::PluginModel
class DiscourseChatIntegration::Rule < DiscourseChatIntegration::PluginModel
# Setup ActiveRecord::Store to use the JSON field to read/write these values
store :value, accessors: [ :channel_id, :type, :group_id, :category_id, :tags, :filter ], coder: JSON

Expand Down Expand Up @@ -75,7 +75,7 @@ def tags=(array)
# Mock foreign key
# Could return nil
def channel
DiscourseChat::Channel.find_by(id: channel_id)
DiscourseChatIntegration::Channel.find_by(id: channel_id)
end

def channel=(val)
Expand All @@ -85,7 +85,7 @@ def channel=(val)
private

def channel_valid?
if !(DiscourseChat::Channel.where(id: channel_id).exists?)
if !(DiscourseChatIntegration::Channel.where(id: channel_id).exists?)
errors.add(:channel_id, "#{channel_id} is not a valid channel id")
end
end
Expand Down

0 comments on commit a73f5da

Please sign in to comment.