Skip to content

Commit c55aef6

Browse files
committed
Fix loading notifications for users in better way
Signed-off-by: Matt Friedman <maf675@gmail.com> fix Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent a69ab01 commit c55aef6

4 files changed

Lines changed: 43 additions & 23 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:

event/listener.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpbb\language\language;
2121
use phpbb\notification\manager;
2222
use phpbb\template\template;
23+
use phpbb\user;
2324
use phpbb\webpushnotifications\form\form_helper;
2425
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2526

@@ -46,6 +47,9 @@ class listener implements EventSubscriberInterface
4647
/* @var template */
4748
protected $template;
4849

50+
/** @var user */
51+
protected $user;
52+
4953
/* @var manager */
5054
protected $phpbb_notifications;
5155

@@ -61,17 +65,19 @@ class listener implements EventSubscriberInterface
6165
* @param form_helper $form_helper Form helper object
6266
* @param language $language Language object
6367
* @param template $template Template object
68+
* @param user $user
6469
* @param manager $phpbb_notifications Notifications manager object
6570
* @param $root_path
6671
*/
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)
72+
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)
6873
{
6974
$this->config = $config;
7075
$this->controller_helper = $controller_helper;
7176
$this->imagesize = $imagesize;
7277
$this->form_helper = $form_helper;
7378
$this->language = $language;
7479
$this->template = $template;
80+
$this->user = $user;
7581
$this->phpbb_notifications = $phpbb_notifications;
7682
$this->root_path = $root_path;
7783
}
@@ -92,19 +98,26 @@ public static function getSubscribedEvents()
9298
*/
9399
public function load_template_data()
94100
{
101+
if (!$this->can_use_notifications())
102+
{
103+
return;
104+
}
105+
95106
$methods = $this->phpbb_notifications->get_subscription_methods();
96107
$webpush_method = $methods['notification.method.phpbb.wpn.webpush'] ?? null;
97108

98-
if ($webpush_method !== null)
109+
if ($webpush_method === null)
99110
{
100-
if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH'))
101-
{
102-
$this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications');
103-
}
111+
return;
112+
}
104113

105-
$template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
106-
$this->template->assign_vars($template_ary);
114+
if (!$this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH'))
115+
{
116+
$this->language->add_lang('webpushnotifications_module_ucp', 'phpbb/webpushnotifications');
107117
}
118+
119+
$template_ary = $webpush_method['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
120+
$this->template->assign_vars($template_ary);
108121
}
109122

110123
/**
@@ -217,4 +230,18 @@ protected function add_error($event, $error_key, $param = null)
217230
$error[] = $this->language->lang($error_key, $param);
218231
$event['error'] = $error;
219232
}
233+
234+
/**
235+
* Can notifications be used by the user?
236+
*
237+
* @return bool
238+
*/
239+
protected function can_use_notifications()
240+
{
241+
return $this->config['load_notifications']
242+
&& $this->config['allow_board_notifications']
243+
&& $this->config['wpn_webpush_enable']
244+
&& $this->user->id() !== ANONYMOUS
245+
&& (int) $this->user->data['user_type'] !== USER_IGNORE;
246+
}
220247
}

notification/method/webpush.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ 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['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'],
@@ -552,17 +552,4 @@ protected function load_recipients_data(): array
552552

553553
return $notify_users;
554554
}
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-
}
568555
}

tests/event/listener_test.php

Lines changed: 6 additions & 1 deletion
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
);

0 commit comments

Comments
 (0)