2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-03-18 12:22:39 +08:00
|
|
|
|
|
|
|
describe PostAlerter do
|
|
|
|
|
|
|
|
let!(:evil_trout) { Fabricate(:evil_trout) }
|
2016-02-24 22:34:40 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-03-18 12:22:39 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def create_post_with_alerts(args = {})
|
2014-03-18 12:22:39 +08:00
|
|
|
post = Fabricate(:post, args)
|
2014-03-19 09:07:48 +08:00
|
|
|
PostAlerter.post_created(post)
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
|
2016-03-22 11:28:14 +08:00
|
|
|
context "private message" do
|
|
|
|
it "notifies for pms correctly" do
|
|
|
|
pm = Fabricate(:topic, archetype: 'private_message', category_id: nil)
|
|
|
|
op = Fabricate(:post, user_id: pm.user_id)
|
|
|
|
pm.allowed_users << pm.user
|
|
|
|
PostAlerter.post_created(op)
|
|
|
|
reply = Fabricate(:post, user_id: pm.user_id, topic_id: pm.id, reply_to_post_number: 1)
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
|
|
|
|
reply2 = Fabricate(:post, topic_id: pm.id, reply_to_post_number: 1)
|
|
|
|
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
|
|
|
|
|
|
|
|
reply3 = Fabricate(:post, topic_id: pm.id)
|
|
|
|
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
|
|
|
|
|
|
|
|
reply4 = Fabricate(:post, topic_id: pm.id, reply_to_post_number: 1)
|
|
|
|
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-20 04:51:32 +08:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
pm = Fabricate(:topic, archetype: 'private_message', category_id: nil)
|
|
|
|
op = Fabricate(:post, user_id: pm.user_id, topic: pm)
|
|
|
|
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-28 09:20:09 +08: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-20 04:51:32 +08:00
|
|
|
end
|
2016-03-22 11:28:14 +08:00
|
|
|
end
|
|
|
|
|
2015-10-20 04:31:48 +08:00
|
|
|
context "unread" do
|
|
|
|
it "does not return whispers as unread posts" do
|
|
|
|
op = Fabricate(:post)
|
2015-11-30 14:03:47 +08:00
|
|
|
_whisper = Fabricate(:post, raw: 'this is a whisper post',
|
2017-07-28 09:20:09 +08:00
|
|
|
user: Fabricate(:admin),
|
|
|
|
topic: op.topic,
|
|
|
|
reply_to_post_number: op.post_number,
|
|
|
|
post_type: Post.types[:whisper])
|
2015-10-20 04:31:48 +08:00
|
|
|
|
|
|
|
expect(PostAlerter.new.first_unread_post(op.user, op.topic)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 13:00:14 +08:00
|
|
|
context 'edits' do
|
|
|
|
it 'notifies correctly on edits' do
|
2016-12-22 13:46:22 +08:00
|
|
|
PostActionNotifier.enable
|
2015-06-19 10:31:36 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'I love waffles')
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
2017-07-28 09:20:09 +08:00
|
|
|
post.revise(admin, raw: 'I made a revision')
|
2015-06-19 10:31:36 +08:00
|
|
|
|
2016-03-03 13:00:14 +08:00
|
|
|
# skip this notification cause we already notified on a similar edit
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time 2.hours.from_now
|
2017-07-28 09:20:09 +08:00
|
|
|
post.revise(admin, raw: 'I made another revision')
|
2015-06-19 10:31:36 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
post.revise(Fabricate(:admin), raw: 'I made a revision')
|
2016-03-03 13:00:14 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time 2.hours.from_now
|
2017-07-28 09:20:09 +08:00
|
|
|
post.revise(admin, raw: 'I made another revision')
|
2016-03-03 13:00:14 +08:00
|
|
|
|
2016-07-18 11:00:23 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(3)
|
2016-03-03 13:00:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'likes' do
|
2016-03-04 19:55:49 +08:00
|
|
|
|
|
|
|
it 'notifies on likes after an undo' do
|
2016-12-22 13:46:22 +08:00
|
|
|
PostActionNotifier.enable
|
2016-03-04 19:55:49 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'I love waffles')
|
|
|
|
|
|
|
|
PostAction.act(evil_trout, post, PostActionType.types[:like])
|
|
|
|
PostAction.remove_act(evil_trout, post, PostActionType.types[:like])
|
|
|
|
PostAction.act(evil_trout, post, PostActionType.types[:like])
|
|
|
|
|
2016-07-18 11:00:23 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(1)
|
2016-03-04 19:55:49 +08:00
|
|
|
end
|
|
|
|
|
2016-03-06 06:12:59 +08:00
|
|
|
it 'notifies on does not notify when never is selected' do
|
2016-12-22 13:46:22 +08:00
|
|
|
PostActionNotifier.enable
|
2016-03-06 06:12:59 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'I love waffles')
|
|
|
|
|
|
|
|
post.user.user_option.update_columns(like_notification_frequency:
|
|
|
|
UserOption.like_notification_frequency_type[:never])
|
|
|
|
|
|
|
|
PostAction.act(evil_trout, post, PostActionType.types[:like])
|
|
|
|
|
2016-07-18 11:00:23 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(0)
|
2016-03-06 06:12:59 +08:00
|
|
|
end
|
|
|
|
|
2016-03-03 13:00:14 +08:00
|
|
|
it 'notifies on likes correctly' do
|
2016-12-22 13:46:22 +08:00
|
|
|
PostActionNotifier.enable
|
2016-03-03 13:00:14 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'I love waffles')
|
2016-03-04 19:55:49 +08:00
|
|
|
|
2016-03-03 13:00:14 +08:00
|
|
|
PostAction.act(evil_trout, post, PostActionType.types[:like])
|
2016-03-04 19:55:49 +08:00
|
|
|
admin = Fabricate(:admin)
|
|
|
|
PostAction.act(admin, post, PostActionType.types[:like])
|
2016-03-03 13:00:14 +08:00
|
|
|
|
|
|
|
# one like
|
2017-07-24 14:25:07 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count)
|
|
|
|
.to eq(1)
|
2016-03-02 20:16:52 +08:00
|
|
|
|
2017-07-24 14:25:07 +08:00
|
|
|
post.user.user_option.update_columns(
|
|
|
|
like_notification_frequency: UserOption.like_notification_frequency_type[:always]
|
|
|
|
)
|
2016-03-02 20:16:52 +08:00
|
|
|
|
|
|
|
admin2 = Fabricate(:admin)
|
2017-07-24 14:25:07 +08:00
|
|
|
|
|
|
|
# Travel 1 hour in time to test that order post_actions by `created_at`
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time 1.hour.from_now
|
|
|
|
PostAction.act(admin2, post, PostActionType.types[:like])
|
2016-03-04 19:55:49 +08:00
|
|
|
|
2017-07-24 14:25:07 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count)
|
|
|
|
.to eq(1)
|
2016-03-04 19:55:49 +08:00
|
|
|
|
2017-07-24 14:25:07 +08:00
|
|
|
# adds info to the notification
|
|
|
|
notification = Notification.find_by(
|
|
|
|
post_number: 1,
|
|
|
|
topic_id: post.topic_id
|
|
|
|
)
|
2016-03-04 19:55:49 +08:00
|
|
|
|
|
|
|
expect(notification.data_hash["count"].to_i).to eq(2)
|
|
|
|
expect(notification.data_hash["username2"]).to eq(evil_trout.username)
|
|
|
|
|
|
|
|
# this is a tricky thing ... removing a like should fix up the notifications
|
|
|
|
PostAction.remove_act(evil_trout, post, PostActionType.types[:like])
|
|
|
|
|
|
|
|
# rebuilds the missing notification
|
2017-07-24 14:25:07 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count)
|
|
|
|
.to eq(1)
|
|
|
|
|
|
|
|
notification = Notification.find_by(
|
|
|
|
post_number: 1,
|
|
|
|
topic_id: post.topic_id
|
|
|
|
)
|
2016-03-04 19:55:49 +08:00
|
|
|
|
|
|
|
expect(notification.data_hash["count"]).to eq(2)
|
|
|
|
expect(notification.data_hash["username"]).to eq(admin2.username)
|
|
|
|
expect(notification.data_hash["username2"]).to eq(admin.username)
|
|
|
|
|
2016-03-02 20:16:52 +08:00
|
|
|
post.user.user_option.update_columns(like_notification_frequency:
|
|
|
|
UserOption.like_notification_frequency_type[:first_time_and_daily])
|
|
|
|
|
|
|
|
# this gets skipped
|
|
|
|
admin3 = Fabricate(:admin)
|
|
|
|
PostAction.act(admin3, post, PostActionType.types[:like])
|
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time 2.days.from_now
|
|
|
|
|
|
|
|
admin4 = Fabricate(:admin)
|
|
|
|
PostAction.act(admin4, post, PostActionType.types[:like])
|
2016-03-02 20:16:52 +08:00
|
|
|
|
|
|
|
# first happend within the same day, no need to notify
|
2016-07-18 11:00:23 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(2)
|
2016-03-02 20:16:52 +08:00
|
|
|
|
2015-06-19 10:31:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 12:22:39 +08:00
|
|
|
context 'quotes' do
|
|
|
|
|
2015-03-24 08:55:22 +08: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 23:18:35 +08:00
|
|
|
expect {
|
2015-03-24 08:55:22 +08:00
|
|
|
PostAlerter.post_created(post)
|
2015-04-25 23:18:35 +08:00
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
2015-03-24 08:55:22 +08:00
|
|
|
end
|
|
|
|
|
2014-03-18 12:22:39 +08:00
|
|
|
it 'notifies a user by username' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
create_post_with_alerts(raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
|
2015-04-25 23:18:35 +08:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
2014-03-18 12:22:39 +08: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 23:18:35 +08:00
|
|
|
expect {
|
2017-07-28 09:20:09 +08:00
|
|
|
p1.revise(p1.user, raw: '[quote="Evil Trout, post:1"]whatup now?[/quote]')
|
2015-04-25 23:18:35 +08:00
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify the poster" do
|
|
|
|
topic = create_post_with_alerts.topic
|
2015-04-25 23:18:35 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
Fabricate(:post, topic: topic, user: topic.user, raw: '[quote="Bruce Wayne, post:1"]whatup[/quote]')
|
2015-04-25 23:18:35 +08:00
|
|
|
}.not_to change(topic.user.notifications, :count)
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
2017-07-20 04:51:32 +08: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-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
|
2014-03-19 09:07:48 +08:00
|
|
|
context 'linked' do
|
2017-07-20 04:51:32 +08:00
|
|
|
let(:post1) { create_post }
|
|
|
|
let(:user) { post1.user }
|
|
|
|
let(:linking_post) { create_post(raw: "my magic topic\n##{Discourse.base_url}#{post1.url}") }
|
|
|
|
|
2014-03-19 09:07:48 +08:00
|
|
|
it "will notify correctly on linking" do
|
2017-07-20 04:51:32 +08:00
|
|
|
linking_post
|
2014-03-19 09:07:48 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(user.notifications.count).to eq(1)
|
2014-03-19 09:07:48 +08:00
|
|
|
|
2015-12-31 07:51:58 +08: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-19 09:07:48 +08:00
|
|
|
|
|
|
|
user.reload
|
2016-04-13 02:09:59 +08:00
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:linked]).count).to eq(1)
|
2014-07-29 13:40:05 +08:00
|
|
|
|
2015-12-31 07:51:58 +08:00
|
|
|
expect(watcher.notifications.count).to eq(1)
|
|
|
|
|
2014-07-29 13:40:05 +08:00
|
|
|
# don't notify on reflection
|
|
|
|
post1.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(PostAlerter.new.extract_linked_users(post1).length).to eq(0)
|
2014-07-29 13:40:05 +08:00
|
|
|
|
2014-03-19 09:07:48 +08:00
|
|
|
end
|
2017-07-20 04:51:32 +08:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
linking_post
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], linking_post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
2014-03-19 09:07:48 +08:00
|
|
|
end
|
|
|
|
|
2015-11-30 14:03:47 +08:00
|
|
|
context '@group mentions' do
|
|
|
|
|
2017-07-20 04:51:32 +08:00
|
|
|
let(:group) { Fabricate(:group, name: 'group', alias_level: Group::ALIAS_LEVELS[:everyone]) }
|
|
|
|
let(:post) { create_post_with_alerts(raw: "Hello @group how are you?") }
|
|
|
|
before { group.add(evil_trout) }
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2017-07-20 04:51:32 +08:00
|
|
|
it 'notifies users correctly' do
|
2015-11-30 14:03:47 +08:00
|
|
|
expect {
|
2017-07-20 04:51:32 +08:00
|
|
|
post
|
2015-11-30 14:03:47 +08:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
|
|
|
|
2015-12-01 13:52:43 +08:00
|
|
|
expect(GroupMention.count).to eq(1)
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2016-02-24 03:57:54 +08:00
|
|
|
Fabricate(:group, name: 'group-alt', alias_level: Group::ALIAS_LEVELS[:everyone])
|
2015-11-30 14:03:47 +08:00
|
|
|
|
|
|
|
expect {
|
2016-02-24 03:57:54 +08:00
|
|
|
create_post_with_alerts(raw: "Hello, @group-alt should not trigger a notification?")
|
2015-11-30 14:03:47 +08:00
|
|
|
}.to change(evil_trout.notifications, :count).by(0)
|
2015-12-01 13:52:43 +08:00
|
|
|
|
|
|
|
expect(GroupMention.count).to eq(2)
|
2016-02-24 03:57:54 +08:00
|
|
|
|
|
|
|
group.update_columns(alias_level: Group::ALIAS_LEVELS[:members_mods_and_admins])
|
|
|
|
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 14:03:47 +08:00
|
|
|
end
|
2017-07-20 04:51:32 +08:00
|
|
|
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
post
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
2015-11-30 14:03:47 +08:00
|
|
|
end
|
|
|
|
|
2014-03-18 12:22:39 +08:00
|
|
|
context '@mentions' do
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout') }
|
2014-03-18 12:22:39 +08:00
|
|
|
let(:topic) { mention_post.topic }
|
|
|
|
|
|
|
|
it 'notifies a user' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
mention_post
|
2015-04-25 23:18:35 +08:00
|
|
|
}.to change(evil_trout.notifications, :count).by(1)
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't notify the user a second time on revision" do
|
|
|
|
mention_post
|
2015-04-25 23:18:35 +08:00
|
|
|
expect {
|
2017-07-28 09:20:09 +08:00
|
|
|
mention_post.revise(mention_post.user, raw: "New raw content that still mentions @eviltrout")
|
2015-04-25 23:18:35 +08:00
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
2014-03-18 12:22:39 +08: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 23:18:35 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
create_post_with_alerts(user: user, raw: 'second post', topic: topic)
|
2015-04-25 23:18:35 +08:00
|
|
|
}.not_to change(user.notifications, :count)
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
|
2017-07-20 04:51:32 +08:00
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
mention_post
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[evil_trout], mention_post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
|
|
|
|
2016-03-09 04:26:06 +08:00
|
|
|
it "notification comes from editor is mention is added later" do
|
2017-07-28 09:20:09 +08:00
|
|
|
admin = Fabricate(:admin)
|
2016-03-09 04:26:06 +08:00
|
|
|
post = create_post_with_alerts(user: user, raw: 'No mention here.')
|
|
|
|
expect {
|
2017-07-28 09:20:09 +08:00
|
|
|
post.revise(admin, raw: "Mention @eviltrout in this edit.")
|
2016-03-09 04:26:06 +08:00
|
|
|
}.to change(evil_trout.notifications, :count)
|
|
|
|
n = evil_trout.notifications.last
|
2016-03-10 01:51:54 +08:00
|
|
|
expect(n.data_hash["original_username"]).to eq(admin.username)
|
2017-07-28 09:20:09 +08:00
|
|
|
end
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
2016-02-23 08:34:16 +08:00
|
|
|
|
|
|
|
describe ".create_notification" do
|
2016-02-24 22:34:40 +08: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-23 08:34:16 +08:00
|
|
|
|
2016-02-24 22:34:40 +08: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-23 08:34:16 +08:00
|
|
|
|
2016-02-24 22:34:40 +08:00
|
|
|
expect(user.notifications.last.data_hash["topic_title"]).to eq(topic.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "keeps the original title for PMs" do
|
2016-02-23 08:34:16 +08: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 22:34:40 +08:00
|
|
|
}.to change { user.notifications.count }.by(1)
|
2016-02-23 08:34:16 +08:00
|
|
|
|
|
|
|
expect(user.notifications.last.data_hash["topic_title"]).to eq(original_title)
|
|
|
|
end
|
2017-07-20 04:51:32 +08: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-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notification, params: [user, type, post, {}])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
2016-02-23 08:34:16 +08:00
|
|
|
end
|
|
|
|
|
2016-08-26 10:47:10 +08:00
|
|
|
describe "push_notification" do
|
2017-07-28 09:20:09 +08:00
|
|
|
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:') }
|
2016-08-26 10:47:10 +08:00
|
|
|
let(:topic) { mention_post.topic }
|
|
|
|
|
2017-04-05 20:28:24 +08:00
|
|
|
it "pushes nothing to suspended users" do
|
2017-06-16 08:08:15 +08:00
|
|
|
SiteSetting.queue_jobs = true
|
2017-04-05 20:28:24 +08:00
|
|
|
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-16 08:08:15 +08:00
|
|
|
expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }
|
2017-04-05 20:28:24 +08:00
|
|
|
end
|
|
|
|
|
2016-08-26 10:47:10 +08:00
|
|
|
it "correctly pushes notifications if configured correctly" do
|
|
|
|
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 19:52:25 +08:00
|
|
|
scopes: ['notifications'],
|
2016-08-26 10:47:10 +08:00
|
|
|
push_url: "https://site2.com/push")
|
|
|
|
end
|
|
|
|
|
2016-09-04 13:50:53 +08:00
|
|
|
body = nil
|
|
|
|
headers = nil
|
2016-08-26 10:47:10 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
Excon.expects(:post).with { |_req, _body|
|
2017-06-16 08:08:15 +08:00
|
|
|
headers = _body[:headers]
|
|
|
|
body = _body[:body]
|
2016-09-04 13:50:53 +08:00
|
|
|
}.returns("OK")
|
2016-08-26 10:47:10 +08:00
|
|
|
|
2016-09-04 13:50:53 +08: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-11 10:03:21 +08:00
|
|
|
'excerpt' => 'Hello @eviltrout ❤',
|
2016-09-04 13:50:53 +08: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-11 10:03:21 +08:00
|
|
|
'excerpt' => 'Hello @eviltrout ❤',
|
2016-09-04 13:50:53 +08:00
|
|
|
'username' => user.username,
|
|
|
|
'url' => UrlHelper.absolute(mention_post.url),
|
|
|
|
'client_id' => 'xxx1'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-06-16 08:08:15 +08:00
|
|
|
mention_post
|
|
|
|
|
2016-09-04 13:50:53 +08:00
|
|
|
expect(JSON.parse(body)).to eq(payload)
|
2017-06-16 08:08:15 +08:00
|
|
|
expect(headers["Content-Type"]).to eq('application/json')
|
2016-08-26 10:47:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 03:56:40 +08: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-20 03:57:05 +08:00
|
|
|
PostAlerter.new.after_save_post(post, true)
|
2016-07-07 03:56:40 +08:00
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:watching_first_post]).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2016-07-20 03:57:05 +08: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-07 03:56:40 +08: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-22 03:05:10 +08:00
|
|
|
topic.topic_allowed_groups.create(group_id: group.id)
|
2016-07-07 03:56:40 +08: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-20 04:51:32 +08: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-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "replies" do
|
|
|
|
it "triggers :before_create_notifications_for_users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
post = Fabricate(:post, user: user, topic: topic)
|
|
|
|
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], reply])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
2017-07-20 04:51:32 +08:00
|
|
|
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-28 09:20:09 +08:00
|
|
|
expect(events).to include(event_name: :before_create_notifications_for_users, params: [[user], post])
|
2017-07-20 04:51:32 +08:00
|
|
|
end
|
|
|
|
end
|
2016-07-07 03:56:40 +08:00
|
|
|
end
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|