2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
|
|
|
const $ = require("./lib/zjquery.cjs");
|
|
|
|
const {page_params} = require("./lib/zpage_params.cjs");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2024-10-01 00:04:14 +02:00
|
|
|
mock_esm("../src/electron_bridge");
|
2023-08-04 23:40:48 +02:00
|
|
|
mock_esm("../src/spoilers", {hide_spoilers_in_notification() {}});
|
|
|
|
|
2022-08-14 15:33:13 +02:00
|
|
|
const user_topics = zrequire("user_topics");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2023-10-07 00:15:22 +02:00
|
|
|
const desktop_notifications = zrequire("desktop_notifications");
|
2023-10-06 22:29:25 +02:00
|
|
|
const message_notifications = zrequire("message_notifications");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_current_user} = zrequire("state_data");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
const current_user = {};
|
|
|
|
set_current_user(current_user);
|
2024-10-09 08:44:21 +02:00
|
|
|
const user_settings = {};
|
|
|
|
initialize_user_settings({user_settings});
|
2018-02-12 22:15:57 +01:00
|
|
|
|
|
|
|
// Not muted streams
|
2019-11-02 00:06:25 +01:00
|
|
|
const general = {
|
2018-02-12 22:15:57 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "general",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: 10,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2019-12-10 22:03:40 +01:00
|
|
|
wildcard_mentions_notify: null,
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "muted",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: 20,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2019-12-10 22:03:40 +01:00
|
|
|
wildcard_mentions_notify: null,
|
2018-02-12 22:15:57 +01:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(general);
|
|
|
|
stream_data.add_sub(muted);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2023-04-11 07:48:13 +02:00
|
|
|
user_topics.update_user_topics(
|
|
|
|
general.stream_id,
|
2024-03-30 23:39:51 +01:00
|
|
|
general.name,
|
2023-04-11 07:48:13 +02:00
|
|
|
"muted topic",
|
|
|
|
user_topics.all_visibility_policies.MUTED,
|
|
|
|
);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
user_topics.update_user_topics(
|
|
|
|
general.stream_id,
|
2024-03-30 23:39:51 +01:00
|
|
|
general.name,
|
2023-06-15 08:33:19 +02:00
|
|
|
"followed topic",
|
|
|
|
user_topics.all_visibility_policies.FOLLOWED,
|
|
|
|
);
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2024-10-09 21:21:41 +02:00
|
|
|
helpers.override(current_user, "is_admin", false);
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.realm_users = [];
|
2024-10-09 21:10:22 +02:00
|
|
|
helpers.override(user_settings, "enable_followed_topic_desktop_notifications", true);
|
|
|
|
helpers.override(user_settings, "enable_followed_topic_audible_notifications", true);
|
|
|
|
helpers.override(user_settings, "enable_desktop_notifications", true);
|
|
|
|
helpers.override(user_settings, "enable_sounds", true);
|
|
|
|
helpers.override(user_settings, "enable_followed_topic_wildcard_mentions_notify", true);
|
|
|
|
helpers.override(user_settings, "wildcard_mentions_notify", true);
|
|
|
|
helpers.override(user_settings, "notification_sound", "ding");
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-04-03 19:07:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
test("message_is_notifiable", ({override}) => {
|
2019-12-10 22:03:40 +01:00
|
|
|
// A notification is sent if both message_is_notifiable(message)
|
|
|
|
// and the appropriate should_send_*_notification function return
|
|
|
|
// true.
|
|
|
|
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 1",
|
2018-05-07 03:30:13 +02:00
|
|
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
2019-12-10 21:54:08 +01:00
|
|
|
// Not notifiable because it was sent by the current user
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), false);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2020-03-28 01:25:56 +01:00
|
|
|
// Case 2: If the user has already been sent a notification about this message,
|
2018-02-12 22:15:57 +01:00
|
|
|
// 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,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 2",
|
2018-05-07 03:30:13 +02:00
|
|
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_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,
|
2020-07-15 01:29:15 +02: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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: muted.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "topic_three",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 4: If the message has been sent to a followed topic,
|
2023-06-15 09:06:42 +02:00
|
|
|
// DO visually and audibly notify the user if 'enable_followed_topic_desktop_notifications'
|
|
|
|
// and 'enable_followed_topic_audible_notifications' are enabled, respectively.
|
2023-06-15 08:33:19 +02:00
|
|
|
// Messages to followed topics trumps muting
|
|
|
|
message = {
|
|
|
|
id: 30,
|
|
|
|
content: "message number 3",
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
|
|
|
mentioned: false,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: general.stream_id,
|
|
|
|
topic: "followed topic",
|
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
2023-06-15 09:06:42 +02:00
|
|
|
// But not if 'enable_followed_topic_desktop_notifications'
|
|
|
|
// and 'enable_followed_topic_audible_notifications' are disabled.
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "enable_followed_topic_desktop_notifications", false);
|
|
|
|
override(user_settings, "enable_followed_topic_audible_notifications", false);
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), false);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), false);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
|
|
|
// Reset state
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "enable_followed_topic_desktop_notifications", true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
|
|
|
// Case 5:
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 4",
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "vanilla",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 6:
|
2019-12-10 22:03:40 +01:00
|
|
|
// Wildcard mention should trigger notification in unmuted topic
|
|
|
|
// if wildcard_mentions_notify
|
|
|
|
message = {
|
|
|
|
id: 40,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 4",
|
2019-12-10 22:03:40 +01:00
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2019-12-10 22:03:40 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "vanilla",
|
2019-12-10 22:03:40 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2019-12-10 22:03:40 +01:00
|
|
|
|
|
|
|
// But not if it's disabled
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "wildcard_mentions_notify", false);
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), false);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), false);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2019-12-10 22:03:40 +01:00
|
|
|
|
|
|
|
// And the stream-level setting overrides the global setting
|
|
|
|
general.wildcard_mentions_notify = true;
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2019-12-10 22:03:40 +01:00
|
|
|
|
|
|
|
// Reset state
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "wildcard_mentions_notify", true);
|
2019-12-10 22:03:40 +01:00
|
|
|
general.wildcard_mentions_notify = null;
|
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 7: If a message is in a muted stream
|
2019-12-10 22:03:40 +01:00
|
|
|
// and does not mention the user DIRECTLY (i.e. wildcard mention),
|
2018-02-12 22:15:57 +01:00
|
|
|
// DO NOT notify the user
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 50,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 5",
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: muted.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), false);
|
2018-12-15 17:58:56 +01:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 8: If a message is in a muted stream
|
2019-12-10 22:03:40 +01:00
|
|
|
// and does mention the user DIRECTLY,
|
|
|
|
// DO notify the user
|
|
|
|
message = {
|
|
|
|
id: 50,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 5",
|
2019-12-10 22:03:40 +01:00
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2019-12-10 22:03:40 +01:00
|
|
|
stream_id: muted.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2019-12-10 22:03:40 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2019-12-10 22:03:40 +01:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 9: If a message is in a muted topic
|
2019-12-10 22:03:40 +01:00
|
|
|
// and does not mention the user DIRECTLY (i.e. wildcard mention),
|
2018-12-15 17:58:56 +01:00
|
|
|
// DO NOT notify the user
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-12-15 17:58:56 +01:00
|
|
|
id: 50,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "message number 6",
|
2018-12-15 17:58:56 +01:00
|
|
|
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,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "muted topic",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), false);
|
2018-02-12 22:15:57 +01:00
|
|
|
|
2023-06-15 08:33:19 +02:00
|
|
|
// Case 10:
|
|
|
|
// Wildcard mentions in a followed topic with 'wildcard_mentions_notify',
|
2023-06-15 09:06:42 +02:00
|
|
|
// 'enable_followed_topic_desktop_notifications',
|
|
|
|
// 'enable_followed_topic_audible_notifications' disabled and
|
2023-06-15 08:33:19 +02:00
|
|
|
// 'enable_followed_topic_wildcard_mentions_notify' enabled;
|
2023-06-15 09:06:42 +02:00
|
|
|
// DO visually and audibly notify the user
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "wildcard_mentions_notify", false);
|
|
|
|
override(user_settings, "enable_followed_topic_desktop_notifications", false);
|
|
|
|
override(user_settings, "enable_followed_topic_audible_notifications", false);
|
2023-06-15 08:33:19 +02:00
|
|
|
message = {
|
|
|
|
id: 50,
|
|
|
|
content: "message number 5",
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: false,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: general.stream_id,
|
|
|
|
topic: "followed topic",
|
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), true);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
|
|
|
// But not if 'enable_followed_topic_wildcard_mentions_notify' is disabled
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "enable_followed_topic_wildcard_mentions_notify", false);
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), false);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), false);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
|
|
|
// Reset state
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "wildcard_mentions_notify", true);
|
|
|
|
override(user_settings, "enable_followed_topic_desktop_notifications", true);
|
|
|
|
override(user_settings, "enable_followed_topic_audible_notifications", true);
|
|
|
|
override(user_settings, "enable_followed_topic_wildcard_mentions_notify", true);
|
2023-06-15 08:33:19 +02:00
|
|
|
|
|
|
|
// Case 11: If `None` is selected as the notification sound, send no
|
2021-04-28 01:04:06 +02:00
|
|
|
// audible notification, no matter what other user configurations are.
|
|
|
|
message = {
|
|
|
|
id: 50,
|
|
|
|
content: "message number 7",
|
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
|
|
|
mentioned: true,
|
|
|
|
mentioned_me_directly: true,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: general.stream_id,
|
|
|
|
topic: "whatever",
|
|
|
|
};
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "notification_sound", "none");
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), true);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), false);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2021-04-28 01:04:06 +02:00
|
|
|
|
|
|
|
// Reset state
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "notification_sound", "ding");
|
2021-04-28 01:04:06 +02:00
|
|
|
|
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),
|
2019-12-10 22:03:40 +01:00
|
|
|
// return true to pass it to notifications settings, which will return false.
|
2019-12-10 21:46:56 +01:00
|
|
|
message = {
|
2018-05-07 03:30:13 +02:00
|
|
|
id: 60,
|
2021-04-28 01:04:06 +02:00
|
|
|
content: "message number 8",
|
2018-05-07 03:30:13 +02:00
|
|
|
sent_by_me: false,
|
|
|
|
notification_sent: false,
|
2019-12-10 22:03:40 +01:00
|
|
|
mentioned: false,
|
2018-05-07 03:30:13 +02:00
|
|
|
mentioned_me_directly: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-15 17:58:56 +01:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "whatever",
|
2019-12-10 21:46:56 +01:00
|
|
|
};
|
2023-10-06 22:29:25 +02:00
|
|
|
assert.equal(message_notifications.should_send_desktop_notification(message), false);
|
|
|
|
assert.equal(message_notifications.should_send_audible_notification(message), false);
|
|
|
|
assert.equal(message_notifications.message_is_notifiable(message), true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-04 12:55:34 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("basic_notifications", () => {
|
2024-04-03 02:51:03 +02:00
|
|
|
$("<div>").set_find_results(".emoji", {text: () => ({contents: () => ({unwrap() {}})})});
|
2023-11-27 07:35:08 +01:00
|
|
|
$("<div>").set_find_results("span.katex", {each() {}});
|
2024-01-22 20:03:18 +01:00
|
|
|
$("<div>").children = () => [];
|
2021-02-21 17:00:49 +01: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
|
2020-04-27 00:20:20 +02:00
|
|
|
class StubNotification {
|
2023-06-29 21:59:08 +02:00
|
|
|
constructor(_title, {icon, body, tag}) {
|
2020-04-27 00:20:20 +02:00
|
|
|
this.icon = icon;
|
|
|
|
this.body = body;
|
|
|
|
this.tag = tag;
|
2018-04-04 12:55:34 +02:00
|
|
|
// properties for testing.
|
2020-04-27 00:20:20 +02:00
|
|
|
this.tests = {
|
2018-04-04 12:55:34 +02:00
|
|
|
shown: false,
|
|
|
|
};
|
2020-04-27 00:20:20 +02:00
|
|
|
last_shown_message_id = this.tag;
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:37:01 +02:00
|
|
|
addEventListener() {}
|
|
|
|
|
2020-04-27 00:20:20 +02:00
|
|
|
close() {
|
|
|
|
last_closed_message_id = this.tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-07 00:15:22 +02:00
|
|
|
desktop_notifications.set_notification_api(StubNotification);
|
2018-04-04 12:55:34 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_1 = {
|
2018-04-04 12:55:34 +02:00
|
|
|
id: 1000,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "@-mentions the user",
|
|
|
|
avatar_url: "url",
|
2018-04-04 12:55:34 +02:00
|
|
|
sent_by_me: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
sender_full_name: "Jesse Pinkman",
|
2018-04-04 12:55:34 +02:00
|
|
|
notification_sent: false,
|
|
|
|
mentioned_me_directly: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2023-07-26 22:07:21 +02:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02: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,
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "url",
|
|
|
|
content: "@-mentions the user",
|
2018-04-04 12:55:34 +02:00
|
|
|
sent_by_me: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
sender_full_name: "Gus Fring",
|
2018-04-04 12:55:34 +02:00
|
|
|
notification_sent: false,
|
|
|
|
mentioned_me_directly: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2023-07-26 22:07:21 +02:00
|
|
|
stream_id: general.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
2018-04-04 12:55:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Send notification.
|
2023-10-06 22:29:25 +02:00
|
|
|
message_notifications.process_notification({message: message_1, desktop_notify: true});
|
2023-10-07 00:15:22 +02:00
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), true);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 1);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_shown_message_id, message_1.id.toString());
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Remove notification.
|
2023-10-07 00:15:22 +02:00
|
|
|
desktop_notifications.close_notification(message_1);
|
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), false);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 0);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_closed_message_id, message_1.id.toString());
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Send notification.
|
|
|
|
message_1.id = 1001;
|
2023-10-06 22:29:25 +02:00
|
|
|
message_notifications.process_notification({message: message_1, desktop_notify: true});
|
2023-10-07 00:15:22 +02:00
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), true);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 1);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_shown_message_id, message_1.id.toString());
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Process same message again. Notification count shouldn't increase.
|
|
|
|
message_1.id = 1002;
|
2023-10-06 22:29:25 +02:00
|
|
|
message_notifications.process_notification({message: message_1, desktop_notify: true});
|
2023-10-07 00:15:22 +02:00
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), true);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 1);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_shown_message_id, message_1.id.toString());
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Send another message. Notification count should increase.
|
2023-10-06 22:29:25 +02:00
|
|
|
message_notifications.process_notification({message: message_2, desktop_notify: true});
|
2023-10-07 00:15:22 +02:00
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Gus Fring to general > lunch"), true);
|
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), true);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 2);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_shown_message_id, message_2.id.toString());
|
2018-04-04 12:55:34 +02:00
|
|
|
|
|
|
|
// Remove notifications.
|
2023-10-07 00:15:22 +02:00
|
|
|
desktop_notifications.close_notification(message_1);
|
|
|
|
desktop_notifications.close_notification(message_2);
|
|
|
|
n = desktop_notifications.get_notifications();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(n.has("Jesse Pinkman to general > whatever"), false);
|
2020-02-06 01:12:42 +01:00
|
|
|
assert.equal(n.size, 0);
|
2024-09-24 00:21:45 +02:00
|
|
|
assert.equal(last_closed_message_id, message_2.id.toString());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|