Skip to content

Commit 36a41c2

Browse files
authored
Merge pull request #74 from iMattPro/manifest
General fixes
2 parents e340b99 + 851787b commit 36a41c2

10 files changed

Lines changed: 107 additions & 67 deletions

File tree

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
- '@phpbb.wpn.form_helper'
1919
- '@language'
2020
- '@template'
21+
- '@user'
2122
- '@notification_manager'
2223
- '%core.root_path%'
2324
tags:

controller/manifest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function handle(): JsonResponse
6161
}
6262

6363
$board_path = $this->config['force_server_vars'] ? $this->config['script_path'] : $this->path_helper->get_web_root_path();
64+
$board_url = generate_board_url();
6465

6566
$manifest = [
6667
'name' => $this->config['sitename'],
@@ -76,12 +77,12 @@ public function handle(): JsonResponse
7677
{
7778
$manifest['icons'] = [
7879
[
79-
'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'],
80+
'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'],
8081
'sizes' => '192x192',
8182
'type' => 'image/png'
8283
],
8384
[
84-
'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'],
85+
'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'],
8586
'sizes' => '512x512',
8687
'type' => 'image/png'
8788
]

event/listener.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010

1111
namespace phpbb\webpushnotifications\event;
1212

13-
/**
14-
* @ignore
15-
*/
16-
1713
use FastImageSize\FastImageSize;
1814
use phpbb\config\config;
1915
use phpbb\controller\helper as controller_helper;
2016
use phpbb\language\language;
2117
use phpbb\notification\manager;
2218
use phpbb\template\template;
19+
use phpbb\user;
2320
use phpbb\webpushnotifications\form\form_helper;
2421
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2522

@@ -46,6 +43,9 @@ class listener implements EventSubscriberInterface
4643
/* @var template */
4744
protected $template;
4845

46+
/** @var user */
47+
protected $user;
48+
4949
/* @var manager */
5050
protected $phpbb_notifications;
5151

@@ -61,27 +61,29 @@ class listener implements EventSubscriberInterface
6161
* @param form_helper $form_helper Form helper object
6262
* @param language $language Language object
6363
* @param template $template Template object
64+
* @param user $user
6465
* @param manager $phpbb_notifications Notifications manager object
6566
* @param $root_path
6667
*/
67-
public function __construct(config $config, controller_helper $controller_helper, FastImageSize $imagesize, form_helper $form_helper, language $language, template $template, manager $phpbb_notifications, $root_path)
68+
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)
6869
{
6970
$this->config = $config;
7071
$this->controller_helper = $controller_helper;
7172
$this->imagesize = $imagesize;
7273
$this->form_helper = $form_helper;
7374
$this->language = $language;
7475
$this->template = $template;
76+
$this->user = $user;
7577
$this->phpbb_notifications = $phpbb_notifications;
7678
$this->root_path = $root_path;
7779
}
7880

7981
public static function getSubscribedEvents()
8082
{
8183
return [
84+
'core.page_header_after' => [['load_template_data'], ['pwa_manifest']],
8285
'core.ucp_display_module_before' => 'load_language',
8386
'core.acp_main_notice' => 'compatibility_notice',
84-
'core.page_header_after' => 'load_template_data',
8587
'core.acp_board_config_edit_add' => 'acp_pwa_options',
8688
'core.validate_config_variable' => 'validate_pwa_options',
8789
];
@@ -92,23 +94,30 @@ public static function getSubscribedEvents()
9294
*/
9395
public function load_template_data()
9496
{
97+
if (!$this->can_use_notifications())
98+
{
99+
return;
100+
}
101+
95102
$methods = $this->phpbb_notifications->get_subscription_methods();
96103
$webpush_method = $methods['notification.method.phpbb.wpn.webpush'] ?? null;
97104

98-
if ($webpush_method !== null)
105+
if ($webpush_method === null)
99106
{
100-
if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH'))
101-
{
102-
$this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications');
103-
}
107+
return;
108+
}
104109

105-
$template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
106-
$this->template->assign_vars($template_ary);
110+
if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH'))
111+
{
112+
$this->load_language();
107113
}
114+
115+
$template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
116+
$this->template->assign_vars($template_ary);
108117
}
109118

110119
/**
111-
* Load language file
120+
* Load language file (this is required for the UCP)
112121
*/
113122
public function load_language()
114123
{
@@ -123,6 +132,19 @@ public function compatibility_notice()
123132
$this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='));
124133
}
125134

135+
/**
136+
* Assign template data for web manifest support
137+
*
138+
* @return void
139+
*/
140+
public function pwa_manifest()
141+
{
142+
$this->template->assign_vars([
143+
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
144+
'U_TOUCH_ICON' => $this->config['pwa_icon_small'],
145+
]);
146+
}
147+
126148
/**
127149
* Progressive web app options for the ACP
128150
*
@@ -217,4 +239,16 @@ protected function add_error($event, $error_key, $param = null)
217239
$error[] = $this->language->lang($error_key, $param);
218240
$event['error'] = $error;
219241
}
242+
243+
/**
244+
* Can notifications be used by the user?
245+
*
246+
* @return bool
247+
*/
248+
protected function can_use_notifications()
249+
{
250+
return $this->config['wpn_webpush_enable']
251+
&& ANONYMOUS !== $this->user->id()
252+
&& USER_IGNORE !== (int) $this->user->data['user_type'];
253+
}
220254
}

