From 5d877225ddf963d41c5ecf09131822eb73d55c0e Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 19 May 2024 21:13:45 +0700 Subject: [PATCH 1/6] Provide correct push worker update. --- notification/method/webpush.php | 2 +- styles/all/template/push_worker.js.twig | 2 +- styles/all/template/webpush.js | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/notification/method/webpush.php b/notification/method/webpush.php index 295b12b..26c9d78 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -455,7 +455,7 @@ protected function prepare_avatar($avatar) $path = !empty($matches[1]) ? end($matches[1]) : $avatar; - return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1); + return ['src' => preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1)]; } /** diff --git a/styles/all/template/push_worker.js.twig b/styles/all/template/push_worker.js.twig index 428c211..8d6ec3c 100644 --- a/styles/all/template/push_worker.js.twig +++ b/styles/all/template/push_worker.js.twig @@ -36,7 +36,7 @@ self.addEventListener('push', event => { const options = { body: responseBody, data: response, - icon: response.avatar, + icon: response.avatar.src, }; self.registration.showNotification(response.heading, options); }); diff --git a/styles/all/template/webpush.js b/styles/all/template/webpush.js index c326fe1..120b1ef 100644 --- a/styles/all/template/webpush.js +++ b/styles/all/template/webpush.js @@ -57,9 +57,10 @@ function PhpbbWebpush() { if ('serviceWorker' in navigator && 'PushManager' in window) { navigator.serviceWorker.register(serviceWorkerUrl) - .then(() => { + .then((registration) => { subscribeButton.addEventListener('click', subscribeButtonHandler); unsubscribeButton.addEventListener('click', unsubscribeButtonHandler); + registration.update(); updateButtonState(); }) From fbf6ec223f809ed19de1da73a6593c6b4321a398 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 26 May 2024 20:45:55 +0700 Subject: [PATCH 2/6] New approach to check webpush worker update on all pages. See https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/update --- event/listener.php | 9 +++++++ notification/method/webpush.php | 2 +- styles/all/template/update_worker.js | 27 +++++++++++++++++++ styles/all/template/webpush.js | 3 +-- .../template/event/overall_footer_after.html | 4 +++ tests/event/listener_test.php | 1 + 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 styles/all/template/update_worker.js create mode 100644 styles/prosilver/template/event/overall_footer_after.html diff --git a/event/listener.php b/event/listener.php index 203d58e..eb2e4f1 100644 --- a/event/listener.php +++ b/event/listener.php @@ -30,6 +30,7 @@ public static function getSubscribedEvents() 'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data', 'core.ucp_display_module_before' => 'load_language', 'core.acp_main_notice' => 'compatibility_notice', + 'core.page_header_after' => 'service_worker_url', ]; } @@ -90,4 +91,12 @@ public function compatibility_notice() { $this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>=')); } + + /** + * Generate service worker URL globally for update + */ + public function service_worker_url() + { + $this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller')); + } } diff --git a/notification/method/webpush.php b/notification/method/webpush.php index 26c9d78..8e74fbb 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -445,7 +445,7 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr * into https://myboard.url/path/to/avatar=123456789.gif * * @param string $avatar - * @return string Absolute path to avatar image + * @return array 'src' => Absolute path to avatar image */ protected function prepare_avatar($avatar) { diff --git a/styles/all/template/update_worker.js b/styles/all/template/update_worker.js new file mode 100644 index 0000000..3d81103 --- /dev/null +++ b/styles/all/template/update_worker.js @@ -0,0 +1,27 @@ +'use strict'; + +function webpushWorkerUpdate() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register(serviceWorkerUrl) + .then((registration) => { + registration.update(); + }) + .catch(error => { + // Service worker could not be updated + console.info(error); + }); + } +} + +function domReady(callBack) { + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', callBack); + } else { + callBack(); + } +} + +domReady(() => { + /* global serviceWorkerUrl */ + webpushWorkerUpdate(); +}); diff --git a/styles/all/template/webpush.js b/styles/all/template/webpush.js index 120b1ef..c326fe1 100644 --- a/styles/all/template/webpush.js +++ b/styles/all/template/webpush.js @@ -57,10 +57,9 @@ function PhpbbWebpush() { if ('serviceWorker' in navigator && 'PushManager' in window) { navigator.serviceWorker.register(serviceWorkerUrl) - .then((registration) => { + .then(() => { subscribeButton.addEventListener('click', subscribeButtonHandler); unsubscribeButton.addEventListener('click', unsubscribeButtonHandler); - registration.update(); updateButtonState(); }) diff --git a/styles/prosilver/template/event/overall_footer_after.html b/styles/prosilver/template/event/overall_footer_after.html new file mode 100644 index 0000000..c5d818c --- /dev/null +++ b/styles/prosilver/template/event/overall_footer_after.html @@ -0,0 +1,4 @@ + +{% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %} diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 3045ad6..b226de9 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -117,6 +117,7 @@ public function test_getSubscribedEvents() 'core.ucp_notifications_output_notification_types_modify_template_vars', 'core.ucp_display_module_before', 'core.acp_main_notice', + 'core.page_header_after', ], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents())); } From d4523cb3d4d1aeaf08b79c7965b534be888e30b7 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 26 May 2024 22:23:45 +0700 Subject: [PATCH 3/6] Avoid double-declaring domReady JS function which exists at UCP notification options page. --- styles/all/template/update_worker.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/styles/all/template/update_worker.js b/styles/all/template/update_worker.js index 3d81103..e3fef99 100644 --- a/styles/all/template/update_worker.js +++ b/styles/all/template/update_worker.js @@ -12,12 +12,14 @@ function webpushWorkerUpdate() { }); } } - -function domReady(callBack) { - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', callBack); - } else { - callBack(); +// Do not redeclare function if exist +if (typeof domReady === 'undefined') { + window.domReady = function(callBack) { + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', callBack); + } else { + callBack(); + } } } From 0beae0ae685aba5b1ab6045b5a94803b2d780a00 Mon Sep 17 00:00:00 2001 From: rxu Date: Mon, 27 May 2024 00:04:40 +0700 Subject: [PATCH 4/6] Do not redefine worker URL template variable if exists --- event/listener.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/event/listener.php b/event/listener.php index eb2e4f1..c05ad5e 100644 --- a/event/listener.php +++ b/event/listener.php @@ -97,6 +97,9 @@ public function compatibility_notice() */ public function service_worker_url() { - $this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller')); + if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL')) + { + $this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller')); + } } } From 12fbb694f5cd28fdd1e905c282dd5291e06944f5 Mon Sep 17 00:00:00 2001 From: rxu Date: Mon, 27 May 2024 00:14:44 +0700 Subject: [PATCH 5/6] Deduplicate service worker registration --- styles/all/template/update_worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/styles/all/template/update_worker.js b/styles/all/template/update_worker.js index e3fef99..11bb989 100644 --- a/styles/all/template/update_worker.js +++ b/styles/all/template/update_worker.js @@ -2,7 +2,7 @@ function webpushWorkerUpdate() { if ('serviceWorker' in navigator) { - navigator.serviceWorker.register(serviceWorkerUrl) + navigator.serviceWorker.getRegistration(serviceWorkerUrl) .then((registration) => { registration.update(); }) From c94e23361fe19aa0557cedb331d8f351af87d887 Mon Sep 17 00:00:00 2001 From: rxu Date: Mon, 27 May 2024 00:37:12 +0700 Subject: [PATCH 6/6] Use const to declare service worker JS variable --- styles/prosilver/template/event/overall_footer_after.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/styles/prosilver/template/event/overall_footer_after.html b/styles/prosilver/template/event/overall_footer_after.html index c5d818c..9249944 100644 --- a/styles/prosilver/template/event/overall_footer_after.html +++ b/styles/prosilver/template/event/overall_footer_after.html @@ -1,4 +1,4 @@ {% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %}