2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-03-18 00:22:39 -04:00
|
|
|
|
2018-03-08 05:40:48 -05:00
|
|
|
RSpec::Matchers.define :add_notification do |user, notification_type|
|
|
|
|
match(notify_expectation_failures: true) do |actual|
|
|
|
|
notifications = user.notifications
|
|
|
|
before = notifications.count
|
|
|
|
|
|
|
|
actual.call
|
|
|
|
|
|
|
|
expect(notifications.count).to eq(before + 1), "expected 1 new notification, got #{notifications.count - before}"
|
|
|
|
|
|
|
|
last_notification_type = notifications.last.notification_type
|
|
|
|
expect(last_notification_type).to eq(Notification.types[notification_type]),
|
|
|
|
"expected notification type to be '#{notification_type}', got '#{Notification.types.key(last_notification_type)}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
match_when_negated do |actual|
|
|
|
|
expect { actual.call }.to_not change { user.notifications.where(notification_type: Notification.types[notification_type]).count }
|
|
|
|
end
|
|
|
|
|
|
|
|
supports_block_expectations
|
|
|
|
end
|
|
|
|
|
2018-04-06 17:37:29 -04:00
|
|
|
RSpec::Matchers.define_negated_matcher :not_add_notification, :add_notification
|
|
|
|
|
2014-03-18 00:22:39 -04:00
|
|
|
describe PostAlerter do
|
|
|
|
|
|
|
|
let!(:evil_trout) { Fabricate(:evil_trout) }
|
2016-02-24 09:34:40 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-03-18 00:22:39 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def create_post_with_alerts(args = {})
|
2014-03-18 00:22:39 -04:00
|
|
|
post = Fabricate(:post, args)
|
2014-03-18 21:07:48 -04:00
|
|
|
PostAlerter.post_created(post)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
2016-03-21 23:28:14 -04:00
|
|
|
context "private message" do
|
|
|
|
it "notifies for pms correctly" do
|
|
|
|
pm = Fabricate(:topic, archetype: 'private_message', category_id: nil)
|
2018-06-05 03:29:17 -04:00
|
|
|
op = Fabricate(:post, user: pm.user)
|
2016-03-21 23:28:14 -04:00
|
|
|
pm.allowed_users << pm.user
|
|
|
|
PostAlerter.post_created(op)
|
2018-06-05 03:29:17 -04:00
|
|
|
|
|
|
|
reply = Fabricate(:post, user: pm.user, topic: pm, reply_to_post_number: 1)
|
2016-03-21 23:28:14 -04:00
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
reply2 = Fabricate(:post, topic: pm, reply_to_post_number: 1)
|
2016-03-21 23:28:14 -04:00
|
|
|
PostAlerter.post_created(reply2)
|
|
|
|
|
|
|
|
# we get a green notification for a reply
|
|
|
|
expect(Notification.where(user_id: pm.user_id).pluck(:notification_type).first).to eq(Notification.types[:private_message])
|
|
|
|
|
|
|
|
TopicUser.change(pm.user_id, pm.id, notification_level: TopicUser.notification_levels[:tracking])
|
|
|
|
|
|
|
|
Notification.destroy_all
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
reply3 = Fabricate(:post, topic: pm)
|
2016-03-21 23:28:14 -04:00
|
|
|
PostAlerter.post_created(reply3)
|
|
|
|
|
|
|
|
# no notification cause we are tracking
|
|
|
|
expect(Notification.where(user_id: pm.user_id).count).to eq(0)
|
|
|
|
|
|
|
|
Notification.destroy_all
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
reply4 = Fabricate(:post, topic: pm, reply_to_post_number: 1)
|
2016-03-21 23:28:14 -04:00
|
|
|
PostAlerter.post_created(reply4)
|
|
|
|
|
|
|
|
# yes notification cause we were replied to
|
|
|
|
expect(Notification.where(user_id: pm.user_id).count).to eq(1)
|
|
|
|
|
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
pm = Fabricate(:topic, archetype: 'private_message', category_id: nil)
|
2018-06-05 03:29:17 -04:00
|
|
|
op = Fabricate(:post, user: pm.user, topic: pm)
|
2017-07-19 16:51:32 -04:00
|
|
|
user1 = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
group = Fabricate(:group, users: [user2])
|
|
|
|
pm.allowed_users << user1
|
|
|
|
pm.allowed_groups << group
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(op)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user1], op])
|
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user2], op])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2016-03-21 23:28:14 -04:00
|
|
|
end
|
|
|
|
|
2015-10-19 16:31:48 -04:00
|
|
|
context "unread" do
|
|
|
|
it "does not return whispers as unread posts" do
|
|
|
|
op = Fabricate(:post)
|
2015-11-30 01:03:47 -05:00
|
|
|
_whisper = Fabricate(:post, raw: 'this is a whisper post',
|
2017-07-27 21:20:09 -04:00
|
|
|
user: Fabricate(:admin),
|
|
|
|
topic: op.topic,
|
|
|
|
reply_to_post_number: op.post_number,
|
|
|
|
post_type: Post.types[:whisper])
|
2015-10-19 16:31:48 -04:00
|
|
|
|
|
|
|
expect(PostAlerter.new.first_unread_post(op.user, op.topic)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 00:00:14 -05:00
|
|
|
context 'edits' do
|
|
|
|
it 'notifies correctly on edits' do
|
2016-12-22 00:46:22 -05:00
|
|
|
PostActionNotifier.enable
|
2015-06-18 22:31:36 -04:00
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'I love waffles')
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
2017-07-27 21:20:09 -04:00
|
|
|
post.revise(admin, raw: 'I made a revision')
|
2015-06-18 22:31:36 -04:00
|
|
|
|
2016-03-03 00:00:14 -05:00
|
|
|
# skip this notification cause we already notified on a similar edit
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time 2.hours.from_now
|
2017-07-27 21:20:09 -04:00
|
|
|
post.revise(admin, raw: 'I made another revision')
|
2015-06-18 22:31:36 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
post.revise(Fabricate(:admin), raw: 'I made a revision')
|
2016-03-03 00:00:14 -05:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time 2.hours.from_now
|
2017-07-27 21:20:09 -04:00
|
|
|
post.revise(admin, raw: 'I made another revision')
|
2016-03-03 00:00:14 -05:00
|
|
|
|
2016-07-17 23:00:23 -04:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(3)
|
2016-03-03 00:00:14 -05:00
|
|
|
end
|
2018-04-06 17:37:29 -04:00
|
|
|
|
|
|
|
it 'notifies flaggers when flagged post gets unhidden by edit' do
|
|
|
|
post = create_post
|
|
|
|
walterwhite = Fabricate(:walter_white)
|
|
|
|
coding_horror = Fabricate(:coding_horror)
|
|
|
|
|
|
|
|
PostActionNotifier.enable
|
2019-01-03 12:03:01 -05:00
|
|
|
SiteSetting.score_required_to_hide_post = 4.0
|
2018-04-06 17:37:29 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(evil_trout, post)
|
|
|
|
PostActionCreator.spam(walterwhite, post)
|
2018-04-06 17:37:29 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it ")
|
|
|
|
}.to add_notification(evil_trout, :edited)
|
|
|
|
.and add_notification(walterwhite, :edited)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.hidden).to eq(false)
|
|
|
|
|
|
|
|
notification = walterwhite.notifications.last
|
|
|
|
expect(notification.topic_id).to eq(post.topic.id)
|
|
|
|
expect(notification.post_number).to eq(post.post_number)
|
|
|
|
expect(notification.data_hash["display_username"]).to eq(post.user.username)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.create(coding_horror, post, :spam)
|
|
|
|
PostActionCreator.create(walterwhite, post, :off_topic)
|
2018-04-06 17:37:29 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it again ")
|
|
|
|
}.to not_add_notification(evil_trout, :edited)
|
|
|
|
.and not_add_notification(coding_horror, :edited)
|
|
|
|
.and not_add_notification(walterwhite, :edited)
|
|
|
|
end
|
2016-03-03 00:00:14 -05:00
|
|
|
end
|
|
|
|
|
2014-03-18 00:22:39 -04:00
|
|
|
context 'quotes' do
|
|
|
|
|
2015-03-23 20:55:22 -04:00
|
|
|
it 'does not notify for muted users' do
|
|
|
|
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
|
|
|
|
MutedUser.create!(user_id: evil_trout.id, muted_user_id: post.user_id)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2015-03-23 20:55:22 -04:00
|
|
|
PostAlerter.post_created(post)
|
2015-04-25 11:18:35 -04:00
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
2015-03-23 20:55:22 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 07:15:34 -04:00
|
|
|
it 'does not notify for ignored users' do
|
|
|
|
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
|
|
|
|
IgnoredUser.create!(user_id: evil_trout.id, ignored_user_id: post.user_id)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
|
|
|
end
|
|
|
|
|
2014-03-18 00:22:39 -04:00
|
|
|
it 'notifies a user by username' do
|
2018-05-24 23:59:29 -04:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2018-05-24 23:59:29 -04:00
|
|
|
2.times do
|
|
|
|
create_post_with_alerts(
|
|
|
|
raw: '[quote="EvilTrout, post:1"]whatup[/quote]',
|
|
|
|
topic: topic
|
|
|
|
)
|
|
|
|
end
|
2015-04-25 11:18:35 -04:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't notify the user a second time on revision" do
|
|
|
|
p1 = create_post_with_alerts(raw: '[quote="Evil Trout, post:1"]whatup[/quote]')
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2017-07-27 21:20:09 -04:00
|
|
|
p1.revise(p1.user, raw: '[quote="Evil Trout, post:1"]whatup now?[/quote]')
|
2015-04-25 11:18:35 -04:00
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify the poster" do
|
|
|
|
topic = create_post_with_alerts.topic
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2014-03-18 00:22:39 -04:00
|
|
|
Fabricate(:post, topic: topic, user: topic.user, raw: '[quote="Bruce Wayne, post:1"]whatup[/quote]')
|
2015-04-25 11:18:35 -04:00
|
|
|
}.not_to change(topic.user.notifications, :count)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
2014-03-18 21:07:48 -04:00
|
|
|
context 'linked' do
|
2017-07-19 16:51:32 -04:00
|
|
|
let(:post1) { create_post }
|
|
|
|
let(:user) { post1.user }
|
|
|
|
let(:linking_post) { create_post(raw: "my magic topic\n##{Discourse.base_url}#{post1.url}") }
|
|
|
|
|
2018-05-31 03:53:49 -04:00
|
|
|
before do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-05-31 03:53:49 -04:00
|
|
|
end
|
|
|
|
|
2014-03-18 21:07:48 -04:00
|
|
|
it "will notify correctly on linking" do
|
2017-07-19 16:51:32 -04:00
|
|
|
linking_post
|
2014-03-18 21:07:48 -04:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.notifications.count).to eq(1)
|
2014-03-18 21:07:48 -04:00
|
|
|
|
2015-12-30 18:51:58 -05:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
watcher = Fabricate(:user)
|
|
|
|
TopicUser.create!(user_id: watcher.id, topic_id: topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
create_post(topic_id: topic.id, user: user, raw: "my magic topic\n##{Discourse.base_url}#{post1.url}")
|
2014-03-18 21:07:48 -04:00
|
|
|
|
|
|
|
user.reload
|
2016-04-12 14:09:59 -04:00
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:linked]).count).to eq(1)
|
2014-07-29 01:40:05 -04:00
|
|
|
|
2015-12-30 18:51:58 -05:00
|
|
|
expect(watcher.notifications.count).to eq(1)
|
|
|
|
|
2014-07-29 01:40:05 -04:00
|
|
|
# don't notify on reflection
|
|
|
|
post1.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(PostAlerter.new.extract_linked_users(post1).length).to eq(0)
|
2014-03-18 21:07:48 -04:00
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
linking_post
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], linking_post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2014-03-18 21:07:48 -04:00
|
|
|
end
|
|
|
|
|
2015-11-30 01:03:47 -05:00
|
|
|
context '@group mentions' do
|
|
|
|
|
2017-08-28 12:32:08 -04:00
|
|
|
let(:group) { Fabricate(:group, name: 'group', mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
|
2017-07-19 16:51:32 -04:00
|
|
|
let(:post) { create_post_with_alerts(raw: "Hello @group how are you?") }
|
|
|
|
before { group.add(evil_trout) }
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
it 'notifies users correctly' do
|
2015-11-30 01:03:47 -05:00
|
|
|
expect {
|
2017-07-19 16:51:32 -04:00
|
|
|
post
|
2015-11-30 01:03:47 -05:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
|
|
|
|
2015-12-01 00:52:43 -05:00
|
|
|
expect(GroupMention.count).to eq(1)
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2017-08-28 12:32:08 -04:00
|
|
|
Fabricate(:group, name: 'group-alt', mentionable_level: Group::ALIAS_LEVELS[:everyone])
|
2015-11-30 01:03:47 -05:00
|
|
|
|
|
|
|
expect {
|
2016-02-23 14:57:54 -05:00
|
|
|
create_post_with_alerts(raw: "Hello, @group-alt should not trigger a notification?")
|
2015-11-30 01:03:47 -05:00
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
2015-12-01 00:52:43 -05:00
|
|
|
|
|
|
|
expect(GroupMention.count).to eq(2)
|
2016-02-23 14:57:54 -05:00
|
|
|
|
2017-08-28 12:32:08 -04:00
|
|
|
group.update_columns(mentionable_level: Group::ALIAS_LEVELS[:members_mods_and_admins])
|
2016-02-23 14:57:54 -05:00
|
|
|
expect {
|
|
|
|
create_post_with_alerts(raw: "Hello @group you are not mentionable")
|
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
|
|
|
|
|
|
|
expect(GroupMention.count).to eq(3)
|
2015-11-30 01:03:47 -05:00
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
post
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2015-11-30 01:03:47 -05:00
|
|
|
end
|
|
|
|
|
2014-03-18 00:22:39 -04:00
|
|
|
context '@mentions' do
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout') }
|
2014-03-18 00:22:39 -04:00
|
|
|
let(:topic) { mention_post.topic }
|
|
|
|
|
2018-05-31 03:53:49 -04:00
|
|
|
before do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-05-31 03:53:49 -04:00
|
|
|
end
|
|
|
|
|
2014-03-18 00:22:39 -04:00
|
|
|
it 'notifies a user' do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2014-03-18 00:22:39 -04:00
|
|
|
mention_post
|
2015-04-25 11:18:35 -04:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't notify the user a second time on revision" do
|
|
|
|
mention_post
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2017-07-27 21:20:09 -04:00
|
|
|
mention_post.revise(mention_post.user, raw: "New raw content that still mentions @eviltrout")
|
2015-04-25 11:18:35 -04:00
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify the user who created the topic in regular mode" do
|
|
|
|
topic.notify_regular!(user)
|
|
|
|
mention_post
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2014-03-18 00:22:39 -04:00
|
|
|
create_post_with_alerts(user: user, raw: 'second post', topic: topic)
|
2015-04-25 11:18:35 -04:00
|
|
|
}.not_to change(user.notifications, :count)
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
mention_post
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], mention_post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
|
|
|
|
2018-03-08 05:40:48 -05:00
|
|
|
it "notification comes from editor if mention is added later" do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
post = create_post_with_alerts(user: user, raw: 'No mention here.')
|
|
|
|
expect {
|
|
|
|
post.revise(admin, raw: "Mention @eviltrout in this edit.")
|
|
|
|
}.to change(evil_trout.notifications, :count)
|
|
|
|
n = evil_trout.notifications.last
|
|
|
|
expect(n.data_hash["original_username"]).to eq(admin.username)
|
|
|
|
end
|
|
|
|
|
2018-04-16 05:48:06 -04:00
|
|
|
it "doesn't notify the last post editor if they mention themself" do
|
|
|
|
post = create_post_with_alerts(user: user, raw: 'Post without a mention.')
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, raw: "O hai, @eviltrout!")
|
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
|
|
|
end
|
|
|
|
|
2018-03-08 05:40:48 -05:00
|
|
|
let(:alice) { Fabricate(:user, username: 'alice') }
|
|
|
|
let(:bob) { Fabricate(:user, username: 'bob') }
|
2018-04-12 10:19:44 -04:00
|
|
|
let(:carol) { Fabricate(:admin, username: 'carol') }
|
|
|
|
let(:dave) { Fabricate(:user, username: 'dave') }
|
|
|
|
let(:eve) { Fabricate(:user, username: 'eve') }
|
|
|
|
let(:group) { Fabricate(:group, name: 'group', mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
|
|
|
|
|
|
|
|
before do
|
2018-11-23 12:25:40 -05:00
|
|
|
group.bulk_add([alice.id, eve.id])
|
2018-04-12 10:19:44 -04:00
|
|
|
end
|
2018-03-08 05:40:48 -05:00
|
|
|
|
|
|
|
def create_post_with_alerts(args = {})
|
|
|
|
post = Fabricate(:post, args)
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_topic_notification_level(user, topic, level_name)
|
|
|
|
TopicUser.change(user.id, topic.id, notification_level: TopicUser.notification_levels[level_name])
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic" do
|
|
|
|
let(:topic) { Fabricate(:topic, user: alice) }
|
|
|
|
|
|
|
|
[:watching, :tracking, :regular].each do |notification_level|
|
|
|
|
context "when notification level is '#{notification_level}'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, topic, notification_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies about @username mention" do
|
|
|
|
args = { user: bob, topic: topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :mentioned)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when notification level is 'muted'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, topic, :muted)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not notify about @username mention" do
|
|
|
|
args = { user: bob, topic: topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(alice, :mentioned)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
context "message to users" do
|
|
|
|
let(:pm_topic) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
user: alice,
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: alice),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: bob),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: Discourse.system_user)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is part of conversation" do
|
2018-03-08 05:40:48 -05:00
|
|
|
[:watching, :tracking, :regular].each do |notification_level|
|
|
|
|
context "when notification level is '#{notification_level}'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, pm_topic, notification_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies about @username mention" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :mentioned)
|
|
|
|
end
|
2018-04-12 10:19:44 -04:00
|
|
|
|
|
|
|
it "notifies about @username mentions by non-human users" do
|
|
|
|
args = { user: Discourse.system_user, topic: pm_topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :mentioned)
|
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
it "notifies about @group mention when allowed user is part of group" do
|
2018-04-12 10:19:44 -04:00
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :group_mentioned)
|
|
|
|
end
|
2018-03-08 05:40:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when notification level is 'muted'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, pm_topic, :muted)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not notify about @username mention" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(alice, :mentioned)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
context "when user is not part of conversation" do
|
|
|
|
it "does not notify about @username mention even though mentioned user is an admin" do
|
2018-03-08 05:40:48 -05:00
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @carol' }
|
2018-11-23 12:25:40 -05:00
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(carol, :mentioned)
|
2018-03-08 05:40:48 -05:00
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
it "does not notify about @username mention by non-human user even though mentioned user is an admin" do
|
2018-04-12 10:19:44 -04:00
|
|
|
args = { user: Discourse.system_user, topic: pm_topic, raw: 'Hello @carol' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(carol, :mentioned)
|
|
|
|
end
|
|
|
|
|
2018-03-08 05:40:48 -05:00
|
|
|
it "does not notify about @username mention when mentioned user is not allowed to see message" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @dave' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(dave, :mentioned)
|
|
|
|
end
|
2018-04-12 10:19:44 -04:00
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
it "does not notify about @group mention when user is not an allowed user" do
|
2018-04-12 10:19:44 -04:00
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @group' }
|
2018-11-23 12:25:40 -05:00
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(eve, :group_mentioned)
|
2018-04-12 10:19:44 -04:00
|
|
|
end
|
2018-03-08 05:40:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
context "message to group" do
|
2018-03-08 05:40:48 -05:00
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
let(:some_group) { Fabricate(:group, name: 'some_group', mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
|
2018-03-08 05:40:48 -05:00
|
|
|
let(:pm_topic) do
|
2018-11-23 12:25:40 -05:00
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
user: alice,
|
|
|
|
topic_allowed_groups: [
|
|
|
|
Fabricate.build(:topic_allowed_group, group: group)
|
|
|
|
],
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: Discourse.system_user)
|
|
|
|
]
|
|
|
|
)
|
2018-03-08 05:40:48 -05:00
|
|
|
end
|
|
|
|
|
2018-04-12 10:19:44 -04:00
|
|
|
before do
|
2018-11-23 12:25:40 -05:00
|
|
|
some_group.bulk_add([alice.id, carol.id])
|
2018-04-12 10:19:44 -04:00
|
|
|
end
|
|
|
|
|
2018-11-23 12:25:40 -05:00
|
|
|
context "when group is part of conversation" do
|
|
|
|
[:watching, :tracking, :regular].each do |notification_level|
|
|
|
|
context "when notification level is '#{notification_level}'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, pm_topic, notification_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies about @group mention" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :group_mentioned)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies about @group mentions by non-human users" do
|
|
|
|
args = { user: Discourse.system_user, topic: pm_topic, raw: 'Hello @group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :group_mentioned)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies about @username mention when user belongs to allowed group" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @alice' }
|
|
|
|
expect { create_post_with_alerts(args) }.to add_notification(alice, :mentioned)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when notification level is 'muted'" do
|
|
|
|
before do
|
|
|
|
set_topic_notification_level(alice, pm_topic, :muted)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not notify about @group mention" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(alice, :group_mentioned)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when group is not part of conversation" do
|
|
|
|
it "does not notify about @group mention even though mentioned user is an admin" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @some_group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(carol, :group_mentioned)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not notify about @group mention by non-human user even though mentioned user is an admin" do
|
|
|
|
args = { user: Discourse.system_user, topic: pm_topic, raw: 'Hello @some_group' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(carol, :group_mentioned)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not notify about @username mention when user doesn't belong to allowed group" do
|
|
|
|
args = { user: bob, topic: pm_topic, raw: 'Hello @dave' }
|
|
|
|
expect { create_post_with_alerts(args) }.to_not add_notification(dave, :mentioned)
|
|
|
|
end
|
|
|
|
end
|
2018-03-08 05:40:48 -05:00
|
|
|
end
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|
2016-02-22 19:34:16 -05:00
|
|
|
|
|
|
|
describe ".create_notification" do
|
2016-02-24 09:34:40 -05:00
|
|
|
let(:topic) { Fabricate(:private_message_topic, user: user, created_at: 1.hour.ago) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic, created_at: 1.hour.ago) }
|
2016-02-22 19:34:16 -05:00
|
|
|
|
2016-02-24 09:34:40 -05:00
|
|
|
it "creates a notification for PMs" do
|
|
|
|
post.revise(user, { raw: 'This is the revised post' }, revised_at: Time.zone.now)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
PostAlerter.new.create_notification(user, Notification.types[:private_message], post)
|
|
|
|
}.to change { user.notifications.count }.by(1)
|
2016-02-22 19:34:16 -05:00
|
|
|
|
2016-02-24 09:34:40 -05:00
|
|
|
expect(user.notifications.last.data_hash["topic_title"]).to eq(topic.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "keeps the original title for PMs" do
|
2016-02-22 19:34:16 -05:00
|
|
|
original_title = topic.title
|
|
|
|
|
|
|
|
post.revise(user, { title: "This is the revised title" }, revised_at: Time.now)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
PostAlerter.new.create_notification(user, Notification.types[:private_message], post)
|
2016-02-24 09:34:40 -05:00
|
|
|
}.to change { user.notifications.count }.by(1)
|
2016-02-22 19:34:16 -05:00
|
|
|
|
|
|
|
expect(user.notifications.last.data_hash["topic_title"]).to eq(original_title)
|
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :post_notification_alert" do
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "triggers :before_create_notification" do
|
|
|
|
type = Notification.types[:private_message]
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.new.create_notification(user, type, post, {})
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notification, params: [user, type, post, {}])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2016-02-22 19:34:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-25 22:47:10 -04:00
|
|
|
describe "push_notification" do
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:') }
|
2016-08-25 22:47:10 -04:00
|
|
|
let(:topic) { mention_post.topic }
|
|
|
|
|
2017-04-05 08:28:24 -04:00
|
|
|
it "pushes nothing to suspended users" do
|
|
|
|
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
|
|
|
|
|
|
|
|
evil_trout.update_columns(suspended_till: 1.year.from_now)
|
|
|
|
|
|
|
|
2.times do |i|
|
|
|
|
UserApiKey.create!(user_id: evil_trout.id,
|
|
|
|
client_id: "xxx#{i}",
|
|
|
|
key: "yyy#{i}",
|
|
|
|
application_name: "iPhone#{i}",
|
|
|
|
scopes: ['notifications'],
|
|
|
|
push_url: "https://site2.com/push")
|
|
|
|
end
|
|
|
|
|
2017-06-15 20:08:15 -04:00
|
|
|
expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }
|
2017-04-05 08:28:24 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 22:47:10 -04:00
|
|
|
it "correctly pushes notifications if configured correctly" do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2016-08-25 22:47:10 -04:00
|
|
|
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
|
|
|
|
|
|
|
|
2.times do |i|
|
|
|
|
UserApiKey.create!(user_id: evil_trout.id,
|
|
|
|
client_id: "xxx#{i}",
|
|
|
|
key: "yyy#{i}",
|
|
|
|
application_name: "iPhone#{i}",
|
2016-10-14 07:52:25 -04:00
|
|
|
scopes: ['notifications'],
|
2016-08-25 22:47:10 -04:00
|
|
|
push_url: "https://site2.com/push")
|
|
|
|
end
|
|
|
|
|
2016-09-04 01:50:53 -04:00
|
|
|
body = nil
|
|
|
|
headers = nil
|
2016-08-25 22:47:10 -04:00
|
|
|
|
2019-04-08 10:58:21 -04:00
|
|
|
stub_request(:post, "https://site2.com/push")
|
|
|
|
.to_return do |request|
|
|
|
|
body = request.body
|
|
|
|
headers = request.headers
|
|
|
|
{ status: 200, body: "OK" }
|
|
|
|
end
|
2016-08-25 22:47:10 -04:00
|
|
|
|
2016-09-04 01:50:53 -04:00
|
|
|
payload = {
|
|
|
|
"secret_key" => SiteSetting.push_api_secret_key,
|
|
|
|
"url" => Discourse.base_url,
|
|
|
|
"title" => SiteSetting.title,
|
|
|
|
"description" => SiteSetting.site_description,
|
|
|
|
"notifications" => [
|
|
|
|
{
|
|
|
|
'notification_type' => 1,
|
|
|
|
'post_number' => 1,
|
|
|
|
'topic_title' => topic.title,
|
|
|
|
'topic_id' => topic.id,
|
2016-10-10 22:03:21 -04:00
|
|
|
'excerpt' => 'Hello @eviltrout ❤',
|
2016-09-04 01:50:53 -04:00
|
|
|
'username' => user.username,
|
|
|
|
'url' => UrlHelper.absolute(mention_post.url),
|
|
|
|
'client_id' => 'xxx0'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'notification_type' => 1,
|
|
|
|
'post_number' => 1,
|
|
|
|
'topic_title' => topic.title,
|
|
|
|
'topic_id' => topic.id,
|
2016-10-10 22:03:21 -04:00
|
|
|
'excerpt' => 'Hello @eviltrout ❤',
|
2016-09-04 01:50:53 -04:00
|
|
|
'username' => user.username,
|
|
|
|
'url' => UrlHelper.absolute(mention_post.url),
|
|
|
|
'client_id' => 'xxx1'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:39:17 -05:00
|
|
|
post = mention_post
|
2017-06-15 20:08:15 -04:00
|
|
|
|
2016-09-04 01:50:53 -04:00
|
|
|
expect(JSON.parse(body)).to eq(payload)
|
2017-06-15 20:08:15 -04:00
|
|
|
expect(headers["Content-Type"]).to eq('application/json')
|
2018-12-05 00:39:17 -05:00
|
|
|
|
|
|
|
TopicUser.change(evil_trout.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
post = Fabricate(:post, topic: post.topic, user_id: evil_trout.id)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
|
|
|
# if we collapse a reply notification we should get notified on the correct post
|
|
|
|
new_post = create_post_with_alerts(topic: post.topic, user_id: user.id, reply_to_post_number: post.post_number, raw: 'this is my first reply')
|
|
|
|
|
|
|
|
changes = {
|
|
|
|
"notification_type" => Notification.types[:posted],
|
|
|
|
"post_number" => new_post.post_number,
|
|
|
|
"username" => new_post.user.username,
|
|
|
|
"excerpt" => new_post.raw,
|
|
|
|
"url" => UrlHelper.absolute(new_post.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload["notifications"][0].merge! changes
|
|
|
|
payload["notifications"][1].merge! changes
|
|
|
|
|
|
|
|
expect(JSON.parse(body)).to eq(payload)
|
|
|
|
|
|
|
|
new_post = create_post_with_alerts(topic: post.topic, user_id: user2.id, reply_to_post_number: post.post_number, raw: 'this is my second reply')
|
|
|
|
|
|
|
|
changes = {
|
|
|
|
"post_number" => new_post.post_number,
|
|
|
|
"username" => new_post.user.username,
|
|
|
|
"excerpt" => new_post.raw,
|
|
|
|
"url" => UrlHelper.absolute(new_post.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload["notifications"][0].merge! changes
|
|
|
|
payload["notifications"][1].merge! changes
|
|
|
|
|
|
|
|
expect(JSON.parse(body)).to eq(payload)
|
|
|
|
|
2016-08-25 22:47:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-06 15:56:40 -04:00
|
|
|
describe "watching_first_post" do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
let(:topic) { Fabricate(:topic, category: category, tags: [tag]) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
it "doesn't notify people who aren't watching" do
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies the user who is following the first post category" do
|
|
|
|
level = CategoryUser.notification_levels[:watching_first_post]
|
|
|
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
2016-07-19 15:57:05 -04:00
|
|
|
PostAlerter.new.after_save_post(post, true)
|
2016-07-06 15:56:40 -04:00
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2016-07-19 15:57:05 -04:00
|
|
|
it "doesn't notify when the record is not new" do
|
|
|
|
level = CategoryUser.notification_levels[:watching_first_post]
|
|
|
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
|
|
|
PostAlerter.new.after_save_post(post, false)
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2016-07-06 15:56:40 -04:00
|
|
|
it "notifies the user who is following the first post tag" do
|
|
|
|
level = TagUser.notification_levels[:watching_first_post]
|
|
|
|
TagUser.change(user.id, tag.id, level)
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies the user who is following the first post group" do
|
|
|
|
GroupUser.create(group_id: group.id, user_id: user.id)
|
|
|
|
GroupUser.create(group_id: group.id, user_id: post.user.id)
|
2016-07-21 15:05:10 -04:00
|
|
|
topic.topic_allowed_groups.create(group_id: group.id)
|
2016-07-06 15:56:40 -04:00
|
|
|
|
|
|
|
level = GroupUser.notification_levels[:watching_first_post]
|
|
|
|
GroupUser.where(user_id: user.id, group_id: group.id).update_all(notification_level: level)
|
|
|
|
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
level = CategoryUser.notification_levels[:watching_first_post]
|
|
|
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.new.after_save_post(post, true)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "replies" do
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
2018-12-04 01:54:27 -05:00
|
|
|
_post = Fabricate(:post, user: user, topic: topic)
|
2017-07-19 16:51:32 -04:00
|
|
|
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], reply])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
2017-09-19 09:51:10 -04:00
|
|
|
|
|
|
|
it "notifies about regular reply" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
2018-12-04 01:54:27 -05:00
|
|
|
_post = Fabricate(:post, user: user, topic: topic)
|
2017-09-19 09:51:10 -04:00
|
|
|
|
|
|
|
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify regular user about whispered reply" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
|
|
|
topic = Fabricate(:topic)
|
2018-12-04 01:54:27 -05:00
|
|
|
_post = Fabricate(:post, user: user, topic: topic)
|
2017-09-19 09:51:10 -04:00
|
|
|
|
|
|
|
whispered_reply = Fabricate(:post, user: admin, topic: topic, post_type: Post.types[:whisper], reply_to_post_number: 1)
|
|
|
|
PostAlerter.post_created(whispered_reply)
|
|
|
|
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:replied]).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies staff user about whispered reply" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
admin1 = Fabricate(:admin)
|
|
|
|
admin2 = Fabricate(:admin)
|
|
|
|
|
|
|
|
topic = Fabricate(:topic)
|
2018-12-04 01:54:27 -05:00
|
|
|
_post = Fabricate(:post, user: user, topic: topic)
|
2017-09-19 09:51:10 -04:00
|
|
|
|
|
|
|
whispered_reply1 = Fabricate(:post, user: admin1, topic: topic, post_type: Post.types[:whisper], reply_to_post_number: 1)
|
|
|
|
whispered_reply2 = Fabricate(:post, user: admin2, topic: topic, post_type: Post.types[:whisper], reply_to_post_number: 2)
|
|
|
|
PostAlerter.post_created(whispered_reply1)
|
|
|
|
PostAlerter.post_created(whispered_reply2)
|
|
|
|
|
|
|
|
expect(admin1.notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
|
2018-12-04 01:54:27 -05:00
|
|
|
|
|
|
|
TopicUser.change(admin1.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
# this should change nothing cause the moderator post has an action code
|
|
|
|
# if we have an action code then we should never have notifications, this is rare but
|
|
|
|
# assign whispers are like this
|
|
|
|
whispered_reply3 = topic.add_moderator_post(admin2, "i am a reply", post_type: Post.types[:whisper], action_code: 'moderator_thing')
|
|
|
|
PostAlerter.post_created(whispered_reply3)
|
|
|
|
|
|
|
|
# if this whisper is not ignored like it should we would see a posted notification and no replied notifications
|
|
|
|
notifications = admin1.notifications.where(topic_id: topic.id).to_a
|
|
|
|
|
|
|
|
expect(notifications.first.notification_type).to eq(Notification.types[:replied])
|
|
|
|
expect(notifications.length).to eq(1)
|
|
|
|
expect(notifications.first.post_number).to eq(whispered_reply2.post_number)
|
2017-09-19 09:51:10 -04:00
|
|
|
end
|
2017-11-10 10:10:25 -05:00
|
|
|
|
|
|
|
it "sends email notifications only to users not on CC list of incoming email" do
|
|
|
|
alice = Fabricate(:user, username: "alice", email: "alice@example.com")
|
|
|
|
bob = Fabricate(:user, username: "bob", email: "bob@example.com")
|
2017-11-10 11:56:18 -05:00
|
|
|
carol = Fabricate(:user, username: "carol", email: "carol@example.com", staged: true)
|
2017-11-10 10:10:25 -05:00
|
|
|
dave = Fabricate(:user, username: "dave", email: "dave@example.com", staged: true)
|
|
|
|
erin = Fabricate(:user, username: "erin", email: "erin@example.com")
|
|
|
|
|
|
|
|
topic = Fabricate(:private_message_topic, topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: alice),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: bob),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: carol),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: dave),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: erin)
|
|
|
|
])
|
2018-12-05 00:39:17 -05:00
|
|
|
_post = Fabricate(:post, user: alice, topic: topic)
|
2017-11-10 10:10:25 -05:00
|
|
|
|
|
|
|
TopicUser.change(alice.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
TopicUser.change(bob.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
TopicUser.change(erin.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
email = Fabricate(:incoming_email,
|
|
|
|
raw: <<~RAW,
|
|
|
|
Return-Path: <bob@example.com>
|
|
|
|
From: Bob <bob@example.com>
|
|
|
|
To: meta+1234@discoursemail.com, dave@example.com
|
|
|
|
CC: carol@example.com, erin@example.com
|
|
|
|
Subject: Hello world
|
|
|
|
Date: Fri, 15 Jan 2016 00:12:43 +0100
|
|
|
|
Message-ID: <12345@example.com>
|
|
|
|
Mime-Version: 1.0
|
|
|
|
Content-Type: text/plain; charset=UTF-8
|
|
|
|
Content-Transfer-Encoding: quoted-printable
|
|
|
|
|
|
|
|
This post was created by email.
|
|
|
|
RAW
|
|
|
|
from_address: "bob@example.com",
|
|
|
|
to_addresses: "meta+1234@discoursemail.com;dave@example.com",
|
|
|
|
cc_addresses: "carol@example.com;erin@example.com")
|
|
|
|
reply = Fabricate(:post_via_email, user: bob, topic: topic, incoming_email: email, reply_to_post_number: 1)
|
|
|
|
|
|
|
|
NotificationEmailer.expects(:process_notification).with { |n| n.user_id == alice.id }.once
|
|
|
|
NotificationEmailer.expects(:process_notification).with { |n| n.user_id == bob.id }.never
|
|
|
|
NotificationEmailer.expects(:process_notification).with { |n| n.user_id == carol.id }.never
|
|
|
|
NotificationEmailer.expects(:process_notification).with { |n| n.user_id == dave.id }.never
|
|
|
|
NotificationEmailer.expects(:process_notification).with { |n| n.user_id == erin.id }.never
|
|
|
|
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
|
|
|
|
expect(alice.notifications.count).to eq(1)
|
|
|
|
expect(bob.notifications.count).to eq(0)
|
|
|
|
expect(carol.notifications.count).to eq(1)
|
|
|
|
expect(dave.notifications.count).to eq(1)
|
|
|
|
expect(erin.notifications.count).to eq(1)
|
|
|
|
end
|
2017-11-17 08:50:35 -05:00
|
|
|
|
|
|
|
it "does not send email notifications to staged users when notification originates in mailinglist mirror category" do
|
|
|
|
category = Fabricate(:mailinglist_mirror_category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
user = Fabricate(:staged)
|
2018-12-05 00:39:17 -05:00
|
|
|
_post = Fabricate(:post, user: user, topic: topic)
|
2017-11-17 08:50:35 -05:00
|
|
|
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
|
|
|
|
|
|
|
|
NotificationEmailer.expects(:process_notification).never
|
|
|
|
expect { PostAlerter.post_created(reply) }.to change(user.notifications, :count).by(0)
|
|
|
|
|
|
|
|
category.mailinglist_mirror = false
|
|
|
|
NotificationEmailer.expects(:process_notification).once
|
|
|
|
expect { PostAlerter.post_created(reply) }.to change(user.notifications, :count).by(1)
|
|
|
|
end
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
|
|
|
|
2018-07-21 05:20:21 -04:00
|
|
|
context "category" do
|
|
|
|
context "watching" do
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
level = CategoryUser.notification_levels[:watching]
|
|
|
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies staff about whispered post" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
level = CategoryUser.notification_levels[:watching]
|
|
|
|
CategoryUser.set_notification_level_for_category(admin, level, category.id)
|
|
|
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
|
|
|
whispered_post = Fabricate(:post, user: Fabricate(:admin), topic: topic, post_type: Post.types[:whisper])
|
|
|
|
expect {
|
|
|
|
PostAlerter.post_created(whispered_post)
|
|
|
|
}.to add_notification(admin, :posted)
|
|
|
|
expect {
|
|
|
|
PostAlerter.post_created(whispered_post)
|
|
|
|
}.not_to add_notification(user, :posted)
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "tags" do
|
|
|
|
context "watching" do
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
topic = Fabricate(:topic, tags: [tag])
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
level = TagUser.notification_levels[:watching]
|
|
|
|
TagUser.change(user.id, tag.id, level)
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
2017-07-19 16:51:32 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-12 13:09:34 -04:00
|
|
|
|
|
|
|
context "on change" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:other_tag) { Fabricate(:tag) }
|
|
|
|
let(:watched_tag) { Fabricate(:tag) }
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2019-04-05 09:06:38 -04:00
|
|
|
TagUser.change(user.id, watched_tag.id, TagUser.notification_levels[:watching_first_post])
|
2019-03-12 13:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "triggers a notification" do
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
|
|
|
|
|
|
|
|
PostRevisor.new(post).revise!(Fabricate(:user), tags: [other_tag.name, watched_tag.name])
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
|
|
|
|
PostRevisor.new(post).revise!(Fabricate(:user), tags: [watched_tag.name, other_tag.name])
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
end
|
2019-04-05 09:06:38 -04:00
|
|
|
|
|
|
|
it "doesn't trigger a notification if topic is unlisted" do
|
|
|
|
post.topic.update_column(:visible, false)
|
|
|
|
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
|
|
|
|
|
|
|
|
PostRevisor.new(post).revise!(Fabricate(:user), tags: [other_tag.name, watched_tag.name])
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(0)
|
|
|
|
end
|
2019-03-12 13:09:34 -04:00
|
|
|
end
|
2016-07-06 15:56:40 -04:00
|
|
|
end
|
2017-09-25 04:16:37 -04:00
|
|
|
|
|
|
|
describe '#extract_linked_users' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:post2) { Fabricate(:post) }
|
|
|
|
|
|
|
|
describe 'when linked post has been deleted' do
|
|
|
|
let(:topic_link) do
|
|
|
|
TopicLink.create!(
|
|
|
|
url: "/t/#{topic.id}",
|
|
|
|
topic_id: topic.id,
|
|
|
|
link_topic_id: post2.topic.id,
|
|
|
|
link_post_id: nil,
|
|
|
|
post_id: post.id,
|
|
|
|
user: user,
|
|
|
|
domain: 'test.com'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the first post of the topic' do
|
|
|
|
topic_link
|
|
|
|
expect(PostAlerter.new.extract_linked_users(post.reload)).to eq([post2.user])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-18 00:22:39 -04:00
|
|
|
end
|