notification/method/webpush.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,13 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
378378
}
379379

380380
return [
381-
'NOTIFICATIONS_WEBPUSH_ENABLE' => $this->allowed_user() && ($this->config['wpn_webpush_dropdown_subscribe'] || stripos($this->user->page['page'], 'notification_options')),
381+
'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'),
382382
'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
383383
'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
384384
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
385385
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
386386
'SUBSCRIPTIONS' => $subscriptions,
387387
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
388-
'U_MANIFEST_URL' => $controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
389-
'U_TOUCH_ICON' => $this->config['pwa_icon_small'],
390388
];
391389
}
392390

@@ -552,17 +550,4 @@ protected function load_recipients_data(): array
552550

553551
return $notify_users;
554552
}
555-
556-
/**
557-
* User is allowed to use web push notifications
558-
*
559-
* @return bool
560-
*/
561-
protected function allowed_user()
562-
{
563-
return $this->user->id() !== ANONYMOUS
564-
&& !$this->user->data['is_bot']
565-
&& (int) $this->user->data['user_type'] !== USER_IGNORE
566-
&& (int) $this->user->data['user_type'] !== USER_INACTIVE;
567-
}
568553
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<meta name="apple-mobile-web-app-capable" content="yes">
2+
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
3+
<meta name="apple-mobile-web-app-title" content="{{ SITENAME }}">
4+
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
5+
{% if U_TOUCH_ICON %}<link rel="apple-touch-icon" href="{{ T_ICONS_PATH ~ U_TOUCH_ICON }}">{% endif %}
6+
17
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
28
{% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %}
39
{% endif %}

styles/all/template/ucp_notifications_webpush.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<meta name="apple-mobile-web-app-capable" content="yes">
2-
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
3-
<meta name="apple-mobile-web-app-title" content="{{ SITENAME }}">
4-
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
5-
{% if U_TOUCH_ICON %}<link rel="apple-touch-icon" href="{{ T_ICONS_PATH ~ U_TOUCH_ICON }}">{% endif %}
6-
71
<script>
82
phpbbWebpushOptions = {
93
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',

styles/all/template/webpush.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ function PhpbbWebpush() {
5151

5252
// Service workers are only supported in secure context
5353
if (window.isSecureContext !== true) {
54-
subscribeButton.disabled = true;
55-
handleDisabledState();
54+
setDisabledState();
5655
return;
5756
}
5857

@@ -67,30 +66,30 @@ function PhpbbWebpush() {
6766
.catch(error => {
6867
console.info(error);
6968
// Service worker could not be registered
70-
subscribeButton.disabled = true;
69+
setDisabledState();
7170
});
7271
} else {
73-
subscribeButton.disabled = true;
72+
setDisabledState();
7473
}
75-
handleDisabledState();
7674
};
7775

7876
/**
7977
* If subscribing is disabled, hide dropdown toggle and update subscribe button text
8078
*
8179
* @return void
8280
*/
83-
function handleDisabledState() {
84-
if (subscribeButton.disabled) {
85-
const notificationList = document.getElementById('notification_list');
86-
const footer = notificationList.querySelector('.wpn-notification-dropdown-footer');
87-
if (footer) {
88-
footer.style.display = 'none';
89-
}
81+
function setDisabledState() {
82+
subscribeButton.disabled = true;
9083

91-
if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) {
92-
subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg');
93-
}
84+
const notificationList = document.getElementById('notification_list');
85+
const subscribeToggle = notificationList.querySelector('.wpn-notification-dropdown-footer');
86+
87+
if (subscribeToggle) {
88+
subscribeToggle.style.display = 'none';
89+
}
90+
91+
if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) {
92+
subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg');
9493
}
9594
}
9695

tests/event/listener_test.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ protected function setUp(): void
8181
return $route . '#' . serialize($params);
8282
});
8383

