Make CHANNEL_PARAMETERS a list

This commit is contained in:
David Taylor 2017-07-17 17:53:32 +01:00
parent ebb6fa947d
commit 1ef9073027
5 changed files with 11 additions and 7 deletions

View File

@ -9,7 +9,7 @@ class DiscourseChat::ChatController < ApplicationController
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| {
name: x::PROVIDER_NAME,
id: x::PROVIDER_NAME,
channel_regex: (defined? x::PROVIDER_CHANNEL_REGEX) ? x::PROVIDER_CHANNEL_REGEX : nil
channel_parameters: (defined? x::CHANNEL_PARAMETERS) ? x::CHANNEL_PARAMETERS : []
}}
render json:providers, root: 'providers'

View File

@ -26,13 +26,13 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
params = ::DiscourseChat::Provider.get_by_name(provider)::CHANNEL_PARAMETERS
unless params.keys.sort == data.keys.sort
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
data.each do |key, value|
regex_string = params[key]
regex_string = params.find{|p| p[:key] == key}[:regex]
if !Regexp.new(regex_string).match?(value)
errors.add(:data, "data.#{key} is invalid")
end

View File

@ -3,7 +3,9 @@ module DiscourseChat::Provider::SlackProvider
PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled
CHANNEL_PARAMETERS = {"identifier" => '^[@#]\S*$'}
CHANNEL_PARAMETERS = [
{key: "identifier", regex: '^[@#]\S*$'}
]
def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length)
doc = Nokogiri::HTML.fragment(post.excerpt(max_length,

View File

@ -3,7 +3,7 @@ module DiscourseChat
module TelegramProvider
PROVIDER_NAME = "telegram".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled
CHANNEL_PARAMETERS = {}
CHANNEL_PARAMETERS = []
end
end

View File

@ -8,7 +8,7 @@ RSpec.shared_context "dummy provider" do
module ::DiscourseChat::Provider::DummyProvider
PROVIDER_NAME = "dummy".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting
CHANNEL_PARAMETERS = {}
CHANNEL_PARAMETERS = []
@@sent_messages = []
@@raise_exception = nil
@ -47,7 +47,9 @@ RSpec.shared_context "validated dummy provider" do
module ::DiscourseChat::Provider::Dummy2Provider
PROVIDER_NAME = "dummy2".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting
CHANNEL_PARAMETERS = {"val" => '\S+'}
CHANNEL_PARAMETERS = [
{key: "val", regex: '\S+'}
]
@@sent_messages = []