[ticket/17010] Remove undeliverable subscriptions

PHPBB3-17010
This commit is contained in:
Marc Alexander 2022-11-01 12:44:22 +01:00
parent e97313839e
commit edaff6cd2d
No known key found for this signature in database
GPG key ID: 50E0D2423696F995

View file

@ -13,6 +13,7 @@
namespace phpbb\notification\method; namespace phpbb\notification\method;
use Minishlink\WebPush\Subscription;
use phpbb\config\config; use phpbb\config\config;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\notification\type\type_interface; use phpbb\notification\type\type_interface;
@ -24,7 +25,7 @@ use phpbb\user_loader;
* This class handles sending push messages for notifications * This class handles sending push messages for notifications
*/ */
class webpush extends \phpbb\notification\method\messenger_base class webpush extends messenger_base
{ {
/** @var config */ /** @var config */
protected $config; protected $config;
@ -169,7 +170,7 @@ class webpush extends \phpbb\notification\method\messenger_base
// Get subscriptions for users // Get subscriptions for users
$user_subscription_map = []; $user_subscription_map = [];
$sql = 'SELECT user_id, endpoint, p256dh, auth $sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth
FROM ' . $this->push_subscriptions_table . ' FROM ' . $this->push_subscriptions_table . '
WHERE ' . $this->db->sql_in_set('user_id', $notify_users); WHERE ' . $this->db->sql_in_set('user_id', $notify_users);
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
@ -196,6 +197,8 @@ class webpush extends \phpbb\notification\method\messenger_base
$web_push = new \Minishlink\WebPush\WebPush($auth); $web_push = new \Minishlink\WebPush\WebPush($auth);
$number_of_notifications = 0; $number_of_notifications = 0;
$remove_subscriptions = [];
// Time to go through the queue and send notifications // Time to go through the queue and send notifications
/** @var type_interface $notification */ /** @var type_interface $notification */
foreach ($this->queue as $notification) foreach ($this->queue as $notification)
@ -221,7 +224,7 @@ class webpush extends \phpbb\notification\method\messenger_base
{ {
try try
{ {
$push_subscription = \Minishlink\WebPush\Subscription::create([ $push_subscription = Subscription::create([
'endpoint' => $subscription['endpoint'], 'endpoint' => $subscription['endpoint'],
'keys' => [ 'keys' => [
'p256dh' => $subscription['p256dh'], 'p256dh' => $subscription['p256dh'],
@ -233,13 +236,19 @@ class webpush extends \phpbb\notification\method\messenger_base
} }
catch (\ErrorException $exception) catch (\ErrorException $exception)
{ {
// @todo: decide whether we want to remove invalid subscriptions directly? $remove_subscriptions[] = $subscription['subscription_id'];
// Might need too many resources ...
} }
} }
} }
// @todo: Try offloading to after request // Remove any subscriptions that couldn't be queued, i.e. that have invalid data
if (count($remove_subscriptions))
{
$sql = 'DELETE FROM ' . $this->push_subscriptions_table . '
WHERE ' . $this->db->sql_in_set('subscription_id', $remove_subscriptions);
$this->db->sql_query($sql);
}
try try
{ {
foreach ($web_push->flush($number_of_notifications) as $report) foreach ($web_push->flush($number_of_notifications) as $report)