From f3347b0a92c4877d2e7395c8e549855a0480fe93 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 1 Aug 2017 17:11:34 +0100 Subject: [PATCH] Add support for group mentions --- app/models/rule.rb | 3 ++- app/services/manager.rb | 13 +++++++++++-- spec/models/rule_spec.rb | 13 +++++++++++++ spec/services/manager_spec.rb | 11 +++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/models/rule.rb b/app/models/rule.rb index 8668888..ca1bbfb 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -14,7 +14,7 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel validates :filter, :inclusion => { :in => %w(watch follow mute), :message => "%{value} is not a valid filter" } - validates :type, :inclusion => { :in => %w(normal group_message), + validates :type, :inclusion => { :in => %w(normal group_message group_mention), :message => "%{value} is not a valid filter" } validate :channel_valid?, :category_valid?, :group_valid?, :tags_valid? @@ -80,6 +80,7 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel super(val.to_i) end end + end # Mock foreign key diff --git a/app/services/manager.rb b/app/services/manager.rb index 11bd537..8c6ac82 100644 --- a/app/services/manager.rb +++ b/app/services/manager.rb @@ -27,9 +27,18 @@ module DiscourseChat return if group_ids_with_access.empty? matching_rules = DiscourseChat::Rule.with_type('group_message').with_group_ids(group_ids_with_access) else - matching_rules = DiscourseChat::Rule.with_category_id(topic.category_id) + matching_rules = DiscourseChat::Rule.with_type('normal').with_category_id(topic.category_id) if topic.category # Also load the rules for the wildcard category - matching_rules += DiscourseChat::Rule.with_category_id(nil) + matching_rules += DiscourseChat::Rule.with_type('normal').with_category_id(nil) + end + end + + # If groups are mentioned, check for any matching rules and append them + mentions = post.raw_mentions + if mentions && mentions.length > 0 + groups = Group.where('LOWER(name) IN (?)', mentions) + if groups.exists? + matching_rules += DiscourseChat::Rule.with_type('group_mention').with_group_ids(groups.map(&:id)) end end diff --git a/spec/models/rule_spec.rb b/spec/models/rule_spec.rb index b698383..83efb13 100644 --- a/spec/models/rule_spec.rb +++ b/spec/models/rule_spec.rb @@ -127,6 +127,19 @@ RSpec.describe DiscourseChat::Rule do expect(DiscourseChat::Rule.with_group_ids([group2.id]).length).to eq(1) end + it 'can be filtered by type' do + group1 = Fabricate(:group) + + rule2 = DiscourseChat::Rule.create!(channel: channel, type: 'group_message', group_id: group1.id) + rule3 = DiscourseChat::Rule.create!(channel: channel, type: 'group_mention', group_id: group1.id) + + expect(DiscourseChat::Rule.all.length).to eq(3) + + expect(DiscourseChat::Rule.with_type('group_message').length).to eq(1) + expect(DiscourseChat::Rule.with_type('group_mention').length).to eq(1) + expect(DiscourseChat::Rule.with_type('normal').length).to eq(1) + end + it 'can be sorted by precedence' do rule2 = DiscourseChat::Rule.create(channel:channel, filter:'mute') rule3 = DiscourseChat::Rule.create(channel:channel, filter:'follow') diff --git a/spec/services/manager_spec.rb b/spec/services/manager_spec.rb index c06ea46..c7ba534 100644 --- a/spec/services/manager_spec.rb +++ b/spec/services/manager_spec.rb @@ -134,6 +134,17 @@ RSpec.describe DiscourseChat::Manager do expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan2.id) end + it "should work for group mentions" do + third_post = Fabricate(:post, topic: topic, post_number: 3, raw: "let's mention @#{group.name}") + + DiscourseChat::Rule.create!(channel: chan1, filter: 'watch') # Wildcard watch + DiscourseChat::Rule.create!(channel: chan2, type: 'group_message', filter: 'watch', group_id: group.id) + DiscourseChat::Rule.create!(channel: chan3, type: 'group_mention', filter: 'watch', group_id: group.id) + + manager.trigger_notifications(third_post.id) + expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan3.id) + end + it "should not notify about posts the chat_user cannot see" do DiscourseChat::Rule.create!(channel: chan1, filter: 'follow', category_id: nil ) # Wildcard watch