84-
$this->config = new \phpbb\config\config([]);
84+
$this->config = new \phpbb\config\config([
85+
'load_notifications' => true,
86+
'allow_board_notifications' => true,
87+
'wpn_webpush_enable' => true,
88+
]);
8589

8690
$this->form_helper = new \phpbb\webpushnotifications\form\form_helper(
8791
$this->config,
@@ -127,6 +131,7 @@ protected function set_listener()
127131
$this->form_helper,
128132
$this->language,
129133
$this->template,
134+
$this->user,
130135
$this->notifications,
131136
$this->root_path
132137
);
@@ -141,9 +146,9 @@ public function test_construct()
141146
public function test_getSubscribedEvents()
142147
{
143148
self::assertEquals([
149+
'core.page_header_after',
144150
'core.ucp_display_module_before',
145151
'core.acp_main_notice',
146-
'core.page_header_after',
147152
'core.acp_board_config_edit_add',
148153
'core.validate_config_variable',
149154
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
@@ -224,7 +229,7 @@ public function get_ucp_template_data_data()
224229
/**
225230
* @dataProvider get_ucp_template_data_data
226231
*/
227-
public function test_get_ucp_template_data($user_id, $method_data, $subscriptions, $expected)
232+
public function test_load_template_data($user_id, $method_data, $subscriptions, $expected)
228233
{
229234
$this->config['wpn_webpush_dropdown_subscribe'] = true;
230235
$this->user->data['user_id'] = $user_id;
@@ -238,8 +243,6 @@ public function test_get_ucp_template_data($user_id, $method_data, $subscription
238243
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
239244
'SUBSCRIPTIONS' => $subscriptions,
240245
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
241-
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
242-
'U_TOUCH_ICON' => '',
243246
]
244247
);
245248

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

262+
public function test_pwa_manifest()
263+
{
264+
$this->config['pwa_icon_small'] = 'icon-192x192.png';
265+
266+
$this->set_listener();
267+
268+
$this->template->expects(self::once())
269+
->method('assign_vars')
270+
->with([
271+
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
272+
'U_TOUCH_ICON' => 'icon-192x192.png',
273+
]);
274+
275+
$dispatcher = new \phpbb\event\dispatcher();
276+
$dispatcher->addListener('core.acp_main_notice', [$this->listener, 'pwa_manifest']);
277+
$dispatcher->trigger_event('core.acp_main_notice');
278+
}
279+
259280
public function acp_pwa_options_data()
260281
{
261282
return [

tests/notification/notification_method_webpush_test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,8 @@ public function test_export_data_with_migration(): void
810810
'item_parent_id' => ['ULINT', 0],
811811
'user_id' => ['ULINT', 0],
812812
'push_data' => ['MTEXT', ''],
813-
'notification_time' => ['TIMESTAMP', 0]
813+
'notification_time' => ['TIMESTAMP', 0],
814+
'push_token' => ['VCHAR', ''],
814815
],
815816
'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'],
816817
];

ucp/controller/webpush.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ public function notification(): JsonResponse
103103
throw new http_exception(Response::HTTP_FORBIDDEN, 'NO_AUTH_OPERATION');
104104
}
105105

106-
if ($this->user->id() !== ANONYMOUS)
107-
{
108-
$notification_data = $this->get_user_notifications();
109-
}
110-
else
111-
{
112-
$notification_data = $this->get_anonymous_notifications();
113-
}
106+
$notification_data = $this->get_user_notifications();
114107

115108
// Decode and return data if everything is fine
116109
$data = json_decode($notification_data, true);
@@ -126,6 +119,11 @@ public function notification(): JsonResponse
126119
*/
127120
private function get_user_notifications(): string
128121
{
122+
if ($this->user->id() === ANONYMOUS)
123+
{
124+
return $this->get_anonymous_notifications();
125+
}
126+
129127
// Subscribe should only be available for logged-in "normal" users
130128
if ($this->user->data['user_type'] == USER_IGNORE)
131129
{

0 commit comments

Comments
 (0)