2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
require "rails_helper"
|
|
|
|
require_dependency "post_creator"
|
|
|
|
require_relative "../dummy_provider"
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
RSpec.describe DiscourseChatIntegration::Manager do
|
|
|
|
let(:manager) { ::DiscourseChatIntegration::Manager }
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2017-08-01 17:15:27 -04:00
|
|
|
let(:group2) { Fabricate(:group) }
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:topic) { Fabricate(:topic, category_id: category.id) }
|
|
|
|
let(:first_post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:second_post) { Fabricate(:post, topic: topic, post_number: 2) }
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe ".trigger_notifications" do
|
2022-10-03 15:36:52 -04:00
|
|
|
include_context "with dummy provider"
|
2017-07-10 13:19:59 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
let(:chan1) { DiscourseChatIntegration::Channel.create!(provider: "dummy") }
|
|
|
|
let(:chan2) { DiscourseChatIntegration::Channel.create!(provider: "dummy") }
|
|
|
|
let(:chan3) { DiscourseChatIntegration::Channel.create!(provider: "dummy") }
|
2017-07-13 16:47:15 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
before { SiteSetting.chat_integration_enabled = true }
|
2017-07-03 10:53:26 -04:00
|
|
|
|
2017-07-04 14:37:56 -04:00
|
|
|
it "should fail gracefully when a provider throws an exception" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2017-07-04 14:37:56 -04:00
|
|
|
|
|
|
|
# Triggering a ProviderError should set the error_key to the error message
|
2022-12-29 07:31:05 -05:00
|
|
|
provider.set_raise_exception(
|
|
|
|
DiscourseChatIntegration::ProviderError.new info: { error_key: "hello" }
|
|
|
|
)
|
2017-07-04 14:37:56 -04:00
|
|
|
manager.trigger_notifications(first_post.id)
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
|
|
|
expect(DiscourseChatIntegration::Channel.all.first.error_key).to eq("hello")
|
2017-07-04 14:37:56 -04:00
|
|
|
|
|
|
|
# Triggering a different error should set the error_key to a generic message
|
2017-07-13 16:47:15 -04:00
|
|
|
provider.set_raise_exception(StandardError.new "hello")
|
2017-07-04 14:37:56 -04:00
|
|
|
manager.trigger_notifications(first_post.id)
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
|
|
|
expect(DiscourseChatIntegration::Channel.all.first.error_key).to eq(
|
|
|
|
"chat_integration.channel_exception",
|
|
|
|
)
|
2017-07-04 14:37:56 -04:00
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
provider.set_raise_exception(nil)
|
2017-07-04 14:37:56 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
2021-07-13 15:36:16 -04:00
|
|
|
expect(DiscourseChatIntegration::Channel.all.first.error_key.nil?).to be true
|
2017-07-04 14:37:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not send notifications when provider is disabled" do
|
2017-07-03 10:53:26 -04:00
|
|
|
SiteSetting.chat_integration_enabled = false
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2017-07-03 10:53:26 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
2017-07-03 10:53:26 -04:00
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
it "should send a notification to watched and following channels for new topic" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
filter: "follow",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan3,
|
|
|
|
filter: "mute",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan2.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should send a notification only to watched for reply" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
filter: "follow",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan3,
|
|
|
|
filter: "mute",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(second_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respect wildcard category settings" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "watch", category_id: nil)
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respect mute over watch" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "watch", category_id: nil) # Wildcard watch
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "mute",
|
|
|
|
category_id: category.id,
|
|
|
|
) # Specific mute
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respect watch over follow" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "follow", category_id: nil) # Wildcard follow
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
category_id: category.id,
|
|
|
|
) # Specific watch
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(second_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
it "should respect thread over watch" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "watch", category_id: nil) # Wildcard watch
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "thread",
|
|
|
|
category_id: category.id,
|
|
|
|
) # Specific thread
|
2020-06-15 11:45:25 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(second_post.id)
|
|
|
|
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
it "should not notify about private messages" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "follow", category_id: nil) # Wildcard watch
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
private_post = Fabricate(:private_message_post)
|
|
|
|
|
|
|
|
manager.trigger_notifications(private_post.id)
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2017-07-28 15:27:49 -04:00
|
|
|
it "should work for group pms" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "watch") # Wildcard watch
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
type: "group_message",
|
|
|
|
filter: "watch",
|
|
|
|
group_id: group.id,
|
|
|
|
) # Group watch
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-07-28 15:27:49 -04:00
|
|
|
private_post = Fabricate(:private_message_post)
|
|
|
|
private_post.topic.invite_group(Fabricate(:user), group)
|
|
|
|
|
|
|
|
manager.trigger_notifications(private_post.id)
|
|
|
|
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should work for pms with multiple groups" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
type: "group_message",
|
|
|
|
filter: "watch",
|
|
|
|
group_id: group.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
type: "group_message",
|
|
|
|
filter: "watch",
|
|
|
|
group_id: group2.id,
|
|
|
|
)
|
2017-07-28 15:27:49 -04:00
|
|
|
|
|
|
|
private_post = Fabricate(:private_message_post)
|
|
|
|
private_post.topic.invite_group(Fabricate(:user), group)
|
|
|
|
private_post.topic.invite_group(Fabricate(:user), group2)
|
|
|
|
|
|
|
|
manager.trigger_notifications(private_post.id)
|
|
|
|
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan2.id)
|
|
|
|
end
|
|
|
|
|
2017-08-01 12:11:34 -04:00
|
|
|
it "should work for group mentions" do
|
2022-12-29 07:31:05 -05:00
|
|
|
third_post =
|
|
|
|
Fabricate(:post, topic: topic, post_number: 3, raw: "let's mention @#{group.name}")
|
|
|
|
|
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "watch") # Wildcard watch
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
type: "group_message",
|
|
|
|
filter: "watch",
|
|
|
|
group_id: group.id,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan3,
|
|
|
|
type: "group_mention",
|
|
|
|
filter: "watch",
|
|
|
|
group_id: group.id,
|
|
|
|
)
|
2017-08-01 12:11:34 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(third_post.id)
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan3.id)
|
|
|
|
end
|
|
|
|
|
2017-08-01 16:09:01 -04:00
|
|
|
it "should give group rule precedence over normal rules" do
|
2022-12-29 07:31:05 -05:00
|
|
|
third_post =
|
|
|
|
Fabricate(:post, topic: topic, post_number: 3, raw: "let's mention @#{group.name}")
|
|
|
|
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "mute",
|
|
|
|
category_id: category.id,
|
|
|
|
) # Mute category
|
2017-08-01 16:09:01 -04:00
|
|
|
manager.trigger_notifications(third_post.id)
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
|
|
|
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
type: "group_mention",
|
|
|
|
group_id: group.id,
|
|
|
|
) # Watch mentions
|
2017-08-01 16:09:01 -04:00
|
|
|
manager.trigger_notifications(third_post.id)
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
|
|
|
end
|
|
|
|
|
2017-08-01 17:15:27 -04:00
|
|
|
it "should not notify about mentions in private messages" do
|
|
|
|
# Group 1 watching for messages on channel 1
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "watch",
|
|
|
|
type: "group_message",
|
|
|
|
group_id: group.id,
|
|
|
|
)
|
2017-08-01 17:15:27 -04:00
|
|
|
# Group 2 watching for mentions on channel 2
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan2,
|
|
|
|
filter: "watch",
|
|
|
|
type: "group_mention",
|
|
|
|
group_id: group2.id,
|
|
|
|
)
|
2017-08-01 17:15:27 -04:00
|
|
|
|
|
|
|
# Make a private message only accessible to group 1
|
|
|
|
private_message = Fabricate(:private_message_post)
|
|
|
|
private_message.topic.invite_group(Fabricate(:user), group)
|
|
|
|
|
|
|
|
# Mention group 2 in the message
|
2022-12-29 07:31:05 -05:00
|
|
|
mention_post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: private_message.topic,
|
|
|
|
post_number: 2,
|
|
|
|
raw: "let's mention @#{group2.name}",
|
|
|
|
)
|
2017-08-01 17:15:27 -04:00
|
|
|
|
|
|
|
# We expect that only group 1 receives a notification
|
|
|
|
manager.trigger_notifications(mention_post.id)
|
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
it "should not notify about posts the chat_user cannot see" do
|
2022-12-29 07:31:05 -05:00
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "follow", category_id: nil) # Wildcard watch
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
# Create a group & user
|
|
|
|
group = Fabricate(:group, name: "friends")
|
2022-12-29 07:31:05 -05:00
|
|
|
user = Fabricate(:user, username: "david")
|
2017-06-27 14:21:27 -04:00
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
# Set the chat_user to the newly created non-admin user
|
2022-12-29 07:31:05 -05:00
|
|
|
SiteSetting.chat_integration_discourse_username = "david"
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
# Create a category
|
|
|
|
category = Fabricate(:category, name: "Test category")
|
|
|
|
topic.category = category
|
|
|
|
topic.save!
|
|
|
|
|
|
|
|
# Restrict category to admins only
|
|
|
|
category.set_permissions(Group[:admins] => :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
# Check no notification sent
|
|
|
|
manager.trigger_notifications(first_post.id)
|
2022-12-29 07:31:05 -05:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
# Now expose category to new user
|
|
|
|
category.set_permissions(Group[:friends] => :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
# Check notification sent
|
|
|
|
manager.trigger_notifications(first_post.id)
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
describe "with tags enabled" do
|
|
|
|
let(:tag) { Fabricate(:tag, name: "gsoc") }
|
2017-08-01 15:53:39 -04:00
|
|
|
let(:tagged_topic) { Fabricate(:topic, category_id: category.id, tags: [tag]) }
|
|
|
|
let(:tagged_first_post) { Fabricate(:post, topic: tagged_topic) }
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
before(:each) { SiteSetting.tagging_enabled = true }
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should still work for rules without any tags specified" do
|
|
|
|
DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "follow", category_id: nil) # Wildcard watch
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
manager.trigger_notifications(tagged_first_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id, chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
it "should only match tagged topics when rule has tags" do
|
|
|
|
DiscourseChatIntegration::Rule.create!(
|
|
|
|
channel: chan1,
|
|
|
|
filter: "follow",
|
|
|
|
category_id: category.id,
|
|
|
|
tags: [tag.name],
|
|
|
|
)
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
manager.trigger_notifications(first_post.id)
|
|
|
|
manager.trigger_notifications(tagged_first_post.id)
|
|
|
|
|
2017-07-13 16:47:15 -04:00
|
|
|
expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id)
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|