From a69ab0156e130621f89afdf8be0b7d2c969ebb95 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 14 Jul 2024 11:51:47 -0700 Subject: [PATCH 01/11] Use absolute URLs for pwa icons Signed-off-by: Matt Friedman --- controller/manifest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/controller/manifest.php b/controller/manifest.php index e2541da..7fd7b46 100644 --- a/controller/manifest.php +++ b/controller/manifest.php @@ -61,6 +61,7 @@ public function handle(): JsonResponse } $board_path = $this->config['force_server_vars'] ? $this->config['script_path'] : $this->path_helper->get_web_root_path(); + $board_url = generate_board_url(); $manifest = [ 'name' => $this->config['sitename'], @@ -76,12 +77,12 @@ public function handle(): JsonResponse { $manifest['icons'] = [ [ - 'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'], + 'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'], 'sizes' => '192x192', 'type' => 'image/png' ], [ - 'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'], + 'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'], 'sizes' => '512x512', 'type' => 'image/png' ] From c55aef61c09bffc8118a9ce00e94b3153cb1cab3 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Wed, 17 Jul 2024 08:32:36 -0700 Subject: [PATCH 02/11] Fix loading notifications for users in better way Signed-off-by: Matt Friedman fix Signed-off-by: Matt Friedman --- config/services.yml | 1 + event/listener.php | 43 +++++++++++++++++++++++++++------ notification/method/webpush.php | 15 +----------- tests/event/listener_test.php | 7 +++++- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/config/services.yml b/config/services.yml index 306df11..4ec48f3 100644 --- a/config/services.yml +++ b/config/services.yml @@ -18,6 +18,7 @@ services: - '@phpbb.wpn.form_helper' - '@language' - '@template' + - '@user' - '@notification_manager' - '%core.root_path%' tags: diff --git a/event/listener.php b/event/listener.php index 07ad1ff..20b04bf 100644 --- a/event/listener.php +++ b/event/listener.php @@ -20,6 +20,7 @@ use phpbb\language\language; use phpbb\notification\manager; use phpbb\template\template; +use phpbb\user; use phpbb\webpushnotifications\form\form_helper; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -46,6 +47,9 @@ class listener implements EventSubscriberInterface /* @var template */ protected $template; + /** @var user */ + protected $user; + /* @var manager */ protected $phpbb_notifications; @@ -61,10 +65,11 @@ class listener implements EventSubscriberInterface * @param form_helper $form_helper Form helper object * @param language $language Language object * @param template $template Template object + * @param user $user * @param manager $phpbb_notifications Notifications manager object * @param $root_path */ - public function __construct(config $config, controller_helper $controller_helper, FastImageSize $imagesize, form_helper $form_helper, language $language, template $template, manager $phpbb_notifications, $root_path) + public function __construct(config $config, controller_helper $controller_helper, FastImageSize $imagesize, form_helper $form_helper, language $language, template $template, user $user, manager $phpbb_notifications, $root_path) { $this->config = $config; $this->controller_helper = $controller_helper; @@ -72,6 +77,7 @@ public function __construct(config $config, controller_helper $controller_helper $this->form_helper = $form_helper; $this->language = $language; $this->template = $template; + $this->user = $user; $this->phpbb_notifications = $phpbb_notifications; $this->root_path = $root_path; } @@ -92,19 +98,26 @@ public static function getSubscribedEvents() */ public function load_template_data() { + if (!$this->can_use_notifications()) + { + return; + } + $methods = $this->phpbb_notifications->get_subscription_methods(); $webpush_method = $methods['notification.method.phpbb.wpn.webpush'] ?? null; - if ($webpush_method !== null) + if ($webpush_method === null) { - if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH')) - { - $this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications'); - } + return; + } - $template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper); - $this->template->assign_vars($template_ary); + if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH')) + { + $this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications'); } + + $template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper); + $this->template->assign_vars($template_ary); } /** @@ -217,4 +230,18 @@ protected function add_error($event, $error_key, $param = null) $error[] = $this->language->lang($error_key, $param); $event['error'] = $error; } + + /** + * Can notifications be used by the user? + * + * @return bool + */ + protected function can_use_notifications() + { + return $this->config['load_notifications'] + && $this->config['allow_board_notifications'] + && $this->config['wpn_webpush_enable'] + && $this->user->id() !== ANONYMOUS + && (int) $this->user->data['user_type'] !== USER_IGNORE; + } } diff --git a/notification/method/webpush.php b/notification/method/webpush.php index 0fc2a08..380ef66 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -378,7 +378,7 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo } return [ - 'NOTIFICATIONS_WEBPUSH_ENABLE' => $this->allowed_user() && ($this->config['wpn_webpush_dropdown_subscribe'] || stripos($this->user->page['page'], 'notification_options')), + 'NOTIFICATIONS_WEBPUSH_ENABLE' => $this->config['wpn_webpush_dropdown_subscribe'] || stripos($this->user->page['page'], 'notification_options'), 'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'), 'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'), 'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'], @@ -552,17 +552,4 @@ protected function load_recipients_data(): array return $notify_users; } - - /** - * User is allowed to use web push notifications - * - * @return bool - */ - protected function allowed_user() - { - return $this->user->id() !== ANONYMOUS - && !$this->user->data['is_bot'] - && (int) $this->user->data['user_type'] !== USER_IGNORE - && (int) $this->user->data['user_type'] !== USER_INACTIVE; - } } diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 42f5d02..7466c48 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -81,7 +81,11 @@ protected function setUp(): void return $route . '#' . serialize($params); }); - $this->config = new \phpbb\config\config([]); + $this->config = new \phpbb\config\config([ + 'load_notifications' => true, + 'allow_board_notifications' => true, + 'wpn_webpush_enable' => true, + ]); $this->form_helper = new \phpbb\webpushnotifications\form\form_helper( $this->config, @@ -127,6 +131,7 @@ protected function set_listener() $this->form_helper, $this->language, $this->template, + $this->user, $this->notifications, $this->root_path ); From a156afb45771796857e5ada3093f3f5d6e626852 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Wed, 17 Jul 2024 08:46:02 -0700 Subject: [PATCH 03/11] Improve disabled state JS Signed-off-by: Matt Friedman --- styles/all/template/webpush.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/styles/all/template/webpush.js b/styles/all/template/webpush.js index 588dac6..685f066 100644 --- a/styles/all/template/webpush.js +++ b/styles/all/template/webpush.js @@ -51,8 +51,7 @@ function PhpbbWebpush() { // Service workers are only supported in secure context if (window.isSecureContext !== true) { - subscribeButton.disabled = true; - handleDisabledState(); + setDisabledState(); return; } @@ -67,12 +66,11 @@ function PhpbbWebpush() { .catch(error => { console.info(error); // Service worker could not be registered - subscribeButton.disabled = true; + setDisabledState(); }); } else { - subscribeButton.disabled = true; + setDisabledState(); } - handleDisabledState(); }; /** @@ -80,17 +78,18 @@ function PhpbbWebpush() { * * @return void */ - function handleDisabledState() { - if (subscribeButton.disabled) { - const notificationList = document.getElementById('notification_list'); - const footer = notificationList.querySelector('.wpn-notification-dropdown-footer'); - if (footer) { - footer.style.display = 'none'; - } + function setDisabledState() { + subscribeButton.disabled = true; - if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) { - subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg'); - } + const notificationList = document.getElementById('notification_list'); + const subscribeToggle = notificationList.querySelector('.wpn-notification-dropdown-footer'); + + if (subscribeToggle) { + subscribeToggle.style.display = 'none'; + } + + if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) { + subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg'); } } From 6423e08411461d6747f98a020f92fbe68460d9ae Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 22 Jul 2024 07:35:44 -0700 Subject: [PATCH 04/11] Decouple manifest from push notifications Signed-off-by: Matt Friedman --- event/listener.php | 15 +++++++++++++- notification/method/webpush.php | 2 -- .../event/overall_header_head_append.html | 6 ++++++ .../template/ucp_notifications_webpush.html | 6 ------ tests/event/listener_test.php | 20 +++++++++++++++++-- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/event/listener.php b/event/listener.php index 20b04bf..2d987a3 100644 --- a/event/listener.php +++ b/event/listener.php @@ -87,7 +87,7 @@ public static function getSubscribedEvents() return [ 'core.ucp_display_module_before' => 'load_language', 'core.acp_main_notice' => 'compatibility_notice', - 'core.page_header_after' => 'load_template_data', + 'core.page_header_after' => [['load_template_data'], ['pwa_manifest']], 'core.acp_board_config_edit_add' => 'acp_pwa_options', 'core.validate_config_variable' => 'validate_pwa_options', ]; @@ -136,6 +136,19 @@ public function compatibility_notice() $this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>=')); } + /** + * Assign template data for web manifest support + * + * @return void + */ + public function pwa_manifest() + { + $this->template->assign_vars([ + 'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'), + 'U_TOUCH_ICON' => $this->config['pwa_icon_small'], + ]); + } + /** * Progressive web app options for the ACP * diff --git a/notification/method/webpush.php b/notification/method/webpush.php index 380ef66..6bb79e3 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -385,8 +385,6 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo 'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'), 'SUBSCRIPTIONS' => $subscriptions, 'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP), - 'U_MANIFEST_URL' => $controller_helper->route('phpbb_webpushnotifications_manifest_controller'), - 'U_TOUCH_ICON' => $this->config['pwa_icon_small'], ]; } diff --git a/styles/all/template/event/overall_header_head_append.html b/styles/all/template/event/overall_header_head_append.html index 98c0cfa..8afce31 100644 --- a/styles/all/template/event/overall_header_head_append.html +++ b/styles/all/template/event/overall_header_head_append.html @@ -1,3 +1,9 @@ + + + + +{% if U_TOUCH_ICON %}{% endif %} + {% if NOTIFICATIONS_WEBPUSH_ENABLE %} {% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %} {% endif %} diff --git a/styles/all/template/ucp_notifications_webpush.html b/styles/all/template/ucp_notifications_webpush.html index a55e262..16ebf96 100644 --- a/styles/all/template/ucp_notifications_webpush.html +++ b/styles/all/template/ucp_notifications_webpush.html @@ -1,9 +1,3 @@ - - - - -{% if U_TOUCH_ICON %}{% endif %} -