Update helper to deal with the new channel structure
This commit is contained in:
parent
246c81ce5c
commit
e850fb194b
|
@ -2,8 +2,9 @@ module DiscourseChat
|
||||||
module Helper
|
module Helper
|
||||||
|
|
||||||
# Produce a string with a list of all rules associated with a channel
|
# Produce a string with a list of all rules associated with a channel
|
||||||
def self.status_for_channel(provider, channel)
|
def self.status_for_channel(channel)
|
||||||
rules = DiscourseChat::Rule.with_channel(provider, channel)
|
rules = channel.rules
|
||||||
|
provider = channel.provider
|
||||||
|
|
||||||
text = I18n.t("chat_integration.provider.#{provider}.status.header") + "\n"
|
text = I18n.t("chat_integration.provider.#{provider}.status.header") + "\n"
|
||||||
|
|
||||||
|
@ -43,8 +44,8 @@ module DiscourseChat
|
||||||
|
|
||||||
# Delete a rule based on its (1 based) index as seen in the
|
# Delete a rule based on its (1 based) index as seen in the
|
||||||
# status_for_channel function
|
# status_for_channel function
|
||||||
def self.delete_by_index(provider, channel, index)
|
def self.delete_by_index(channel, index)
|
||||||
rules = DiscourseChat::Rule.with_channel(provider, channel)
|
rules = DiscourseChat::Rule.with_channel(channel)
|
||||||
|
|
||||||
return false if index < 1 or index > rules.size
|
return false if index < 1 or index > rules.size
|
||||||
|
|
||||||
|
@ -58,8 +59,8 @@ module DiscourseChat
|
||||||
# :updated if an existing rule has been updated
|
# :updated if an existing rule has been updated
|
||||||
# :created if a new rule has been created
|
# :created if a new rule has been created
|
||||||
# false if there was an error
|
# false if there was an error
|
||||||
def self.smart_create_rule(provider:, channel:, filter:, category_id:, tags:)
|
def self.smart_create_rule(channel:, filter:, category_id:nil, tags:nil)
|
||||||
existing_rules = DiscourseChat::Rule.with_channel(provider, channel)
|
existing_rules = DiscourseChat::Rule.with_channel(channel)
|
||||||
|
|
||||||
# Select the ones that have the same category
|
# Select the ones that have the same category
|
||||||
same_category = existing_rules.select { |rule| rule.category_id == category_id }
|
same_category = existing_rules.select { |rule| rule.category_id == category_id }
|
||||||
|
@ -100,7 +101,7 @@ module DiscourseChat
|
||||||
end
|
end
|
||||||
|
|
||||||
# This rule is unique! Create a new one:
|
# This rule is unique! Create a new one:
|
||||||
return :created if Rule.new({provider: provider, channel: channel, filter: filter, category_id: category_id, tags: tags}).save
|
return :created if Rule.new(channel: channel, filter: filter, category_id: category_id, tags: tags).save
|
||||||
|
|
||||||
return false # Error
|
return false # Error
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
require_relative '../dummy_provider'
|
||||||
|
|
||||||
RSpec.describe DiscourseChat::Manager do
|
RSpec.describe DiscourseChat::Manager do
|
||||||
|
include_context "dummy provider"
|
||||||
|
|
||||||
|
let(:chan1){DiscourseChat::Channel.create!(provider:'dummy')}
|
||||||
|
let(:chan2){DiscourseChat::Channel.create!(provider:'dummy')}
|
||||||
|
|
||||||
|
let(:category) {Fabricate(:category)}
|
||||||
|
|
||||||
let(:category) {Fabricate(:category)}
|
let(:category) {Fabricate(:category)}
|
||||||
let(:tag1){Fabricate(:tag)}
|
let(:tag1){Fabricate(:tag)}
|
||||||
|
@ -11,45 +18,36 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
|
|
||||||
context 'with no rules' do
|
context 'with no rules' do
|
||||||
it 'includes the heading' do
|
it 'includes the heading' do
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
string = DiscourseChat::Helper.status_for_channel(chan1)
|
||||||
expect(string).to include('Rules for this channel')
|
expect(string).to include('dummy.status.header')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'includes the no_rules string' do
|
it 'includes the no_rules string' do
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
string = DiscourseChat::Helper.status_for_channel(chan1)
|
||||||
expect(string).to include('no rules')
|
expect(string).to include('dummy.status.no_rules')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with some rules' do
|
context 'with some rules' do
|
||||||
before do
|
before do
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'watch', category_id:category.id, tags:nil}).save!
|
DiscourseChat::Rule.create!(channel: chan1, filter:'watch', category_id:category.id, tags:nil)
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'mute', category_id:nil, tags:nil}).save!
|
DiscourseChat::Rule.create!(channel: chan1, filter:'mute', category_id:nil, tags:nil)
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'follow', category_id:nil, tags:[tag1.name]}).save!
|
DiscourseChat::Rule.create!(channel: chan1, filter:'follow', category_id:nil, tags:[tag1.name])
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#otherchannel', filter:'watch', category_id:1, tags:nil}).save!
|
DiscourseChat::Rule.create!(channel: chan2, filter:'watch', category_id:1, tags:nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'displays the correct rules' do
|
it 'displays the correct rules' do
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
string = DiscourseChat::Helper.status_for_channel(chan1)
|
||||||
expect(string.scan('watch').size).to eq(1)
|
expect(string.scan('status.rule_string').size).to eq(3)
|
||||||
expect(string.scan('mute').size).to eq(1)
|
|
||||||
expect(string.scan('follow').size).to eq(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'enumerates the rules correctly' do
|
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
|
||||||
expect(string.scan('1)').size).to eq(1)
|
|
||||||
expect(string.scan('2)').size).to eq(1)
|
|
||||||
expect(string.scan('3)').size).to eq(1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'only displays tags for rules with tags' do
|
it 'only displays tags for rules with tags' do
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
string = DiscourseChat::Helper.status_for_channel(chan1)
|
||||||
expect(string.scan('with tags').size).to eq(0)
|
expect(string.scan('rule_string_tags_suffix').size).to eq(0)
|
||||||
|
|
||||||
SiteSetting.tagging_enabled = true
|
SiteSetting.tagging_enabled = true
|
||||||
string = DiscourseChat::Helper.status_for_channel('slack','#general')
|
string = DiscourseChat::Helper.status_for_channel(chan1)
|
||||||
expect(string.scan('with tags').size).to eq(1)
|
expect(string.scan('rule_string_tags_suffix').size).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -64,46 +62,42 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
# Three identical rules, with different categories
|
# Three identical rules, with different categories
|
||||||
# Status will be sorted by category id, so they should
|
# Status will be sorted by category id, so they should
|
||||||
# be in this order
|
# be in this order
|
||||||
rule1 = DiscourseChat::Rule.new({provider: 'slack',
|
rule1 = DiscourseChat::Rule.create(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
}).save!
|
)
|
||||||
rule2 = DiscourseChat::Rule.new({provider: 'slack',
|
rule2 = DiscourseChat::Rule.create(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category2.id,
|
category_id: category2.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
}).save!
|
)
|
||||||
rule3 = DiscourseChat::Rule.new({provider: 'slack',
|
rule3 = DiscourseChat::Rule.create(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category3.id,
|
category_id: category3.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
}).save!
|
)
|
||||||
|
|
||||||
expect(DiscourseChat::Rule.all.size).to eq(3)
|
expect(DiscourseChat::Rule.all.size).to eq(3)
|
||||||
|
|
||||||
expect(DiscourseChat::Helper.delete_by_index('slack','#general',2)).to eq(:deleted)
|
expect(DiscourseChat::Helper.delete_by_index(chan1,2)).to eq(:deleted)
|
||||||
|
|
||||||
expect(DiscourseChat::Rule.all.size).to eq(2)
|
expect(DiscourseChat::Rule.all.size).to eq(2)
|
||||||
expect(DiscourseChat::Rule.all.map(&:category_id)).to contain_exactly(category.id, category3.id)
|
expect(DiscourseChat::Rule.all.map(&:category_id)).to contain_exactly(category.id, category3.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'fails gracefully for out of range indexes' do
|
it 'fails gracefully for out of range indexes' do
|
||||||
rule1 = DiscourseChat::Rule.new({provider: 'slack',
|
rule1 = DiscourseChat::Rule.create(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
}).save!
|
)
|
||||||
|
|
||||||
expect(DiscourseChat::Helper.delete_by_index('slack','#general',-1)).to eq(false)
|
expect(DiscourseChat::Helper.delete_by_index(chan1,-1)).to eq(false)
|
||||||
expect(DiscourseChat::Helper.delete_by_index('slack','#general',0)).to eq(false)
|
expect(DiscourseChat::Helper.delete_by_index(chan1,0)).to eq(false)
|
||||||
expect(DiscourseChat::Helper.delete_by_index('slack','#general',2)).to eq(false)
|
expect(DiscourseChat::Helper.delete_by_index(chan1,2)).to eq(false)
|
||||||
|
|
||||||
expect(DiscourseChat::Helper.delete_by_index('slack','#general',1)).to eq(:deleted)
|
expect(DiscourseChat::Helper.delete_by_index(chan1,1)).to eq(:deleted)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,8 +106,7 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
describe '.smart_create_rule' do
|
describe '.smart_create_rule' do
|
||||||
|
|
||||||
it 'creates a rule when there are none' do
|
it 'creates a rule when there are none' do
|
||||||
val = DiscourseChat::Helper.smart_create_rule(provider: 'slack',
|
val = DiscourseChat::Helper.smart_create_rule(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name]
|
tags: [tag1.name]
|
||||||
|
@ -121,23 +114,20 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
expect(val).to eq(:created)
|
expect(val).to eq(:created)
|
||||||
|
|
||||||
record = DiscourseChat::Rule.all.first
|
record = DiscourseChat::Rule.all.first
|
||||||
expect(record.provider).to eq('slack')
|
expect(record.channel).to eq(chan1)
|
||||||
expect(record.channel).to eq('#general')
|
|
||||||
expect(record.filter).to eq('watch')
|
expect(record.filter).to eq('watch')
|
||||||
expect(record.category_id).to eq(category.id)
|
expect(record.category_id).to eq(category.id)
|
||||||
expect(record.tags).to eq([tag1.name])
|
expect(record.tags).to eq([tag1.name])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'updates a rule when it has the same category and tags' do
|
it 'updates a rule when it has the same category and tags' do
|
||||||
existing = DiscourseChat::Rule.new({provider: 'slack',
|
existing = DiscourseChat::Rule.create!(channel:chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag2.name, tag1.name]
|
tags: [tag2.name, tag1.name]
|
||||||
}).save!
|
)
|
||||||
|
|
||||||
val = DiscourseChat::Helper.smart_create_rule(provider: 'slack',
|
val = DiscourseChat::Helper.smart_create_rule(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'mute',
|
filter: 'mute',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
|
@ -150,15 +140,13 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'updates a rule when it has the same category and filter' do
|
it 'updates a rule when it has the same category and filter' do
|
||||||
existing = DiscourseChat::Rule.new({provider: 'slack',
|
existing = DiscourseChat::Rule.create(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name, tag2.name]
|
tags: [tag1.name, tag2.name]
|
||||||
}).save!
|
)
|
||||||
|
|
||||||
val = DiscourseChat::Helper.smart_create_rule(provider: 'slack',
|
val = DiscourseChat::Helper.smart_create_rule(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: category.id,
|
category_id: category.id,
|
||||||
tags: [tag1.name, tag3.name]
|
tags: [tag1.name, tag3.name]
|
||||||
|
@ -171,11 +159,10 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'destroys duplicate rules on save' do
|
it 'destroys duplicate rules on save' do
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter: 'watch'}).save!
|
DiscourseChat::Rule.create!(channel: chan1, filter: 'watch')
|
||||||
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter: 'watch'}).save!
|
DiscourseChat::Rule.create!(channel: chan1, filter: 'watch')
|
||||||
expect(DiscourseChat::Rule.all.size).to eq(2)
|
expect(DiscourseChat::Rule.all.size).to eq(2)
|
||||||
val = DiscourseChat::Helper.smart_create_rule(provider: 'slack',
|
val = DiscourseChat::Helper.smart_create_rule(channel: chan1,
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
filter: 'watch',
|
||||||
category_id: nil,
|
category_id: nil,
|
||||||
tags: nil
|
tags: nil
|
||||||
|
@ -185,12 +172,7 @@ RSpec.describe DiscourseChat::Manager do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns false on error' do
|
it 'returns false on error' do
|
||||||
val = DiscourseChat::Helper.smart_create_rule(provider: 'nonexistantprovider',
|
val = DiscourseChat::Helper.smart_create_rule(channel: chan1, filter: 'blah')
|
||||||
channel: '#general',
|
|
||||||
filter: 'watch',
|
|
||||||
category_id: nil,
|
|
||||||
tags: nil
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(val).to eq(false)
|
expect(val).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue