improve notification click handling

- also here don't use client.url to figure out if a session is open
   as that doesn't work in FF
 - use tag to make sure we're dealing with the right type of notif
 - use findClient function
This commit is contained in:
Bruno Windels 2021-03-24 15:25:59 +01:00
parent f91abe4301
commit 0b3f2a7fa0

View file

@ -189,23 +189,24 @@ const NOTIF_TAG_MESSAGES_READ = "messages_read";
const NOTIF_TAG_NEW_MESSAGE = "new_message"; const NOTIF_TAG_NEW_MESSAGE = "new_message";
async function openClientFromNotif(event) { async function openClientFromNotif(event) {
const clientList = await self.clients.matchAll({type: "window"}); if (event.notification.tag !== NOTIF_TAG_NEW_MESSAGE) {
return;
}
const {sessionId, roomId} = event.notification.data; const {sessionId, roomId} = event.notification.data;
const sessionHash = `#/session/${sessionId}`; const sessionHash = `#/session/${sessionId}`;
const roomHash = `${sessionHash}/room/${roomId}`; const roomHash = `${sessionHash}/room/${roomId}`;
const roomURL = `/${roomHash}`; const roomURL = `/${roomHash}`;
for (let i = 0; i < clientList.length; i++) { const clientWithSession = await findClient(async client => {
const client = clientList[i]; return await sendAndWaitForReply(client, "hasSessionOpen", {sessionId});
const url = new URL(client.url, baseURL); });
if (url.hash.startsWith(sessionHash)) { if (clientWithSession) {
client.navigate(roomURL); console.log("notificationclick: client has session open, showing room there");
if ('focus' in client) { clientWithSession.navigate(roomURL);
await client.focus(); if ('focus' in clientWithSession) {
await clientWithSession.focus();
} }
return; } else if (self.client.openWindow) {
} console.log("notificationclick: no client found with session open, opening new window");
}
if (self.clients.openWindow) {
await self.clients.openWindow(roomURL); await self.clients.openWindow(roomURL);
} }
} }