discourse/app/assets/javascripts/service-worker.js.erb
David Battersby 7bcd46b87d
DEV: use service worker for chat sound (#29388)
This change makes use of service workers to determine if we should play chat sounds in the current browser tab. Since users can have multiple tabs open, we currently attempt to play sound across all active tabs.

With this change we iterate over all clients and check if client.focused is true (ie. the current tab/window we have open), if so we allow playing the audio in the current tab and for all other hidden tabs/windows we return false.

---------

Co-authored-by: Bianca Nenciu <nbianca@users.noreply.github.com>
2024-10-29 13:15:53 +04:00

147 lines
4.2 KiB
Plaintext

'use strict';
var chatRegex = /\/chat\/channel\/(\d+)\//;
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
function showNotification(title, body, icon, badge, tag, baseUrl, url) {
var notificationOptions = {
body: body,
icon: icon,
badge: badge,
data: { url: url, baseUrl: baseUrl },
tag: tag
}
if (chatRegex.test(url)) {
notificationOptions['actions'] = [{
action: "reply",
title: "Reply",
placeholder: "reply",
type: "text",
icon: inlineReplyIcon
}];
}
return self.registration.showNotification(title, notificationOptions);
}
self.addEventListener('push', function(event) {
var payload = event.data.json();
event.waitUntil(
showNotification(payload.title, payload.body, payload.icon, payload.badge, payload.tag, payload.base_url, payload.url)
);
});
self.addEventListener('notificationclick', function(event) {
// Android doesn't close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
var url = event.notification.data.url;
var baseUrl = event.notification.data.baseUrl;
if (event.action === "reply") {
let csrf;
fetch("/session/csrf", {
credentials: "include",
headers: {
Accept: "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not OK");
}
return response.json();
})
.then((data) => {
csrf = data.csrf;
let chatTest = url.match(chatRegex);
if (chatTest.length > 0) {
let chatChannel = chatTest[1];
fetch(`${baseUrl}/chat/${chatChannel}.json`, {
credentials: "include",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-CSRF-Token": csrf,
},
body: `message=${event.reply}`,
method: "POST",
mode: "cors",
});
}
});
} else {
// This looks to see if the current window is already open and
// focuses if it is
event.waitUntil(
clients.matchAll({ type: "window" }).then(function (clientList) {
var reusedClientWindow = clientList.some(function (client) {
if (client.url === baseUrl + url && "focus" in client) {
client.focus();
return true;
}
if ("postMessage" in client && "focus" in client) {
client.focus();
client.postMessage({ url: url });
return true;
}
return false;
});
if (!reusedClientWindow && clients.openWindow)
return clients.openWindow(baseUrl + url);
})
);
}
});
self.addEventListener('pushsubscriptionchange', function(event) {
event.waitUntil(
Promise.all(
fetch('<%= Discourse.base_url %>/push_notifications/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: new URLSearchParams({
"subscription[endpoint]": event.newSubscription.endpoint,
"subscription[keys][auth]": event.newSubscription.toJSON().keys.auth,
"subscription[keys][p256dh]": event.newSubscription.toJSON().keys.p256dh,
"send_confirmation": false
})
}),
fetch('<%= Discourse.base_url %>/push_notifications/unsubscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: new URLSearchParams({
"subscription[endpoint]": event.oldSubscription.endpoint,
"subscription[keys][auth]": event.oldSubscription.toJSON().keys.auth,
"subscription[keys][p256dh]": event.oldSubscription.toJSON().keys.p256dh
})
})
)
);
});
self.addEventListener('message', function(event) {
if (event.data?.action !== "primaryTab") {
return;
}
event.waitUntil(
self.clients.matchAll().then(function(clients) {
clients.forEach(function(client) {
client.postMessage({
primaryTab: client.focused
});
});
})
);
});
<% DiscoursePluginRegistry.service_workers.each do |js| %>
<%=raw "#{File.read(js)}" %>
<% end %>