2018-02-12 22:15:57 +01:00
|
|
|
// Dependencies
|
2018-04-04 12:55:34 +02:00
|
|
|
set_global('$', global.make_zjquery({
|
|
|
|
silent: true,
|
|
|
|
}));
|
2018-05-16 00:50:52 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2018-02-12 22:15:57 +01:00
|
|
|
set_global('document', {
|
|
|
|
hasFocus: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
set_global('page_params', {
|
|
|
|
is_admin: false,
|
|
|
|
realm_users: [],
|
2019-12-10 21:54:08 +01:00
|
|
|
enable_desktop_notifications: true,
|
|
|
|
enable_sounds: true,
|
2018-02-12 22:15:57 +01:00
|
|
|
});
|
2019-05-16 06:27:37 +02:00
|
|
|
const _navigator = {
|
|
|
|
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/64.0.3282.167 Safari/537.36',
|
|
|
|
};
|
|
|
|
set_global('navigator', _navigator);
|
2018-04-04 12:55:34 +02:00
|
|
|
|
2019-12-10 21:54:08 +01:00
|
|
|
zrequire('alert_words');
|
2018-04-04 12:55:34 +02:00
|
|
|
zrequire('muting');
|
|
|
|
zrequire('stream_data');
|
|
|
|
zrequire('people');
|
2018-12-22 23:15:04 +01:00
|
|
|
zrequire('ui');
|
|
|
|
zrequire('util');
|
2018-02-12 22:15:57 +01:00
|
|
|
|
|
|
|
zrequire('notifications');
|
|
|
|
|
|
|
|
// Not muted streams
|
2019-11-02 00:06:25 +01:00
|
|
|
const general = {
|
2018-02-12 22:15:57 +01:00
|
|
|
subscribed: true,
|
2018-12-15 17:58:56 +01:00
|
|
|
name: 'general',
|
|
|
|
stream_id: 10,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2018-02-12 22:15:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Muted streams
|
2019-11-02 00:06:25 +01:00
|
|
|
const muted = {
|
2018-02-12 22:15:57 +01:00
|
|
|
subscribed: true,
|
2018-12-15 17:58:56 +01:00
|
|
|
name: 'muted',
|
|
|
|
stream_id: 20,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2018-02-12 22:15:57 +01:00
|
|
|
};
|
|
|
|
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_data.add_sub('general', general);
|
|
|
|
stream_data.add_sub('muted', muted);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2018-12-15 17:58:56 +01:00
|
|
|
muting.add_muted_topic(general.stream_id, 'muted topic');
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('message_is_notifiable', () => {
|
2018-02-12 22:15:57 +01:00
|
|
|
// Case 1: If the message was sent by this user,
|
|
|
|
// DO NOT notify the user
|
2018-05-07 03:30:13 +02:00
|
|
|
// In this test, all other circumstances should trigger notification
|
|
|
|
// EXCEPT sent_by_me, which should trump them
|
2019-12-10 21:46:56 +01:00
|
|
|
let message = {
|
2018-12-15 17:58:56 +01:00
|
|
|
id: muted.stream_id,
|
2018-05-07 03:30:13 +02:00
|
|
|
content: 'message number 1',
|
|
|
|
sent_by_me: true,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: general.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
|
|
|
// Not notifiable because it was sent by the current user
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), false);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
|
|
|
// Case 2: If the user has already been sent a notificaton about this message,
|
|
|
|
// DO NOT notify the user
|
2018-05-07 03:30:13 +02:00
|
|
|
// In this test, all other circumstances should trigger notification
|
|
|
|
// EXCEPT notification_sent, which should trump them
|
|
|
|
// (ie: it mentions user, it's not muted, etc)
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-12-15 17:58:56 +01:00
|
|
|
id: general.stream_id,
|
2018-05-07 03:30:13 +02:00
|
|
|
content: 'message number 2',
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: true,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: general.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), false);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
|
|
|
// Case 3: If a message mentions the user directly,
|
|
|
|
// DO notify the user
|
2018-05-07 03:30:13 +02:00
|
|
|
// Mentioning trumps muting
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 30,
|
2018-12-15 17:58:56 +01:00
|
|
|
content: 'message number 3',
|
2018-05-07 03:30:13 +02:00
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'muted',
|
|
|
|
stream_id: muted.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'topic_three',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), true);
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2018-12-15 17:58:56 +01:00
|
|
|
// Case 4:
|
2018-05-07 03:30:13 +02:00
|
|
|
// Mentioning should trigger notification in unmuted topic
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 40,
|
|
|
|
content: 'message number 4',
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: general.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'vanilla',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), true);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2018-12-15 17:58:56 +01:00
|
|
|
// Case 5: If a message is in a muted stream
|
2018-02-12 22:15:57 +01:00
|
|
|
// and does not mention the user DIRECTLY,
|
|
|
|
// DO NOT notify the user
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 50,
|
|
|
|
content: 'message number 5',
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: false,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'muted',
|
|
|
|
stream_id: muted.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), false);
|
2018-12-15 17:58:56 +01:00
|
|
|
|
|
|
|
// Case 6: If a message is in a muted topic
|
|
|
|
// and does not mention the user DIRECTLY,
|
|
|
|
// DO NOT notify the user
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-12-15 17:58:56 +01:00
|
|
|
id: 50,
|
|
|
|
content: 'message number 6',
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-12-15 17:58:56 +01:00
|
|
|
mentioned_me_directly: false,
|
|
|
|
type: 'stream',
|
|
|
|
stream: 'general',
|
|
|
|
stream_id: general.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'muted topic',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), false);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2018-12-15 17:58:56 +01:00
|
|
|
// Case 7
|
2018-05-07 03:30:13 +02:00
|
|
|
// If none of the above cases apply
|
|
|
|
// (ie: topic is not muted, message does not mention user,
|
|
|
|
// no notification sent before, message not sent by user),
|
|
|
|
// return true to pass it to notifications settings
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 60,
|
2018-12-15 17:58:56 +01:00
|
|
|
content: 'message number 7',
|
2018-05-07 03:30:13 +02:00
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 21:54:08 +01:00
|
|
|
mentioned: true,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: false,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: general.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2019-12-10 21:54:08 +01:00
|
|
|
assert.equal(notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:46:56 +01:00
|
|
|
assert.equal(notifications.message_is_notifiable(message), true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basic_notifications', () => {
|
2018-04-04 12:55:34 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let n; // Object for storing all notification data for assertions.
|
|
|
|
let last_closed_message_id = null;
|
|
|
|
let last_shown_message_id = null;
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Notifications API stub
|
|
|
|
notifications.set_notification_api({
|
|
|
|
createNotification: function createNotification(icon, title, content, tag) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const notification_object = {icon: icon, body: content, tag: tag};
|
2018-04-04 12:55:34 +02:00
|
|
|
// properties for testing.
|
|
|
|
notification_object.tests = {
|
|
|
|
shown: false,
|
|
|
|
};
|
|
|
|
notification_object.show = function () {
|
|
|
|
last_shown_message_id = this.tag;
|
|
|
|
};
|
|
|
|
notification_object.close = function () {
|
|
|
|
last_closed_message_id = this.tag;
|
|
|
|
};
|
|
|
|
notification_object.cancel = function () { notification_object.close(); };
|
|
|
|
return notification_object;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_1 = {
|
2018-04-04 12:55:34 +02:00
|
|
|
id: 1000,
|
|
|
|
content: '@-mentions the user',
|
2018-05-10 21:18:23 +02:00
|
|
|
avatar_url: 'url',
|
2018-04-04 12:55:34 +02:00
|
|
|
sent_by_me: false,
|
2018-12-15 18:14:15 +01:00
|
|
|
sender_full_name: 'Jesse Pinkman',
|
2018-04-04 12:55:34 +02:00
|
|
|
notification_sent: false,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: muted.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'whatever',
|
2018-04-04 12:55:34 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_2 = {
|
2018-04-04 12:55:34 +02:00
|
|
|
id: 1500,
|
2018-05-10 21:18:23 +02:00
|
|
|
avatar_url: 'url',
|
2018-04-04 12:55:34 +02:00
|
|
|
content: '@-mentions the user',
|
|
|
|
sent_by_me: false,
|
2018-12-15 18:14:15 +01:00
|
|
|
sender_full_name: 'Gus Fring',
|
2018-04-04 12:55:34 +02:00
|
|
|
notification_sent: false,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: 'stream',
|
2018-12-15 17:58:56 +01:00
|
|
|
stream: 'general',
|
|
|
|
stream_id: muted.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'lunch',
|
2018-04-04 12:55:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Send notification.
|
2019-05-16 06:27:37 +02:00
|
|
|
notifications.process_notification({message: message_1, desktop_notify: true});
|
2018-04-04 12:55:34 +02:00
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, true);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 1);
|
|
|
|
assert.equal(last_shown_message_id, message_1.id);
|
|
|
|
|
|
|
|
// Remove notification.
|
|
|
|
notifications.close_notification(message_1);
|
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, false);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 0);
|
|
|
|
assert.equal(last_closed_message_id, message_1.id);
|
|
|
|
|
|
|
|
// Send notification.
|
|
|
|
message_1.id = 1001;
|
2019-05-16 06:27:37 +02:00
|
|
|
notifications.process_notification({message: message_1, desktop_notify: true});
|
2018-04-04 12:55:34 +02:00
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, true);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 1);
|
|
|
|
assert.equal(last_shown_message_id, message_1.id);
|
|
|
|
|
|
|
|
// Process same message again. Notification count shouldn't increase.
|
|
|
|
message_1.id = 1002;
|
2019-05-16 06:27:37 +02:00
|
|
|
notifications.process_notification({message: message_1, desktop_notify: true});
|
2018-04-04 12:55:34 +02:00
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, true);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 1);
|
|
|
|
assert.equal(last_shown_message_id, message_1.id);
|
|
|
|
|
|
|
|
// Send another message. Notification count should increase.
|
2019-05-16 06:27:37 +02:00
|
|
|
notifications.process_notification({message: message_2, desktop_notify: true});
|
2018-04-04 12:55:34 +02:00
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Gus Fring to general > lunch' in n, true);
|
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, true);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 2);
|
|
|
|
assert.equal(last_shown_message_id, message_2.id);
|
|
|
|
|
|
|
|
// Remove notifications.
|
|
|
|
notifications.close_notification(message_1);
|
|
|
|
notifications.close_notification(message_2);
|
|
|
|
n = notifications.get_notifications();
|
2018-12-15 18:14:15 +01:00
|
|
|
assert.equal('Jesse Pinkman to general > whatever' in n, false);
|
2018-04-04 12:55:34 +02:00
|
|
|
assert.equal(Object.keys(n).length, 0);
|
|
|
|
assert.equal(last_closed_message_id, message_2.id);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|