Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

desktop_notifications: Check notification permission when creating one.

Pratik Chandac2f359202e9e
diff --git a/web/src/desktop_notifications.ts b/web/src/desktop_notifications.ts
index b1243e2df2d..4262213b33c 100644
--- a/web/src/desktop_notifications.ts
+++ b/web/src/desktop_notifications.ts
@@ -107,16 +107,15 @@ export function create_notification(opts: {
key: string;
title: string;
data: NotificationData;
- desktop_notify: boolean;
on_click?: (() => void) | undefined;
}): void {
- const {notification_options, key, title, data, desktop_notify, on_click} = opts;
+ const {notification_options, key, title, data, on_click} = opts;
const existing_notification = notice_memory.get(key);
if (existing_notification) {
existing_notification.obj.close();
}
- if (!desktop_notify || NotificationAPI === undefined) {
+ if (!granted_desktop_notifications_permission()) {
return;
}
diff --git a/web/src/message_notifications.ts b/web/src/message_notifications.ts
index 09952d06310..dcbdea44b14 100644
--- a/web/src/message_notifications.ts
+++ b/web/src/message_notifications.ts
@@ -162,11 +162,7 @@ function get_notification_title(
return title_prefix + title_suffix;
}
-export function process_notification(notification: {
- message: Message | TestNotificationMessage;
- desktop_notify: boolean;
-}): void {
- const message = notification.message;
+export function process_notification(message: Message | TestNotificationMessage): void {
const content = get_notification_content(message);
const key = get_notification_key(message);
let msg_count = 1;
@@ -205,7 +201,6 @@ export function process_notification(notification: {
key,
title,
data: {type: "message", message_id: message.id, msg_count},
- desktop_notify: notification.desktop_notify,
on_click,
});
}
@@ -417,10 +412,7 @@ export function received_messages(messages: (Message | TestNotificationMessage)[
message.notification_sent = true;
if (should_send_desktop_notification(message)) {
- process_notification({
- message,
- desktop_notify: desktop_notifications.granted_desktop_notifications_permission(),
- });
+ process_notification(message);
}
if (should_send_audible_notification(message)) {
void ui_util.play_audio(util.the($("#user-notification-sound-audio")));
diff --git a/web/tests/notifications.test.cjs b/web/tests/notifications.test.cjs
index 3caebfec1da..8b033d7f1e0 100644
--- a/web/tests/notifications.test.cjs
+++ b/web/tests/notifications.test.cjs
@@ -406,6 +406,8 @@ test("basic_notifications", () => {
// Notifications API stub
class StubNotification {
+ static permission = "granted";
+
constructor(_title, {icon, body, tag}) {
this.icon = icon;
this.body = body;
@@ -501,7 +503,7 @@ test("basic_notifications", () => {
};
// Send notification.
- message_notifications.process_notification({message: stream_message_1, desktop_notify: true});
+ message_notifications.process_notification(stream_message_1);
n = desktop_notifications.get_notifications();
assert.equal(n.has("channel:1:10:whatever"), true);
assert.equal(n.size, 1);
@@ -516,7 +518,7 @@ test("basic_notifications", () => {
// Send notification.
stream_message_1.id = 1001;
- message_notifications.process_notification({message: stream_message_1, desktop_notify: true});
+ message_notifications.process_notification(stream_message_1);
n = desktop_notifications.get_notifications();
assert.equal(n.has("channel:1:10:whatever"), true);
assert.equal(n.size, 1);
@@ -524,14 +526,14 @@ test("basic_notifications", () => {
// Process same message again. Notification count shouldn't increase.
stream_message_1.id = 1002;
- message_notifications.process_notification({message: stream_message_1, desktop_notify: true});
+ message_notifications.process_notification(stream_message_1);
n = desktop_notifications.get_notifications();
assert.equal(n.has("channel:1:10:whatever"), true);
assert.equal(n.size, 1);
assert.equal(last_shown_message_id, stream_message_1.id.toString());
// Send another message. Notification count should increase.
- message_notifications.process_notification({message: stream_message_2, desktop_notify: true});
+ message_notifications.process_notification(stream_message_2);
n = desktop_notifications.get_notifications();
assert.equal(n.has("channel:2:10:lunch"), true);
assert.equal(n.has("channel:1:10:whatever"), true);
@@ -546,18 +548,21 @@ test("basic_notifications", () => {
assert.equal(n.size, 0);
assert.equal(last_closed_message_id, stream_message_2.id.toString());
- message_notifications.process_notification({message: direct_message, desktop_notify: true});
+ message_notifications.process_notification(direct_message);
n = desktop_notifications.get_notifications();
assert.equal(n.has("dm:2,3"), true);
assert.equal(n.size, 1);
desktop_notifications.close_notification(direct_message.id);
- message_notifications.process_notification({
- message: test_notification_message,
- desktop_notify: true,
- });
+ message_notifications.process_notification(test_notification_message);
n = desktop_notifications.get_notifications();
assert.equal(n.has("test:Notification Bot"), true);
assert.equal(n.size, 1);
desktop_notifications.close_notification(test_notification_message.id);
+
+ // Without the browser's permission, no notification is shown.
+ StubNotification.permission = "denied";
+ message_notifications.process_notification(stream_message_1);
+ n = desktop_notifications.get_notifications();
+ assert.equal(n.size, 0);
});