2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Notification do
|
2013-05-14 09:59:55 +08:00
|
|
|
before do
|
2016-12-22 12:29:34 +08:00
|
|
|
NotificationEmailer.enable
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
it { is_expected.to validate_presence_of :notification_type }
|
|
|
|
it { is_expected.to validate_presence_of :data }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
it { is_expected.to belong_to :user }
|
|
|
|
it { is_expected.to belong_to :topic }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-01-08 18:53:52 +08:00
|
|
|
describe '#types' do
|
|
|
|
context "verify enum sequence" do
|
|
|
|
before do
|
|
|
|
@types = Notification.types
|
|
|
|
end
|
|
|
|
|
2020-08-19 18:07:51 +08:00
|
|
|
it "has a correct position for each type" do
|
2016-01-08 18:53:52 +08:00
|
|
|
expect(@types[:mentioned]).to eq(1)
|
2020-08-19 18:07:51 +08:00
|
|
|
expect(@types[:replied]).to eq(2)
|
|
|
|
expect(@types[:quoted]).to eq(3)
|
|
|
|
expect(@types[:edited]).to eq(4)
|
|
|
|
expect(@types[:liked]).to eq(5)
|
|
|
|
expect(@types[:private_message]).to eq(6)
|
|
|
|
expect(@types[:invited_to_private_message]).to eq(7)
|
|
|
|
expect(@types[:invitee_accepted]).to eq(8)
|
|
|
|
expect(@types[:posted]).to eq(9)
|
|
|
|
expect(@types[:moved_post]).to eq(10)
|
|
|
|
expect(@types[:linked]).to eq(11)
|
|
|
|
expect(@types[:granted_badge]).to eq(12)
|
|
|
|
expect(@types[:invited_to_topic]).to eq(13)
|
|
|
|
expect(@types[:custom]).to eq(14)
|
2016-01-08 18:53:52 +08:00
|
|
|
expect(@types[:group_mentioned]).to eq(15)
|
2020-08-19 18:07:51 +08:00
|
|
|
expect(@types[:group_message_summary]).to eq(16)
|
|
|
|
expect(@types[:watching_first_post]).to eq(17)
|
|
|
|
expect(@types[:topic_reminder]).to eq(18)
|
|
|
|
expect(@types[:liked_consolidated]).to eq(19)
|
|
|
|
expect(@types[:post_approved]).to eq(20)
|
|
|
|
expect(@types[:code_review_commit_approved]).to eq(21)
|
|
|
|
expect(@types[:membership_request_accepted]).to eq(22)
|
|
|
|
expect(@types[:membership_request_consolidated]).to eq(23)
|
|
|
|
expect(@types[:bookmark_reminder]).to eq(24)
|
|
|
|
expect(@types[:reaction]).to eq(25)
|
|
|
|
expect(@types[:votes_released]).to eq(26)
|
|
|
|
expect(@types[:event_reminder]).to eq(27)
|
|
|
|
expect(@types[:event_invitation]).to eq(28)
|
2016-01-08 18:53:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-14 09:59:55 +08:00
|
|
|
describe 'post' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post_args) do
|
|
|
|
{ user: topic.user, topic: topic }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
describe 'replies' do
|
2014-03-18 12:22:39 +08:00
|
|
|
def process_alerts(post)
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:post) {
|
|
|
|
process_alerts(Fabricate(:post, post_args.merge(raw: "Hello @CodingHorror")))
|
|
|
|
}
|
2013-05-14 09:59:55 +08:00
|
|
|
|
|
|
|
it 'notifies the poster on reply' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
reply = Fabricate(:basic_reply, user: coding_horror, topic: post.topic)
|
|
|
|
process_alerts(reply)
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to change(post.user.notifications, :count).by(1)
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify the poster when they reply to their own post" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
reply = Fabricate(:basic_reply, user: post.user, topic: post.topic)
|
|
|
|
process_alerts(reply)
|
2015-01-06 00:04:23 +08:00
|
|
|
}.not_to change(post.user.notifications, :count)
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'watching' do
|
|
|
|
it "does notify watching users of new posts" do
|
2014-03-18 12:22:39 +08:00
|
|
|
post = PostAlerter.post_created(Fabricate(:post, post_args))
|
2013-05-14 09:59:55 +08:00
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
post_args[:topic].notify_watch!(user2)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
PostAlerter.post_created(Fabricate(:post, user: post.user, topic: post.topic))
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to change(user2.notifications, :count).by(1)
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'muting' do
|
|
|
|
it "does not notify users of new posts" do
|
|
|
|
post = Fabricate(:post, post_args)
|
|
|
|
user = post_args[:user]
|
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
|
|
|
|
post_args[:topic].notify_muted!(user)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2013-05-14 09:59:55 +08:00
|
|
|
Fabricate(:post, user: user2, topic: post.topic, raw: 'hello @' + user.username)
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to change(user.notifications, :count).by(0)
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2016-02-02 02:12:10 +08:00
|
|
|
|
2020-05-07 12:35:32 +08:00
|
|
|
describe 'high priority creation' do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "automatically marks the notification as high priority if it is a high priority type" do
|
|
|
|
notif = Notification.create(user: user, notification_type: Notification.types[:bookmark_reminder], data: {})
|
|
|
|
expect(notif.high_priority).to eq(true)
|
|
|
|
notif = Notification.create(user: user, notification_type: Notification.types[:private_message], data: {})
|
|
|
|
expect(notif.high_priority).to eq(true)
|
|
|
|
notif = Notification.create(user: user, notification_type: Notification.types[:liked], data: {})
|
|
|
|
expect(notif.high_priority).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows manually specifying a notification is high priority" do
|
|
|
|
notif = Notification.create(user: user, notification_type: Notification.types[:liked], data: {}, high_priority: true)
|
|
|
|
expect(notif.high_priority).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'unread counts' do
|
|
|
|
|
2019-05-10 18:59:31 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'a regular notification' do
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'increases unread_notifications' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:notification, user: user); user.reload }.to change(user, :unread_notifications)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-10-13 18:26:30 +08:00
|
|
|
it 'increases total_unread_notifications' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:notification, user: user); user.reload }.to change(user, :total_unread_notifications)
|
2014-10-13 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "doesn't increase unread_private_messages" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:notification, user: user); user.reload }.not_to change(user, :unread_private_messages)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'a private message' do
|
|
|
|
it "doesn't increase unread_notifications" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:private_message_notification, user: user); user.reload }.not_to change(user, :unread_notifications)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-10-13 18:26:30 +08:00
|
|
|
it 'increases total_unread_notifications' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:notification, user: user); user.reload }.to change(user, :total_unread_notifications)
|
2014-10-13 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "increases unread_private_messages" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:private_message_notification, user: user); user.reload }.to change(user, :unread_private_messages)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2020-04-01 07:09:20 +08:00
|
|
|
|
|
|
|
it "increases unread_high_priority_notifications" do
|
|
|
|
expect { Fabricate(:private_message_notification, user: user); user.reload }.to change(user, :unread_high_priority_notifications)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a bookmark reminder message' do
|
|
|
|
it "doesn't increase unread_notifications" do
|
|
|
|
expect { Fabricate(:bookmark_reminder_notification, user: user); user.reload }.not_to change(user, :unread_notifications)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increases total_unread_notifications' do
|
|
|
|
expect { Fabricate(:notification, user: user); user.reload }.to change(user, :total_unread_notifications)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "increases unread_high_priority_notifications" do
|
|
|
|
expect { Fabricate(:bookmark_reminder_notification, user: user); user.reload }.to change(user, :unread_high_priority_notifications)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'message bus' do
|
2020-02-14 16:19:40 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'updates the notification count on create' do
|
2013-05-16 13:03:03 +08:00
|
|
|
Notification.any_instance.expects(:refresh_notification_count).returns(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
Fabricate(:notification)
|
|
|
|
end
|
|
|
|
|
2020-02-14 16:19:40 +08:00
|
|
|
it 'works' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
user.notifications.create!(notification_type: Notification.types[:mentioned], data: '{}')
|
|
|
|
user.notifications.create!(notification_type: Notification.types[:mentioned], data: '{}')
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.size).to eq(2)
|
|
|
|
expect(messages[0].channel).to eq("/notification/#{user.id}")
|
|
|
|
expect(messages[0].data[:unread_notifications]).to eq(1)
|
|
|
|
expect(messages[1].channel).to eq("/notification/#{user.id}")
|
|
|
|
expect(messages[1].data[:unread_notifications]).to eq(2)
|
|
|
|
end
|
|
|
|
|
2019-10-22 02:33:58 +08:00
|
|
|
it 'works for partial model instances' do
|
|
|
|
NotificationEmailer.disable
|
2020-02-14 16:19:40 +08:00
|
|
|
partial_user = User.select(:id).find_by(id: user.id)
|
2019-10-22 02:33:58 +08:00
|
|
|
partial_user.notifications.create!(notification_type: Notification.types[:mentioned], data: '{}')
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'destroy' do
|
|
|
|
let!(:notification) { Fabricate(:notification) }
|
|
|
|
|
|
|
|
it 'updates the notification count on destroy' do
|
2013-05-16 13:03:03 +08:00
|
|
|
Notification.any_instance.expects(:refresh_notification_count).returns(nil)
|
2017-03-20 12:06:37 +08:00
|
|
|
notification.destroy!
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'private message' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic = Fabricate(:private_message_topic)
|
2013-03-23 23:02:59 +08:00
|
|
|
@post = Fabricate(:post, topic: @topic, user: @topic.user)
|
2013-02-06 03:16:51 +08:00
|
|
|
@target = @post.topic.topic_allowed_users.reject { |a| a.user_id == @post.user_id }[0].user
|
2016-03-22 11:50:12 +08:00
|
|
|
|
|
|
|
TopicUser.change(@target.id, @topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
PostAlerter.post_created(@post)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
it 'should create and rollup private message notifications' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@target.notifications.first.notification_type).to eq(Notification.types[:private_message])
|
|
|
|
expect(@post.user.unread_notifications).to eq(0)
|
|
|
|
expect(@post.user.total_unread_notifications).to eq(0)
|
|
|
|
expect(@target.unread_private_messages).to eq(1)
|
2014-01-20 13:18:43 +08:00
|
|
|
|
|
|
|
Fabricate(:post, topic: @topic, user: @topic.user)
|
|
|
|
@target.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@target.unread_private_messages).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-01-20 13:18:43 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.post' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let!(:notification) { Fabricate(:notification, user: post.user, topic: post.topic, post_number: post.post_number) }
|
|
|
|
|
|
|
|
it 'returns the post' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(notification.post).to eq(post)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'data' do
|
|
|
|
let(:notification) { Fabricate.build(:notification) }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should have a data hash' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(notification.data_hash).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should have the data within the json' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(notification.data_hash[:poison]).to eq('ivy')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-16 14:37:47 +08:00
|
|
|
describe 'saw_regular_notification_id' do
|
|
|
|
it 'correctly updates the read state' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2015-02-19 10:20:25 +08:00
|
|
|
t = Fabricate(:topic)
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
Notification.create!(read: false,
|
2013-05-16 14:37:47 +08:00
|
|
|
user_id: user.id,
|
2015-02-19 10:20:25 +08:00
|
|
|
topic_id: t.id,
|
2013-05-16 14:37:47 +08:00
|
|
|
post_number: 1,
|
2014-02-04 09:57:52 +08:00
|
|
|
data: '{}',
|
2013-05-16 14:37:47 +08:00
|
|
|
notification_type: Notification.types[:private_message])
|
|
|
|
|
2020-04-01 07:09:20 +08:00
|
|
|
Notification.create!(read: false,
|
|
|
|
user_id: user.id,
|
|
|
|
topic_id: t.id,
|
|
|
|
post_number: 1,
|
|
|
|
data: '{}',
|
|
|
|
notification_type: Notification.types[:bookmark_reminder])
|
|
|
|
|
2013-05-16 14:37:47 +08:00
|
|
|
other = Notification.create!(read: false,
|
|
|
|
user_id: user.id,
|
2015-02-19 10:20:25 +08:00
|
|
|
topic_id: t.id,
|
2013-05-16 14:37:47 +08:00
|
|
|
post_number: 1,
|
2014-02-04 09:57:52 +08:00
|
|
|
data: '{}',
|
2013-05-16 14:37:47 +08:00
|
|
|
notification_type: Notification.types[:mentioned])
|
|
|
|
|
|
|
|
user.saw_notification_id(other.id)
|
|
|
|
user.reload
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(user.unread_notifications).to eq(0)
|
2020-04-01 07:09:20 +08:00
|
|
|
expect(user.total_unread_notifications).to eq(3)
|
|
|
|
# NOTE: because of deprecation this will be equal to unread_high_priority_notifications,
|
|
|
|
# to be remonved in 2.5
|
|
|
|
expect(user.unread_private_messages).to eq(2)
|
|
|
|
expect(user.unread_high_priority_notifications).to eq(2)
|
2013-05-16 14:37:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'mark_posts_read' do
|
|
|
|
it "marks multiple posts as read if needed" do
|
2013-02-25 15:42:42 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
(1..3).map do |i|
|
2014-02-04 09:56:28 +08:00
|
|
|
Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '{}', notification_type: 1)
|
2013-02-25 15:42:42 +08:00
|
|
|
end
|
2014-02-04 09:56:28 +08:00
|
|
|
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '{}', notification_type: 1)
|
2013-02-25 15:42:42 +08:00
|
|
|
|
2015-12-16 23:44:52 +08:00
|
|
|
expect { Notification.mark_posts_read(user, 2, [1, 2, 3, 4]) }.to change { Notification.where(read: true).count }.by(3)
|
2013-02-25 15:42:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-01 07:09:20 +08:00
|
|
|
describe '#ensure_consistency!' do
|
2013-05-16 15:50:14 +08:00
|
|
|
it 'deletes notifications if post is missing or deleted' do
|
|
|
|
|
2016-12-22 12:29:34 +08:00
|
|
|
NotificationEmailer.disable
|
|
|
|
|
2013-05-16 15:50:14 +08:00
|
|
|
p = Fabricate(:post)
|
|
|
|
p2 = Fabricate(:post)
|
|
|
|
|
|
|
|
Notification.create!(read: false, user_id: p.user_id, topic_id: p.topic_id, post_number: p.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:private_message])
|
|
|
|
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:private_message])
|
2020-04-01 07:09:20 +08:00
|
|
|
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:bookmark_reminder])
|
2013-05-16 15:50:14 +08:00
|
|
|
|
|
|
|
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:liked])
|
2013-07-10 03:20:18 +08:00
|
|
|
p2.trash!(p.user)
|
2013-05-16 15:50:14 +08:00
|
|
|
|
|
|
|
# we may want to make notification "trashable" but for now we nuke pm notifications from deleted topics/posts
|
|
|
|
Notification.ensure_consistency!
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Notification.count).to eq(2)
|
2013-05-16 15:50:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-05 17:06:06 +08:00
|
|
|
describe '.filter_by_consolidation_data' do
|
2019-01-16 10:40:16 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
2019-05-10 18:59:31 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-01-16 10:40:16 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
PostActionNotifier.enable
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right notifications' do
|
2019-12-05 17:06:06 +08:00
|
|
|
expect(Notification.filter_by_consolidation_data(
|
|
|
|
Notification.types[:liked], display_username: user.username_lower
|
2019-01-17 14:31:07 +08:00
|
|
|
)).to eq([])
|
2019-01-16 10:40:16 +08:00
|
|
|
|
|
|
|
expect do
|
|
|
|
PostAlerter.post_created(Fabricate(:basic_reply,
|
|
|
|
user: user,
|
|
|
|
topic: post.topic
|
|
|
|
))
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(user, post)
|
2019-01-16 10:40:16 +08:00
|
|
|
end.to change { Notification.count }.by(2)
|
|
|
|
|
2019-12-05 17:06:06 +08:00
|
|
|
expect(Notification.filter_by_consolidation_data(
|
|
|
|
Notification.types[:liked], display_username: user.username_lower
|
2019-01-17 14:31:07 +08:00
|
|
|
)).to contain_exactly(
|
2019-01-16 10:40:16 +08:00
|
|
|
Notification.find_by(notification_type: Notification.types[:liked])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-18 23:03:51 +08:00
|
|
|
describe "do not disturb" do
|
|
|
|
it "calls NotificationEmailer.process_notification when user is not in 'do not disturb'" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
notification = Notification.new(read: false, user_id: user.id, topic_id: 2, post_number: 1, data: '{}', notification_type: 1)
|
|
|
|
NotificationEmailer.expects(:process_notification).with(notification)
|
|
|
|
notification.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't call NotificationEmailer.process_notification when user is in 'do not disturb'" do
|
|
|
|
freeze_time
|
|
|
|
user = Fabricate(:user)
|
|
|
|
Fabricate(:do_not_disturb_timing, user: user, starts_at: Time.zone.now, ends_at: 1.day.from_now)
|
|
|
|
|
|
|
|
notification = Notification.new(read: false, user_id: user.id, topic_id: 2, post_number: 1, data: '{}', notification_type: 1)
|
|
|
|
NotificationEmailer.expects(:process_notification).with(notification).never
|
|
|
|
notification.save!
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-02-13 14:12:17 +08:00
|
|
|
|
|
|
|
# pulling this out cause I don't want an observer
|
|
|
|
describe Notification do
|
|
|
|
describe '#recent_report' do
|
2019-05-10 18:59:31 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2014-02-13 14:12:17 +08:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
def fab(type, read)
|
|
|
|
@i ||= 0
|
|
|
|
@i += 1
|
|
|
|
Notification.create!(read: read, user_id: user.id, topic_id: post.topic_id, post_number: post.post_number, data: '[]',
|
|
|
|
notification_type: type, created_at: @i.days.from_now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_pm
|
|
|
|
fab(Notification.types[:private_message], false)
|
|
|
|
end
|
|
|
|
|
2020-04-01 07:09:20 +08:00
|
|
|
def unread_bookmark_reminder
|
|
|
|
fab(Notification.types[:bookmark_reminder], false)
|
|
|
|
end
|
|
|
|
|
2014-02-13 14:12:17 +08:00
|
|
|
def pm
|
|
|
|
fab(Notification.types[:private_message], true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def regular
|
|
|
|
fab(Notification.types[:liked], true)
|
|
|
|
end
|
|
|
|
|
2019-01-16 17:08:59 +08:00
|
|
|
def liked_consolidated
|
|
|
|
fab(Notification.types[:liked_consolidated], true)
|
|
|
|
end
|
|
|
|
|
2015-06-23 04:14:22 +08:00
|
|
|
it 'correctly finds visible notifications' do
|
|
|
|
pm
|
|
|
|
expect(Notification.visible.count).to eq(1)
|
|
|
|
post.topic.trash!
|
|
|
|
expect(Notification.visible.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2020-04-01 07:09:20 +08:00
|
|
|
it 'orders stuff by creation descending, bumping unread high priority (pms, bookmark reminders) to top' do
|
|
|
|
# note we expect the final order to read bottom-up for this list of variables,
|
|
|
|
# with unread pm + bookmark reminder at the top of that list
|
2014-02-13 14:12:17 +08:00
|
|
|
a = unread_pm
|
2020-04-01 07:09:20 +08:00
|
|
|
regular
|
|
|
|
b = unread_bookmark_reminder
|
2014-02-13 14:12:17 +08:00
|
|
|
c = pm
|
|
|
|
d = regular
|
|
|
|
|
2020-04-01 07:09:20 +08:00
|
|
|
notifications = Notification.recent_report(user, 4)
|
|
|
|
expect(notifications.map { |n| n.id }).to eq([b.id, a.id, d.id, c.id])
|
2014-02-13 14:12:17 +08:00
|
|
|
|
|
|
|
end
|
2019-01-16 17:08:59 +08:00
|
|
|
|
|
|
|
describe 'for a user that does not want to be notify on liked' do
|
|
|
|
before do
|
|
|
|
user.user_option.update!(
|
|
|
|
like_notification_frequency:
|
|
|
|
UserOption.like_notification_frequency_type[:never]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not return any form of liked notifications" do
|
|
|
|
notification = pm
|
|
|
|
regular
|
|
|
|
liked_consolidated
|
|
|
|
|
|
|
|
expect(Notification.recent_report(user)).to contain_exactly(notification)
|
|
|
|
end
|
|
|
|
end
|
2019-11-28 06:32:35 +08:00
|
|
|
|
|
|
|
describe '#consolidate_membership_requests' do
|
|
|
|
fab!(:group) { Fabricate(:group, name: "XXsssssddd") }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
def create_membership_request_notification
|
|
|
|
Notification.create(
|
|
|
|
notification_type: Notification.types[:private_message],
|
|
|
|
user_id: user.id,
|
|
|
|
data: {
|
|
|
|
topic_title: I18n.t('groups.request_membership_pm.title', group_name: group.name),
|
|
|
|
original_post_id: post.id
|
|
|
|
}.to_json,
|
|
|
|
updated_at: Time.zone.now,
|
|
|
|
created_at: Time.zone.now
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
PostCustomField.create!(post_id: post.id, name: "requested_group_id", value: group.id)
|
2019-12-05 17:06:06 +08:00
|
|
|
2.times { create_membership_request_notification }
|
2019-11-28 06:32:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should consolidate membership requests to a new notification' do
|
|
|
|
notification = create_membership_request_notification
|
|
|
|
notification.reload
|
|
|
|
|
|
|
|
notification = create_membership_request_notification
|
|
|
|
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
|
|
|
|
notification = Notification.last
|
2021-01-08 00:49:49 +08:00
|
|
|
expect(notification.processed).to eq(true)
|
2019-11-28 06:32:35 +08:00
|
|
|
expect(notification.notification_type).to eq(Notification.types[:membership_request_consolidated])
|
|
|
|
|
|
|
|
data = notification.data_hash
|
|
|
|
expect(data[:group_name]).to eq(group.name)
|
2019-12-05 17:06:06 +08:00
|
|
|
expect(data[:count]).to eq(4)
|
2019-11-28 06:32:35 +08:00
|
|
|
|
|
|
|
notification = create_membership_request_notification
|
|
|
|
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
|
2019-12-05 17:06:06 +08:00
|
|
|
expect(Notification.last.data_hash[:count]).to eq(5)
|
2019-11-28 06:32:35 +08:00
|
|
|
end
|
2021-01-08 00:49:49 +08:00
|
|
|
|
|
|
|
it 'consolidates membership requests with "processed" false if user is in DND' do
|
|
|
|
user.do_not_disturb_timings.create(starts_at: Time.now, ends_at: 3.days.from_now)
|
|
|
|
|
|
|
|
create_membership_request_notification
|
|
|
|
create_membership_request_notification
|
|
|
|
|
|
|
|
notification = Notification.last
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:membership_request_consolidated])
|
|
|
|
expect(notification.processed).to eq(false)
|
|
|
|
end
|
2019-11-28 06:32:35 +08:00
|
|
|
end
|
2014-02-13 14:12:17 +08:00
|
|
|
end
|
2020-02-24 08:42:50 +08:00
|
|
|
|
|
|
|
describe "purge_old!" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:notification1) { Fabricate(:notification, user: user) }
|
|
|
|
fab!(:notification2) { Fabricate(:notification, user: user) }
|
|
|
|
fab!(:notification3) { Fabricate(:notification, user: user) }
|
|
|
|
fab!(:notification4) { Fabricate(:notification, user: user) }
|
|
|
|
|
|
|
|
it "does nothing if set to 0" do
|
|
|
|
SiteSetting.max_notifications_per_user = 0
|
|
|
|
Notification.purge_old!
|
|
|
|
|
|
|
|
expect(Notification.where(user_id: user.id).count).to eq(4)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly limits" do
|
|
|
|
SiteSetting.max_notifications_per_user = 2
|
|
|
|
Notification.purge_old!
|
|
|
|
|
|
|
|
expect(Notification.where(user_id: user.id).pluck(:id)).to contain_exactly(notification4.id, notification3.id)
|
|
|
|
end
|
|
|
|
end
|
2021-01-08 00:49:49 +08:00
|
|
|
|
|
|
|
describe "processed" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "is false after creation when the user is in do not disturb" do
|
|
|
|
user.do_not_disturb_timings.create(starts_at: Time.now, ends_at: 3.days.from_now)
|
|
|
|
notification = Notification.create(read: false, user_id: user.id, topic_id: 2, post_number: 1, data: '{}', notification_type: 1)
|
|
|
|
expect(notification.processed).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true after creation when the user isn't in do not disturb" do
|
|
|
|
notification = Notification.create(read: false, user_id: user.id, topic_id: 2, post_number: 1, data: '{}', notification_type: 1)
|
|
|
|
expect(notification.processed).to be(true)
|
|
|
|
end
|
|
|
|
end
|
2014-02-13 14:12:17 +08:00
|
|
|
end
|