A platform for community discussion. Free, open, simple.
FEATURE: Replyable chat push notifications (#18973)
Rafael dos Santos Silva99e5fbe30361
diff --git a/app/assets/javascripts/service-worker.js.erb b/app/assets/javascripts/service-worker.js.erbindex 8580e6e6fce..e54831fef04 100644--- a/app/assets/javascripts/service-worker.js.erb+++ b/app/assets/javascripts/service-worker.js.erb@@ -8,6 +8,8 @@ workbox.setConfig({});var authUrls = ["auth", "session/sso_login", "session/sso"].map(path => `<%= Discourse.base_path %>/${path}`);+var chatRegex = /\/chat\/channel\/(\d+)\//;+var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";var cacheVersion = "1";var discourseCacheName = "discourse-" + cacheVersion;@@ -134,6 +136,16 @@ function showNotification(title, body, icon, badge, tag, baseUrl, url) {tag: tag}+ if (chatRegex.test(url)) {+ notificationOptions['actions'] = [{+ action: "reply",+ title: "Reply",+ placeholder: "reply",+ type: "text",+ icon: inlineReplyIcon+ }];+ }+return self.registration.showNotification(title, notificationOptions);}@@ -163,18 +175,51 @@ self.addEventListener('notificationclick', function(event) {var url = event.notification.data.url;var baseUrl = event.notification.data.baseUrl;- // 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) {+ 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) {+ if ("postMessage" in client && "focus" in client) {client.focus();client.postMessage({ url: url });return true;@@ -182,9 +227,11 @@ self.addEventListener('notificationclick', function(event) {return false;});- if (!reusedClientWindow && clients.openWindow) return clients.openWindow(baseUrl + url);+ if (!reusedClientWindow && clients.openWindow)+ return clients.openWindow(baseUrl + url);})- );+ );+ }});self.addEventListener('message', function(event) {diff --git a/public/images/push-notifications/inline_reply.png b/public/images/push-notifications/inline_reply.pngnew file mode 100644index 00000000000..bafc74baa3bBinary files /dev/null and b/public/images/push-notifications/inline_reply.png differ