From 6af31bb2448cfde07c99ea342f330b1b852077fb Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 1 Aug 2017 21:09:01 +0100 Subject: [PATCH] Fix precedence for group-based rules --- app/services/manager.rb | 7 ++++--- spec/services/manager_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/services/manager.rb b/app/services/manager.rb index ad39b98..253560b 100644 --- a/app/services/manager.rb +++ b/app/services/manager.rb @@ -52,9 +52,10 @@ module DiscourseChat end end - # Sort by order of precedence (mute always wins; watch beats follow) - precedence = { 'mute' => 0, 'watch' => 1, 'follow' => 2 } - sort_func = proc { |a, b| precedence[a.filter] <=> precedence[b.filter] } + # Sort by order of precedence + t_prec = { 'group_message' => 0, 'group_mention' => 1, 'normal' => 2 } # Group things win + f_prec = { 'mute' => 0, 'watch' => 1, 'follow' => 2 } #(mute always wins; watch beats follow) + sort_func = proc { |a, b| [t_prec[a.type], f_prec[a.filter]] <=> [t_prec[b.type], f_prec[b.filter]] } matching_rules = matching_rules.sort(&sort_func) # Take the first rule for each channel diff --git a/spec/services/manager_spec.rb b/spec/services/manager_spec.rb index 5d0ef91..bd75d55 100644 --- a/spec/services/manager_spec.rb +++ b/spec/services/manager_spec.rb @@ -145,6 +145,20 @@ RSpec.describe DiscourseChat::Manager do expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan3.id) end + + + it "should give group rule precedence over normal rules" do + third_post = Fabricate(:post, topic: topic, post_number: 3, raw: "let's mention @#{group.name}") + + DiscourseChat::Rule.create!(channel: chan1, filter: 'mute', category_id: category.id) # Mute category + manager.trigger_notifications(third_post.id) + expect(provider.sent_to_channel_ids).to contain_exactly() + + DiscourseChat::Rule.create!(channel: chan1, filter: 'watch', type: 'group_mention', group_id: group.id) # Watch mentions + manager.trigger_notifications(third_post.id) + expect(provider.sent_to_channel_ids).to contain_exactly(chan1.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