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: Extract a reusable create_notification.

Pratik Chanda37cea8e652f5
diff --git a/web/src/desktop_notifications.ts b/web/src/desktop_notifications.ts
index 76e923554f3..b8e57f3ca62 100644
--- a/web/src/desktop_notifications.ts
+++ b/web/src/desktop_notifications.ts
@@ -69,6 +69,54 @@ if (electron_bridge?.new_notification) {
NotificationAPI = window.Notification;
}
+export function create_notification(opts: {
+ notification_options: NotificationOptions;
+ key: string;
+ title: string;
+ message_id: number;
+ msg_count: number;
+ desktop_notify: boolean;
+ on_click?: (() => void) | undefined;
+}): void {
+ const {notification_options, key, title, message_id, msg_count, desktop_notify, on_click} =
+ opts;
+ const existing_notification = notice_memory.get(key);
+ if (existing_notification) {
+ existing_notification.obj.close();
+ }
+
+ if (!desktop_notify || NotificationAPI === undefined) {
+ return;
+ }
+
+ const notification_object = new NotificationAPI(title, notification_options);
+ notice_memory.set(key, {
+ obj: notification_object,
+ msg_count,
+ message_id,
+ });
+
+ if (typeof notification_object.addEventListener === "function") {
+ // Sadly, some third-party Electron apps like Franz/Ferdi
+ // misimplement the Notification API not inheriting from
+ // EventTarget. This results in addEventListener being
+ // unavailable for them.
+ notification_object.addEventListener("click", () => {
+ notification_object.close();
+ on_click?.();
+ window.focus();
+ });
+ notification_object.addEventListener("close", () => {
+ const current_notice_memory = notice_memory.get(key);
+ // This check helps avoid race between close event for current notification
+ // object and the previous notification_object close handler.
+ if (current_notice_memory?.obj === notification_object) {
+ notice_memory.delete(key);
+ }
+ });
+ }
+}
+
export function get_notifications(): NoticeMemory {
return notice_memory;
}
diff --git a/web/src/message_notifications.ts b/web/src/message_notifications.ts
index 4a473054f08..808a93acd10 100644
--- a/web/src/message_notifications.ts
+++ b/web/src/message_notifications.ts
@@ -3,7 +3,6 @@ import {$} from "jquery";
import * as alert_words from "./alert_words.ts";
import * as blueslip from "./blueslip.ts";
import * as desktop_notifications from "./desktop_notifications.ts";
-import type {ElectronBridgeNotification} from "./desktop_notifications.ts";
import {$t} from "./i18n.ts";
import * as message_parser from "./message_parser.ts";
import type {Message} from "./message_store.ts";
@@ -170,7 +169,6 @@ export function process_notification(notification: {
const message = notification.message;
const content = get_notification_content(message);
const key = get_notification_key(message);
- let notification_object: ElectronBridgeNotification | Notification;
let msg_count = 1;
debug_notification_source_value(message);
@@ -178,53 +176,39 @@ export function process_notification(notification: {
const notice_memory = desktop_notifications.notice_memory.get(key);
if (notice_memory) {
msg_count = notice_memory.msg_count + 1;
- notification_object = notice_memory.obj;
- notification_object.close();
}
const title = get_notification_title(message, msg_count);
- if (notification.desktop_notify && desktop_notifications.NotificationAPI !== undefined) {
- const icon_url =
- message.type === "test-notification"
- ? small_avatar_url_for_test_notification(message)
- : people.small_avatar_url(message);
- notification_object = new desktop_notifications.NotificationAPI(title, {
- icon: icon_url,
- body: content,
- tag: message.id.toString(),
- });
- desktop_notifications.notice_memory.set(key, {
- obj: notification_object,
- msg_count,
- message_id: message.id,
- });
-
- if (typeof notification_object.addEventListener === "function") {
- // Sadly, some third-party Electron apps like Franz/Ferdi
- // misimplement the Notification API not inheriting from
- // EventTarget. This results in addEventListener being
- // unavailable for them.
- notification_object.addEventListener("click", () => {
- notification_object.close();
- if (message.type !== "test-notification") {
- // Narrowing to message's near view helps to handle the case
- // where a user clicked the notification, but before narrowing
- // the message deletion got processed.
- message_view.narrow_to_message_near(message, "notification");
- }
- window.focus();
- });
- notification_object.addEventListener("close", () => {
- const current_notice_memory = desktop_notifications.notice_memory.get(key);
- // This check helps avoid race between close event for current notification
- // object and the previous notification_object close handler.
- if (current_notice_memory?.obj === notification_object) {
- desktop_notifications.notice_memory.delete(key);
- }
- });
+ const icon_url =
+ message.type === "test-notification"
+ ? small_avatar_url_for_test_notification(message)
+ : people.small_avatar_url(message);
+
+ const notification_options = {
+ icon: icon_url,
+ body: content,
+ tag: message.id.toString(),
+ };
+
+ function on_click(): void {
+ if (message.type !== "test-notification") {
+ // Narrowing to message's near view helps to handle the case
+ // where a user clicked the notification, but before narrowing
+ // the message deletion got processed.
+ message_view.narrow_to_message_near(message, "notification");
}
}
+
+ desktop_notifications.create_notification({
+ notification_options,
+ key,
+ title,
+ message_id: message.id,
+ msg_count,
+ desktop_notify: notification.desktop_notify,
+ on_click,
+ });
}
export function message_is_notifiable(message: Message | TestNotificationMessage): boolean {