Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General fixes #74

Merged
merged 11 commits into from
Aug 1, 2024
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
- '@phpbb.wpn.form_helper'
- '@language'
- '@template'
- '@user'
- '@notification_manager'
- '%core.root_path%'
tags:
Expand Down
5 changes: 3 additions & 2 deletions controller/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'
]
Expand Down
62 changes: 48 additions & 14 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@

namespace phpbb\webpushnotifications\event;

/**
* @ignore
*/

use FastImageSize\FastImageSize;
use phpbb\config\config;
use phpbb\controller\helper as controller_helper;
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;

Expand All @@ -46,6 +43,9 @@ class listener implements EventSubscriberInterface
/* @var template */
protected $template;

/** @var user */
protected $user;

/* @var manager */
protected $phpbb_notifications;

Expand All @@ -61,27 +61,29 @@ 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;
$this->imagesize = $imagesize;
$this->form_helper = $form_helper;
$this->language = $language;
$this->template = $template;
$this->user = $user;
$this->phpbb_notifications = $phpbb_notifications;
$this->root_path = $root_path;
}

public static function getSubscribedEvents()
{
return [
'core.page_header_after' => [['load_template_data'], ['pwa_manifest']],
'core.ucp_display_module_before' => 'load_language',
'core.acp_main_notice' => 'compatibility_notice',
'core.page_header_after' => 'load_template_data',
'core.acp_board_config_edit_add' => 'acp_pwa_options',
'core.validate_config_variable' => 'validate_pwa_options',
];
Expand All @@ -92,23 +94,30 @@ 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->load_language();
}

$template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
$this->template->assign_vars($template_ary);
}

/**
* Load language file
* Load language file (this is required for the UCP)
*/
public function load_language()
{
Expand All @@ -123,6 +132,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
*
Expand Down Expand Up @@ -217,4 +239,16 @@ 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['wpn_webpush_enable']
&& ANONYMOUS !== $this->user->id()
&& USER_IGNORE !== (int) $this->user->data['user_type'];
}
}
17 changes: 1 addition & 16 deletions notification/method/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,13 @@ 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['load_notifications'] && $this->config['allow_board_notifications'] && $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'],
'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'],
];
}

Expand Down Expand Up @@ -552,17 +550,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;
}
}
6 changes: 6 additions & 0 deletions styles/all/template/event/overall_header_head_append.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="{{ SITENAME }}">
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
{% if U_TOUCH_ICON %}<link rel="apple-touch-icon" href="{{ T_ICONS_PATH ~ U_TOUCH_ICON }}">{% endif %}

{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
{% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %}
{% endif %}
6 changes: 0 additions & 6 deletions styles/all/template/ucp_notifications_webpush.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="{{ SITENAME }}">
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
{% if U_TOUCH_ICON %}<link rel="apple-touch-icon" href="{{ T_ICONS_PATH ~ U_TOUCH_ICON }}">{% endif %}

<script>
phpbbWebpushOptions = {
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
Expand Down
29 changes: 14 additions & 15 deletions styles/all/template/webpush.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ function PhpbbWebpush() {

// Service workers are only supported in secure context
if (window.isSecureContext !== true) {
subscribeButton.disabled = true;
handleDisabledState();
setDisabledState();
return;
}

Expand All @@ -67,30 +66,30 @@ function PhpbbWebpush() {
.catch(error => {
console.info(error);
// Service worker could not be registered
subscribeButton.disabled = true;
setDisabledState();
});
} else {
subscribeButton.disabled = true;
setDisabledState();
}
handleDisabledState();
};

/**
* If subscribing is disabled, hide dropdown toggle and update subscribe button text
*
* @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');
}
}

Expand Down
31 changes: 26 additions & 5 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -127,6 +131,7 @@ protected function set_listener()
$this->form_helper,
$this->language,
$this->template,
$this->user,
$this->notifications,
$this->root_path
);
Expand All @@ -141,9 +146,9 @@ public function test_construct()
public function test_getSubscribedEvents()
{
self::assertEquals([
'core.page_header_after',
'core.ucp_display_module_before',
'core.acp_main_notice',
'core.page_header_after',
'core.acp_board_config_edit_add',
'core.validate_config_variable',
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
Expand Down Expand Up @@ -224,7 +229,7 @@ public function get_ucp_template_data_data()
/**
* @dataProvider get_ucp_template_data_data
*/
public function test_get_ucp_template_data($user_id, $method_data, $subscriptions, $expected)
public function test_load_template_data($user_id, $method_data, $subscriptions, $expected)
{
$this->config['wpn_webpush_dropdown_subscribe'] = true;
$this->user->data['user_id'] = $user_id;
Expand All @@ -238,8 +243,6 @@ public function test_get_ucp_template_data($user_id, $method_data, $subscription
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
'SUBSCRIPTIONS' => $subscriptions,
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
'U_TOUCH_ICON' => '',
]
);

Expand All @@ -256,6 +259,24 @@ public function test_get_ucp_template_data($user_id, $method_data, $subscription
$dispatcher->trigger_event('core.page_header_after');
}

public function test_pwa_manifest()
{
$this->config['pwa_icon_small'] = 'icon-192x192.png';

$this->set_listener();

$this->template->expects(self::once())
->method('assign_vars')
->with([
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
'U_TOUCH_ICON' => 'icon-192x192.png',
]);

$dispatcher = new \phpbb\event\dispatcher();
$dispatcher->addListener('core.acp_main_notice', [$this->listener, 'pwa_manifest']);
$dispatcher->trigger_event('core.acp_main_notice');
}

public function acp_pwa_options_data()
{
return [
Expand Down
3 changes: 2 additions & 1 deletion tests/notification/notification_method_webpush_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,8 @@ public function test_export_data_with_migration(): void
'item_parent_id' => ['ULINT', 0],
'user_id' => ['ULINT', 0],
'push_data' => ['MTEXT', ''],
'notification_time' => ['TIMESTAMP', 0]
'notification_time' => ['TIMESTAMP', 0],
'push_token' => ['VCHAR', ''],
],
'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'],
];
Expand Down
14 changes: 6 additions & 8 deletions ucp/controller/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ public function notification(): JsonResponse
throw new http_exception(Response::HTTP_FORBIDDEN, 'NO_AUTH_OPERATION');
}

if ($this->user->id() !== ANONYMOUS)
{
$notification_data = $this->get_user_notifications();
}
else
{
$notification_data = $this->get_anonymous_notifications();
}
$notification_data = $this->get_user_notifications();

// Decode and return data if everything is fine
$data = json_decode($notification_data, true);
Expand All @@ -126,6 +119,11 @@ public function notification(): JsonResponse
*/
private function get_user_notifications(): string
{
if ($this->user->id() === ANONYMOUS)
{
return $this->get_anonymous_notifications();
}

// Subscribe should only be available for logged-in "normal" users
if ($this->user->data['user_type'] == USER_IGNORE)
{
Expand Down
Loading