Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
phpbb.wpn.ucp.controller.webpush:
class: phpbb\webpushnotifications\ucp\controller\webpush
arguments:
- '@config'
- '@controller.helper'
- '@dbal.conn'
- '@phpbb.wpn.form_helper'
Expand Down
1 change: 1 addition & 0 deletions notification/method/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ protected function notify_using_webpush(): void
$data = [
'item_id' => $notification->item_id,
'type_id' => $notification->notification_type_id,
'version' => $this->config['assets_version'],
];
$json_data = json_encode($data);

Expand Down
25 changes: 24 additions & 1 deletion styles/all/template/push_worker.js.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Event listener for install event
*/
self.addEventListener('install', () => {
// Call to ensure service worker is correctly updated
self.skipWaiting();
});

/**
* Event listener for activate event
*/
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});

/**
* Event listener for push event
*/
Expand All @@ -7,17 +22,25 @@ self.addEventListener('push', event => {
}

let itemId = 0;
let typeId = 0;
let typeId = 0;
let notificationVersion = 5;
try {
const notificationData = event.data.json();
itemId = notificationData.item_id;
typeId = notificationData.type_id;
notificationVersion = parseInt(notificationData.version, 10);
} catch {
self.registration.showNotification(event.data.text());
return;
}

const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);

// Force update if versions differ
if (assetsVersion !== notificationVersion) {
self.registration.update();
}

const formData = new FormData();
formData.append('item_id', itemId.toString(10));
Expand Down
1 change: 0 additions & 1 deletion styles/all/template/ucp_notifications_webpush.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
</script>

{% INCLUDEJS '@phpbb_webpushnotifications/webpush.js' %}
{% INCLUDEJS '@phpbb_webpushnotifications/update_worker.js' %}
{% INCLUDECSS '@phpbb_webpushnotifications/phpbb_wpn.css' %}
29 changes: 0 additions & 29 deletions styles/all/template/update_worker.js

This file was deleted.

9 changes: 8 additions & 1 deletion ucp/controller/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace phpbb\webpushnotifications\ucp\controller;

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

/** @var config */
protected $config;

/** @var controller_helper */
protected $controller_helper;

Expand Down Expand Up @@ -61,6 +65,7 @@ class webpush
/**
* Constructor for webpush controller
*
* @param config $config
* @param controller_helper $controller_helper
* @param driver_interface $db
* @param form_helper $form_helper
Expand All @@ -71,9 +76,10 @@ class webpush
* @param string $notification_webpush_table
* @param string $push_subscriptions_table
*/
public function __construct(controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table)
{
$this->config = $config;
$this->controller_helper = $controller_helper;
$this->db = $db;
$this->form_helper = $form_helper;
Expand Down Expand Up @@ -129,6 +135,7 @@ public function worker(): Response
// @todo: only work for logged in users, no anonymous & bot
$content = $this->template->render('@phpbb_webpushnotifications/push_worker.js.twig', [
'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_get_notification_controller'),
'ASSETS_VERSION' => $this->config['assets_version'],
]);

$response = new Response($content);
Expand Down