From ad715dac085abbfdd5fb468561959e9a5e78411b Mon Sep 17 00:00:00 2001 From: rxu Date: Mon, 27 May 2024 11:30:15 +0700 Subject: [PATCH] [ticket/17321] Provide correct push worker update PHPBB3-17321 --- phpBB/assets/javascript/update_worker.js | 29 +++++++++++++++++++ phpBB/includes/functions.php | 17 ++++++++++- .../prosilver/template/overall_footer.html | 7 +++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 phpBB/assets/javascript/update_worker.js diff --git a/phpBB/assets/javascript/update_worker.js b/phpBB/assets/javascript/update_worker.js new file mode 100644 index 0000000000..11bb989655 --- /dev/null +++ b/phpBB/assets/javascript/update_worker.js @@ -0,0 +1,29 @@ +'use strict'; + +function webpushWorkerUpdate() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.getRegistration(serviceWorkerUrl) + .then((registration) => { + registration.update(); + }) + .catch(error => { + // Service worker could not be updated + console.info(error); + }); + } +} +// Do not redeclare function if exist +if (typeof domReady === 'undefined') { + window.domReady = function(callBack) { + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', callBack); + } else { + callBack(); + } + } +} + +domReady(() => { + /* global serviceWorkerUrl */ + webpushWorkerUpdate(); +}); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index bca9a97c48..02ae20b339 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3855,7 +3855,8 @@ function page_header($page_title = '', $display_online_list = false, $item_id = // Output the notifications $notifications = false; - if ($config['load_notifications'] && $config['allow_board_notifications'] && $user->data['user_id'] != ANONYMOUS && $user->data['user_type'] != USER_IGNORE) + $notifications_enabled = $config['load_notifications'] && $config['allow_board_notifications'] && $user->data['user_id'] != ANONYMOUS && $user->data['user_type'] != USER_IGNORE; + if ($notifications_enabled) { /* @var $phpbb_notifications \phpbb\notification\manager */ $phpbb_notifications = $phpbb_container->get('notification_manager'); @@ -4032,6 +4033,20 @@ function page_header($page_title = '', $display_online_list = false, $item_id = 'SITE_LOGO_IMG' => $user->img('site_logo'), )); + $webpush_available = $notifications_enabled && $phpbb_notifications->get_method_class('notification.method.webpush')->is_available(); + if ($webpush_available) + { + if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL')) + { + $this->template->assign_var('U_WEBPUSH_WORKER_URL', $controller_helper->route('phpbb_ucp_push_worker_controller')); + } + + if (!$this->template->retrieve_var('NOTIFICATIONS_WEBPUSH_ENABLE')) + { + $this->template->assign_var('NOTIFICATIONS_WEBPUSH_ENABLE', true); + } + } + $http_headers = array(); if ($send_headers) diff --git a/phpBB/styles/prosilver/template/overall_footer.html b/phpBB/styles/prosilver/template/overall_footer.html index 237620bd61..baa707e6a9 100644 --- a/phpBB/styles/prosilver/template/overall_footer.html +++ b/phpBB/styles/prosilver/template/overall_footer.html @@ -117,6 +117,13 @@ +{% if NOTIFICATIONS_WEBPUSH_ENABLE %} + + {% INCLUDEJS(T_ASSETS_PATH ~ '/javascript/update_worker.js') %} +{% endif %} +