Add support for group mentions

This commit is contained in:
David Taylor 2017-08-01 17:11:34 +01:00
parent 209daf7801
commit f3347b0a92
4 changed files with 37 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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