Skip to content

Commit 7ab03cf

Browse files
committed
Add new worker update
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 5d5f034 commit 7ab03cf

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ services:
4040
phpbb.wpn.ucp.controller.webpush:
4141
class: phpbb\webpushnotifications\ucp\controller\webpush
4242
arguments:
43+
- '@config'
4344
- '@controller.helper'
4445
- '@dbal.conn'
4546
- '@phpbb.wpn.form_helper'

notification/method/webpush.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ protected function notify_using_webpush(): void
221221
$data = [
222222
'item_id' => $notification->item_id,
223223
'type_id' => $notification->notification_type_id,
224+
'version' => $this->config['assets_version'],
224225
];
225226
$json_data = json_encode($data);
226227

styles/all/template/push_worker.js.twig

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Event listener for install event
3+
*/
4+
self.addEventListener('install', () => {
5+
// Call to ensure service worker is correctly updated
6+
self.skipWaiting();
7+
});
8+
9+
/* global clients */
10+
self.addEventListener('activate', event => {
11+
event.waitUntil(clients.claim());
12+
});
13+
114
/**
215
* Event listener for push event
316
*/
@@ -7,17 +20,25 @@ self.addEventListener('push', event => {
720
}
821

922
let itemId = 0;
10-
let typeId = 0;
23+
let typeId = 0;
24+
let notificationVersion = 5;
1125
try {
1226
const notificationData = event.data.json();
1327
itemId = notificationData.item_id;
1428
typeId = notificationData.type_id;
29+
notificationVersion = parseInt(notificationData.version, 10);
1530
} catch {
1631
self.registration.showNotification(event.data.text());
1732
return;
1833
}
1934

2035
const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
36+
const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);
37+
38+
// Force update if versions differ
39+
if (assetsVersion !== notificationVersion) {
40+
self.registration.update();
41+
}
2142

2243
const formData = new FormData();
2344
formData.append('item_id', itemId.toString(10));

ucp/controller/webpush.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace phpbb\webpushnotifications\ucp\controller;
1212

13+
use phpbb\config\config;
1314
use phpbb\controller\helper as controller_helper;
1415
use phpbb\db\driver\driver_interface;
1516
use phpbb\exception\http_exception;
@@ -31,6 +32,9 @@ class webpush
3132
/** @var string UCP form token name */
3233
public const FORM_TOKEN_UCP = 'ucp_webpush';
3334

35+
/** @var config */
36+
protected $config;
37+
3438
/** @var controller_helper */
3539
protected $controller_helper;
3640

@@ -61,6 +65,7 @@ class webpush
6165
/**
6266
* Constructor for webpush controller
6367
*
68+
* @param config $config
6469
* @param controller_helper $controller_helper
6570
* @param driver_interface $db
6671
* @param form_helper $form_helper
@@ -71,9 +76,10 @@ class webpush
7176
* @param string $notification_webpush_table
7277
* @param string $push_subscriptions_table
7378
*/
74-
public function __construct(controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
79+
public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
7580
request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table)
7681
{
82+
$this->config = $config;
7783
$this->controller_helper = $controller_helper;
7884
$this->db = $db;
7985
$this->form_helper = $form_helper;
@@ -129,6 +135,7 @@ public function worker(): Response
129135
// @todo: only work for logged in users, no anonymous & bot
130136
$content = $this->template->render('@phpbb_webpushnotifications/push_worker.js.twig', [
131137
'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_get_notification_controller'),
138+
'ASSETS_VERSION' => $this->config['assets_version'],
132139
]);
133140

134141
$response = new Response($content);

0 commit comments

Comments
 (0)