From 85234ae8d71bf414896b8f29fc9e3d557349a405 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 15 Jun 2021 13:21:37 +0300 Subject: [PATCH] Notifications: Another fix loading global settings (#1165) --- src/util/notifications.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/util/notifications.ts b/src/util/notifications.ts index aa0dad4a6..6ed3dcfa6 100644 --- a/src/util/notifications.ts +++ b/src/util/notifications.ts @@ -119,7 +119,6 @@ async function unsubscribeFromPush(subscription: PushSubscription | null) { } } - export async function unsubscribe() { if (!checkIfPushSupported()) return; const serviceWorkerRegistration = await navigator.serviceWorker.ready; @@ -127,20 +126,22 @@ export async function unsubscribe() { await unsubscribeFromPush(subscription); } +// Indicates if notification settings are loaded from the api +let areSettingsLoaded = false; + // Load notification settings from the api async function loadNotificationSettings() { + if (areSettingsLoaded) return; const [result] = await Promise.all([ callApi('fetchNotificationSettings'), callApi('fetchNotificationExceptions'), ]); - if (!result) return; setGlobal(replaceSettings(getGlobal(), result)); + areSettingsLoaded = true; } export async function subscribe() { - await loadNotificationSettings(); - if (!checkIfPushSupported()) { // Ask for notification permissions only if service worker notifications are not supported // As pushManager.subscribe automatically triggers permission popup @@ -190,6 +191,7 @@ export async function subscribe() { } function checkIfShouldNotify(chat: ApiChat, isActive: boolean) { + if (!areSettingsLoaded) return false; const global = getGlobal(); if (selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)) || chat.isNotJoined) { return false; @@ -243,7 +245,7 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage) { }; } -export function showNewMessageNotification({ +export async function showNewMessageNotification({ chat, message, isActiveChat, @@ -251,6 +253,7 @@ export function showNewMessageNotification({ if (!checkIfNotificationsSupported()) return; if (!message.id) return; + await loadNotificationSettings(); if (!checkIfShouldNotify(chat, isActiveChat)) return; const {