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.

navbar_alerts: Improve checks for Notifications API support.

Karl Stolley646f41d9e553
diff --git a/web/src/desktop_notifications.ts b/web/src/desktop_notifications.ts
index f173173c78f..0dbe79052f0 100644
--- a/web/src/desktop_notifications.ts
+++ b/web/src/desktop_notifications.ts
@@ -83,6 +83,11 @@ export function initialize(): void {
});
}
+// Utility function for testing support for the Notifications API
+export function has_notification_support(): boolean {
+ return NotificationAPI !== undefined;
+}
+
export function permission_state(): string {
if (NotificationAPI) {
return NotificationAPI.permission;
diff --git a/web/src/navbar_alerts.ts b/web/src/navbar_alerts.ts
index f3f2c65f76b..285af21ceac 100644
--- a/web/src/navbar_alerts.ts
+++ b/web/src/navbar_alerts.ts
@@ -28,7 +28,6 @@ import * as unread from "./unread.ts";
import * as unread_ops from "./unread_ops.ts";
import {user_settings} from "./user_settings.ts";
import * as user_topics from "./user_topics.ts";
-import * as util from "./util.ts";
function open_navbar_banner_and_resize(banner: AlertBanner): void {
banners.open(banner, $("#navbar_alerts_wrapper"));
@@ -55,10 +54,8 @@ export function should_show_desktop_notifications_banner(ls: LocalStorage): bool
// Spectators cannot receive desktop notifications, so never
// request permissions to send them.
!page_params.is_spectator &&
- // notifications *basically* don't work on any mobile platforms, so don't
- // event show the banners. This prevents trying to access things that
- // don't exist like `Notification.permission`.
- !util.is_mobile() &&
+ // Only show banners on UAs that do not include the Notification API.
+ desktop_notifications.has_notification_support() &&
// if permission has not been granted yet.
!desktop_notifications.granted_desktop_notifications_permission() &&
// if permission is allowed to be requested (e.g. not in "denied" state).
diff --git a/web/tests/navbar_alerts.test.cjs b/web/tests/navbar_alerts.test.cjs
index 7598e958256..66ebf2ad596 100644
--- a/web/tests/navbar_alerts.test.cjs
+++ b/web/tests/navbar_alerts.test.cjs
@@ -11,7 +11,6 @@ const {page_params} = require("./lib/zpage_params.cjs");
const desktop_notifications = mock_esm("../src/desktop_notifications");
const unread = mock_esm("../src/unread");
-const util = mock_esm("../src/util");
const {localstorage} = zrequire("localstorage");
const navbar_alerts = zrequire("navbar_alerts");
@@ -40,7 +39,7 @@ test("should_show_desktop_notifications_banner", ({override}) => {
// - The user has not said to never show banner on this device again.
ls.set("dontAskForNotifications", undefined);
page_params.is_spectator = false;
- override(util, "is_mobile", () => false);
+ override(desktop_notifications, "has_notification_support", () => true);
override(desktop_notifications, "granted_desktop_notifications_permission", () => false);
override(desktop_notifications, "permission_state", () => "default");
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), true);
@@ -50,10 +49,10 @@ test("should_show_desktop_notifications_banner", ({override}) => {
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
ls.set("dontAskForNotifications", undefined);
- // Don't ask for permission if device is mobile.
- override(util, "is_mobile", () => true);
+ // Don't ask for permission if UA has no Notification API support.
+ override(desktop_notifications, "has_notification_support", () => false);
assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);
- override(util, "is_mobile", () => false);
+ override(desktop_notifications, "has_notification_support", () => true);
// Don't ask for permission if notification is denied by user.
override(desktop_notifications, "permission_state", () => "denied");