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 @@ -26,6 +26,7 @@ services:
arguments:
- '@config'
- '@dbal.conn'
- '@language'
- '@log'
- '@user_loader'
- '@user'
Expand Down
75 changes: 54 additions & 21 deletions notification/method/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use phpbb\config\config;
use phpbb\controller\helper;
use phpbb\db\driver\driver_interface;
use phpbb\language\language;
use phpbb\log\log_interface;
use phpbb\notification\method\messenger_base;
use phpbb\notification\type\type_interface;
Expand All @@ -36,6 +37,9 @@ class webpush extends messenger_base implements extended_method_interface
/** @var driver_interface */
protected $db;

/** @var language */
protected $language;

/** @var log_interface */
protected $log;

Expand All @@ -62,6 +66,7 @@ class webpush extends messenger_base implements extended_method_interface
*
* @param config $config
* @param driver_interface $db
* @param language $language
* @param log_interface $log
* @param user_loader $user_loader
* @param user $user
Expand All @@ -71,13 +76,14 @@ class webpush extends messenger_base implements extended_method_interface
* @param string $notification_webpush_table
* @param string $push_subscriptions_table
*/
public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, path_helper $path_helper,
public function __construct(config $config, driver_interface $db, language $language, log_interface $log, user_loader $user_loader, user $user, path_helper $path_helper,
string $phpbb_root_path, string $php_ext, string $notification_webpush_table, string $push_subscriptions_table)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);

$this->config = $config;
$this->db = $db;
$this->language = $language;
$this->log = $log;
$this->user = $user;
$this->path_helper = $path_helper;
Expand Down Expand Up @@ -140,10 +146,21 @@ public function notify()
{
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_webpush_table);

// Load all users data we want to notify
$notify_users = $this->load_recipients_data();

/** @var type_interface $notification */
foreach ($this->queue as $notification)
{
$data = $notification->get_insert_array();

// Change notification language if needed only
$recipient_data = $this->user_loader->get_user($notification->user_id);
if ($this->language->get_used_language() !== $recipient_data['user_lang'])
{
$this->language->set_user_language($recipient_data['user_lang'], true);
}

$data += [
'push_data' => json_encode([
'heading' => $this->config['sitename'],
Expand All @@ -162,41 +179,30 @@ public function notify()

$insert_buffer->flush();

$this->notify_using_webpush();
// Restore current user's language if needed only
if ($this->language->get_used_language() !== $this->user->data['user_lang'])
{
$this->language->set_user_language($this->user->data['user_lang'], true);
}

$this->notify_using_webpush($notify_users);

return false;
}

/**
* Notify using Web Push
*
* @param array $notify_users Array of user ids to notify
* @return void
*/
protected function notify_using_webpush(): void
protected function notify_using_webpush($notify_users = []): void
{
if (empty($this->queue))
{
return;
}

// Load all users we want to notify
$user_ids = [];
foreach ($this->queue as $notification)
{
$user_ids[] = $notification->user_id;
}

// Do not send push notifications to banned users
if (!function_exists('phpbb_get_banned_user_ids'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
}
$banned_users = phpbb_get_banned_user_ids($user_ids);

// Load all the users we need
$notify_users = array_diff($user_ids, $banned_users);
$this->user_loader->load_users($notify_users, array(USER_IGNORE));

// Get subscriptions for users
$user_subscription_map = $this->get_user_subscription_map($notify_users);

Expand Down Expand Up @@ -518,4 +524,31 @@ protected function set_endpoint_padding(\Minishlink\WebPush\WebPush $web_push, s
}
}
}

/**
* Load all users data to send notifications
*
* @return array Array of user ids to notify
*/
protected function load_recipients_data(): array
{
$notify_users = $user_ids = [];
foreach ($this->queue as $notification)
{
$user_ids[] = $notification->user_id;
}

// Do not send push notifications to banned users
if (!function_exists('phpbb_get_banned_user_ids'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
}
$banned_users = phpbb_get_banned_user_ids($user_ids);

// Load all the users we need
$notify_users = array_diff($user_ids, $banned_users);
$this->user_loader->load_users($notify_users, [USER_IGNORE]);

return $notify_users;
}
}
1 change: 1 addition & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function setUp(): void
$this->notification_method_webpush = new \phpbb\webpushnotifications\notification\method\webpush(
$this->config,
$db,
$this->language,
new \phpbb\log\dummy(),
$user_loader,
$this->user,
Expand Down
1 change: 1 addition & 0 deletions tests/notification/notification_method_webpush_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ protected function setUp(): void
$this->notification_method_webpush = new webpush(
$phpbb_container->get('config'),
$phpbb_container->get('dbal.conn'),
$phpbb_container->get('language'),
$phpbb_container->get('log'),
$phpbb_container->get('user_loader'),
$phpbb_container->get('user'),
Expand Down