2017-10-30 19:46:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
2024-02-08 06:55:32 -05:00
|
|
|
var chatRegex = /\/chat\/channel\/(\d+)\//;
|
|
|
|
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
|
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
function showNotification(title, body, icon, badge, tag, baseUrl, url) {
|
|
|
|
var notificationOptions = {
|
|
|
|
body: body,
|
|
|
|
icon: icon,
|
|
|
|
badge: badge,
|
|
|
|
data: { url: url, baseUrl: baseUrl },
|
|
|
|
tag: tag
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if (chatRegex.test(url)) {
|
|
|
|
notificationOptions['actions'] = [{
|
|
|
|
action: "reply",
|
|
|
|
title: "Reply",
|
|
|
|
placeholder: "reply",
|
|
|
|
type: "text",
|
|
|
|
icon: inlineReplyIcon
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
return self.registration.showNotification(title, notificationOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('push', function(event) {
|
|
|
|
var payload = event.data.json();
|
|
|
|
|
|
|
|
event.waitUntil(
|
2024-01-02 22:01:42 -05:00
|
|
|
showNotification(payload.title, payload.body, payload.icon, payload.badge, payload.tag, payload.base_url, payload.url)
|
2018-05-04 18:31:48 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
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) {
|
2018-05-04 18:31:48 -04:00
|
|
|
client.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if ("postMessage" in client && "focus" in client) {
|
2018-05-04 18:31:48 -04:00
|
|
|
client.focus();
|
|
|
|
client.postMessage({ url: url });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if (!reusedClientWindow && clients.openWindow)
|
|
|
|
return clients.openWindow(baseUrl + url);
|
2018-05-04 18:31:48 -04:00
|
|
|
})
|
2022-11-11 10:30:21 -05:00
|
|
|
);
|
|
|
|
}
|
2018-05-04 18:31:48 -04:00
|
|
|
});
|
|
|
|
|
2021-02-08 10:09:52 -05:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-10-29 05:15:53 -04:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-11-22 20:02:01 -05:00
|
|
|
<% DiscoursePluginRegistry.service_workers.each do |js| %>
|
|
|
|
<%=raw "#{File.read(js)}" %>
|
|
|
|
<% end %>
|