Notifications: Another fix loading global settings (#1165)
This commit is contained in:
parent
558d453f21
commit
85234ae8d7
@ -119,7 +119,6 @@ async function unsubscribeFromPush(subscription: PushSubscription | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function unsubscribe() {
|
export async function unsubscribe() {
|
||||||
if (!checkIfPushSupported()) return;
|
if (!checkIfPushSupported()) return;
|
||||||
const serviceWorkerRegistration = await navigator.serviceWorker.ready;
|
const serviceWorkerRegistration = await navigator.serviceWorker.ready;
|
||||||
@ -127,20 +126,22 @@ export async function unsubscribe() {
|
|||||||
await unsubscribeFromPush(subscription);
|
await unsubscribeFromPush(subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indicates if notification settings are loaded from the api
|
||||||
|
let areSettingsLoaded = false;
|
||||||
|
|
||||||
// Load notification settings from the api
|
// Load notification settings from the api
|
||||||
async function loadNotificationSettings() {
|
async function loadNotificationSettings() {
|
||||||
|
if (areSettingsLoaded) return;
|
||||||
const [result] = await Promise.all([
|
const [result] = await Promise.all([
|
||||||
callApi('fetchNotificationSettings'),
|
callApi('fetchNotificationSettings'),
|
||||||
callApi('fetchNotificationExceptions'),
|
callApi('fetchNotificationExceptions'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
setGlobal(replaceSettings(getGlobal(), result));
|
setGlobal(replaceSettings(getGlobal(), result));
|
||||||
|
areSettingsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function subscribe() {
|
export async function subscribe() {
|
||||||
await loadNotificationSettings();
|
|
||||||
|
|
||||||
if (!checkIfPushSupported()) {
|
if (!checkIfPushSupported()) {
|
||||||
// Ask for notification permissions only if service worker notifications are not supported
|
// Ask for notification permissions only if service worker notifications are not supported
|
||||||
// As pushManager.subscribe automatically triggers permission popup
|
// As pushManager.subscribe automatically triggers permission popup
|
||||||
@ -190,6 +191,7 @@ export async function subscribe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkIfShouldNotify(chat: ApiChat, isActive: boolean) {
|
function checkIfShouldNotify(chat: ApiChat, isActive: boolean) {
|
||||||
|
if (!areSettingsLoaded) return false;
|
||||||
const global = getGlobal();
|
const global = getGlobal();
|
||||||
if (selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)) || chat.isNotJoined) {
|
if (selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)) || chat.isNotJoined) {
|
||||||
return false;
|
return false;
|
||||||
@ -243,7 +245,7 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showNewMessageNotification({
|
export async function showNewMessageNotification({
|
||||||
chat,
|
chat,
|
||||||
message,
|
message,
|
||||||
isActiveChat,
|
isActiveChat,
|
||||||
@ -251,6 +253,7 @@ export function showNewMessageNotification({
|
|||||||
if (!checkIfNotificationsSupported()) return;
|
if (!checkIfNotificationsSupported()) return;
|
||||||
if (!message.id) return;
|
if (!message.id) return;
|
||||||
|
|
||||||
|
await loadNotificationSettings();
|
||||||
if (!checkIfShouldNotify(chat, isActiveChat)) return;
|
if (!checkIfShouldNotify(chat, isActiveChat)) return;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user