From 6cfe60e4d996c587b81d296287458831643979e4 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 3 Sep 2017 18:57:07 +0300 Subject: [PATCH] Group push notifications per target --- client/service-worker.js | 32 ++++++++++++++++++++++--------- src/client.js | 2 +- src/models/chan.js | 4 ++-- src/plugins/irc-events/message.js | 21 ++++++++++++++------ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/client/service-worker.js b/client/service-worker.js index fcb63300..641e050b 100644 --- a/client/service-worker.js +++ b/client/service-worker.js @@ -9,16 +9,30 @@ self.addEventListener("push", function(event) { const payload = event.data.json(); - if (payload.type === "notification") { - event.waitUntil( - self.registration.showNotification(payload.title, { - badge: "img/logo-64.png", - icon: "img/touch-icon-192x192.png", - body: payload.body, - timestamp: payload.timestamp, - }) - ); + if (payload.type !== "notification") { + return; } + + // get current notification, close it, and draw new + event.waitUntil( + self.registration + .getNotifications({ + tag: `chan-${payload.chanId}` + }) + .then((notifications) => { + for (const notification of notifications) { + notification.close(); + } + + return self.registration.showNotification(payload.title, { + tag: `chan-${payload.chanId}`, + badge: "img/logo-64.png", + icon: "img/touch-icon-192x192.png", + body: payload.body, + timestamp: payload.timestamp, + }); + }) + ); }); self.addEventListener("notificationclick", function(event) { diff --git a/src/client.js b/src/client.js index 2d436a4d..d4cd3ccc 100644 --- a/src/client.js +++ b/src/client.js @@ -430,7 +430,7 @@ Client.prototype.open = function(socketId, target) { target.chan.firstUnread = 0; target.chan.unread = 0; - target.chan.highlight = false; + target.chan.highlight = 0; this.attachedClients[socketId].openChannel = target.chan.id; this.lastActiveChannel = target.chan.id; diff --git a/src/models/chan.js b/src/models/chan.js index 3a9ddcec..c7889178 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -26,7 +26,7 @@ function Chan(attr) { type: Chan.Type.CHANNEL, firstUnread: 0, unread: 0, - highlight: false, + highlight: 0, users: [] }); } @@ -78,7 +78,7 @@ Chan.prototype.pushMessage = function(client, msg, increasesUnread) { } if (msg.highlight) { - this.highlight = true; + this.highlight++; } } }; diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index fbfc7102..24251180 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -106,20 +106,29 @@ module.exports = function(irc, network) { // Do not send notifications for messages older than 15 minutes (znc buffer for example) if (highlight && (!data.time || data.time > Date.now() - 900000)) { - let title = data.nick; + let title = chan.name; + let body = Helper.cleanIrcMessage(data.message); + // In channels, prepend sender nickname to the message if (chan.type !== Chan.Type.QUERY) { - title += ` (${chan.name}) mentioned you`; - } else { - title += " sent you a message"; + body = `${data.nick}: ${body}`; + } + + // If a channel is active on any client, highlight won't increment and notification will say (0 mention) + if (chan.highlight > 0) { + title += ` (${chan.highlight} ${chan.type === Chan.Type.QUERY ? "new message" : "mention"}${chan.highlight > 1 ? "s" : ""})`; + } + + if (chan.highlight > 1) { + body += `\n\n… and ${chan.highlight - 1} other message${chan.highlight > 2 ? "s" : ""}`; } client.manager.webPush.push(client, { type: "notification", chanId: chan.id, timestamp: data.time || Date.now(), - title: `The Lounge: ${title}`, - body: Helper.cleanIrcMessage(data.message) + title: title, + body: body }, true); } }