diff --git a/app/models/channel.rb b/app/models/channel.rb index ca97194..9269373 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -36,12 +36,26 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel return end + check_unique = false + matching_channels = DiscourseChat::Channel.all + data.each do |key, value| regex_string = params.find{|p| p[:key] == key}[:regex] if !Regexp.new(regex_string).match?(value) errors.add(:data, "data.#{key} is invalid") end + + unique = params.find{|p| p[:key] == key}[:unique] + if unique + check_unique = true + matching_channels = matching_channels.with_data_value(key, value) + end end + + if check_unique && matching_channels.exists? + errors.add(:data, "matches an existing channel") + end + end def rules diff --git a/lib/discourse_chat/provider/discord/discord_provider.rb b/lib/discourse_chat/provider/discord/discord_provider.rb index a91e33e..950bed4 100644 --- a/lib/discourse_chat/provider/discord/discord_provider.rb +++ b/lib/discourse_chat/provider/discord/discord_provider.rb @@ -5,7 +5,7 @@ module DiscourseChat PROVIDER_ENABLED_SETTING = :chat_integration_discord_enabled CHANNEL_PARAMETERS = [ {key: "name", regex: '^\S+'}, - {key: "webhook_url", regex: '^https:\/\/discordapp\.com\/api\/webhooks\/', hidden: true} + {key: "webhook_url", regex: '^https:\/\/discordapp\.com\/api\/webhooks\/', unique: true, hidden: true} ] def self.send_message(url, message) diff --git a/lib/discourse_chat/provider/hipchat/hipchat_provider.rb b/lib/discourse_chat/provider/hipchat/hipchat_provider.rb index 97b6af1..4e12ac1 100644 --- a/lib/discourse_chat/provider/hipchat/hipchat_provider.rb +++ b/lib/discourse_chat/provider/hipchat/hipchat_provider.rb @@ -5,7 +5,7 @@ module DiscourseChat PROVIDER_ENABLED_SETTING = :chat_integration_hipchat_enabled CHANNEL_PARAMETERS = [ {key: "name", regex: '^\S+'}, - {key: "webhook_url", regex: 'hipchat\.com', hidden:true}, + {key: "webhook_url", regex: 'hipchat\.com', unique: true, hidden:true}, {key: "color", regex: '(yellow|green|red|purple|gray|random)'} ] diff --git a/lib/discourse_chat/provider/matrix/matrix_provider.rb b/lib/discourse_chat/provider/matrix/matrix_provider.rb index f4478ee..a2e1853 100644 --- a/lib/discourse_chat/provider/matrix/matrix_provider.rb +++ b/lib/discourse_chat/provider/matrix/matrix_provider.rb @@ -5,7 +5,7 @@ module DiscourseChat PROVIDER_ENABLED_SETTING = :chat_integration_matrix_enabled CHANNEL_PARAMETERS = [ {key: "name", regex: '^\S+'}, - {key: "room_id", regex: '^\!\S+:\S+$', hidden:true} + {key: "room_id", regex: '^\!\S+:\S+$', unique: true, hidden:true} ] def self.send_message(room_id, message) diff --git a/lib/discourse_chat/provider/mattermost/mattermost_provider.rb b/lib/discourse_chat/provider/mattermost/mattermost_provider.rb index c3d5292..e64a1ce 100644 --- a/lib/discourse_chat/provider/mattermost/mattermost_provider.rb +++ b/lib/discourse_chat/provider/mattermost/mattermost_provider.rb @@ -4,7 +4,7 @@ module DiscourseChat PROVIDER_NAME = "mattermost".freeze PROVIDER_ENABLED_SETTING = :chat_integration_mattermost_enabled CHANNEL_PARAMETERS = [ - {key: "identifier", regex: '^[@#]\S*$'} + {key: "identifier", regex: '^[@#]\S*$', unique: true} ] def self.send_via_webhook(message) diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index 23405a6..e161733 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -4,7 +4,7 @@ module DiscourseChat::Provider::SlackProvider PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled CHANNEL_PARAMETERS = [ - {key: "identifier", regex: '^[@#]\S*$'} + {key: "identifier", regex: '^[@#]\S*$', unique: true} ] def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length) diff --git a/lib/discourse_chat/provider/telegram/telegram_provider.rb b/lib/discourse_chat/provider/telegram/telegram_provider.rb index 2dfb4a8..d77a5e8 100644 --- a/lib/discourse_chat/provider/telegram/telegram_provider.rb +++ b/lib/discourse_chat/provider/telegram/telegram_provider.rb @@ -5,7 +5,7 @@ module DiscourseChat PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled CHANNEL_PARAMETERS = [ {key: "name", regex: '^\S+'}, - {key: "chat_id", regex: '^(-?[0-9]+|@\S+)$'} + {key: "chat_id", regex: '^(-?[0-9]+|@\S+)$', unique: true} ] def self.setup_webhook diff --git a/spec/dummy_provider.rb b/spec/dummy_provider.rb index f16da02..14f9b7c 100644 --- a/spec/dummy_provider.rb +++ b/spec/dummy_provider.rb @@ -48,7 +48,7 @@ RSpec.shared_context "validated dummy provider" do PROVIDER_NAME = "dummy2".freeze PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting CHANNEL_PARAMETERS = [ - {key: "val", regex: '^\S+$'} + {key: "val", regex: '^\S+$', unique: true} ] @@sent_messages = [] diff --git a/spec/models/channel_spec.rb b/spec/models/channel_spec.rb index 12147e2..5078a94 100644 --- a/spec/models/channel_spec.rb +++ b/spec/models/channel_spec.rb @@ -24,7 +24,7 @@ RSpec.describe DiscourseChat::Channel do it 'can be filtered by provider' do channel1 = DiscourseChat::Channel.create!(provider:'dummy') channel2 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah"}) - channel3 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah"}) + channel3 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah2"}) expect(DiscourseChat::Channel.all.length).to eq(3) @@ -64,7 +64,6 @@ RSpec.describe DiscourseChat::Channel do end describe 'validations' do - let(:channel) { } it 'validates provider correctly' do channel = DiscourseChat::Channel.create!(provider:"dummy") @@ -93,5 +92,14 @@ RSpec.describe DiscourseChat::Channel do expect(channel2.valid?).to eq(false) end + it 'disallows duplicate channels' do + channel1 = DiscourseChat::Channel.create(provider:"dummy2", data:{val:"hello"}) + channel2 = DiscourseChat::Channel.new(provider:"dummy2", data:{val:"hello"}) + expect(channel2.valid?).to eq(false) + channel2.data[:val] = "hello2" + expect(channel2.valid?).to eq(true) + end + + end end \ No newline at end of file