Group push notifications per target

This commit is contained in:
Pavel Djundik 2017-09-03 18:57:07 +03:00
parent c7433eca95
commit 6cfe60e4d9
4 changed files with 41 additions and 18 deletions

View File

@ -9,16 +9,30 @@ self.addEventListener("push", function(event) {
const payload = event.data.json(); const payload = event.data.json();
if (payload.type === "notification") { if (payload.type !== "notification") {
return;
}
// get current notification, close it, and draw new
event.waitUntil( event.waitUntil(
self.registration.showNotification(payload.title, { 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", badge: "img/logo-64.png",
icon: "img/touch-icon-192x192.png", icon: "img/touch-icon-192x192.png",
body: payload.body, body: payload.body,
timestamp: payload.timestamp, timestamp: payload.timestamp,
});
}) })
); );
}
}); });
self.addEventListener("notificationclick", function(event) { self.addEventListener("notificationclick", function(event) {

View File

@ -430,7 +430,7 @@ Client.prototype.open = function(socketId, target) {
target.chan.firstUnread = 0; target.chan.firstUnread = 0;
target.chan.unread = 0; target.chan.unread = 0;
target.chan.highlight = false; target.chan.highlight = 0;
this.attachedClients[socketId].openChannel = target.chan.id; this.attachedClients[socketId].openChannel = target.chan.id;
this.lastActiveChannel = target.chan.id; this.lastActiveChannel = target.chan.id;

View File

@ -26,7 +26,7 @@ function Chan(attr) {
type: Chan.Type.CHANNEL, type: Chan.Type.CHANNEL,
firstUnread: 0, firstUnread: 0,
unread: 0, unread: 0,
highlight: false, highlight: 0,
users: [] users: []
}); });
} }
@ -78,7 +78,7 @@ Chan.prototype.pushMessage = function(client, msg, increasesUnread) {
} }
if (msg.highlight) { if (msg.highlight) {
this.highlight = true; this.highlight++;
} }
} }
}; };

View File

@ -106,20 +106,29 @@ module.exports = function(irc, network) {
// Do not send notifications for messages older than 15 minutes (znc buffer for example) // Do not send notifications for messages older than 15 minutes (znc buffer for example)
if (highlight && (!data.time || data.time > Date.now() - 900000)) { 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) { if (chan.type !== Chan.Type.QUERY) {
title += ` (${chan.name}) mentioned you`; body = `${data.nick}: ${body}`;
} else { }
title += " sent you a message";
// 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, { client.manager.webPush.push(client, {
type: "notification", type: "notification",
chanId: chan.id, chanId: chan.id,
timestamp: data.time || Date.now(), timestamp: data.time || Date.now(),
title: `The Lounge: ${title}`, title: title,
body: Helper.cleanIrcMessage(data.message) body: body
}, true); }, true);
} }
} }