Skip to content

Commit ea4db3c

Browse files
committed
Add in latest webpush updates
[ticket/17010] Update web-push-testing to 1.1.0 [ticket/17010] Increase test coverage [ticket/17010] Update web-push-testing to 1.1.1 [ticket/17010] Properly handle expired subscriptions and extend tests Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 3ec0fd1 commit ea4db3c

5 files changed

Lines changed: 413 additions & 38 deletions

File tree

notification/method/webpush.php

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,27 @@ protected function notify_using_webpush(): void
238238
}
239239

240240
// Remove any subscriptions that couldn't be queued, i.e. that have invalid data
241-
if (count($remove_subscriptions))
242-
{
243-
$sql = 'DELETE FROM ' . $this->push_subscriptions_table . '
244-
WHERE ' . $this->db->sql_in_set('subscription_id', $remove_subscriptions);
245-
$this->db->sql_query($sql);
246-
}
241+
$this->remove_subscriptions($remove_subscriptions);
242+
243+
// List to fill with expired subscriptions based on return
244+
$expired_endpoints = [];
247245

248246
try
249247
{
250248
foreach ($web_push->flush($number_of_notifications) as $report)
251249
{
252250
if (!$report->isSuccess())
253251
{
254-
$report_data = json_sanitizer::sanitize($report->jsonSerialize());
255-
$this->log->add('admin', ANONYMOUS, '', 'LOG_WEBPUSH_MESSAGE_FAIL', false, [$report_data['reason']]);
252+
// Fill array of endpoints to remove if subscription has expired
253+
if ($report->isSubscriptionExpired())
254+
{
255+
$expired_endpoints[] = $report->getEndpoint();
256+
}
257+
else
258+
{
259+
$report_data = json_sanitizer::sanitize($report->jsonSerialize());
260+
$this->log->add('admin', ANONYMOUS, '', 'LOG_WEBPUSH_MESSAGE_FAIL', false, [$report_data['reason']]);
261+
}
256262
}
257263
}
258264
}
@@ -261,6 +267,8 @@ protected function notify_using_webpush(): void
261267
$this->log->add('critical', ANONYMOUS, '', 'LOG_WEBPUSH_MESSAGE_FAIL', false, [$exception->getMessage()]);
262268
}
263269

270+
$this->clean_expired_subscriptions($user_subscription_map, $expired_endpoints);
271+
264272
// We're done, empty the queue
265273
$this->empty_queue();
266274
}
@@ -372,4 +380,52 @@ protected function get_user_subscription_map(array $notify_users): array
372380

373381
return $user_subscription_map;
374382
}
383+
384+
/**
385+
* Remove subscriptions
386+
*
387+
* @param array $subscription_ids Subscription ids to remove
388+
* @return void
389+
*/
390+
public function remove_subscriptions(array $subscription_ids): void
391+
{
392+
if (count($subscription_ids))
393+
{
394+
$sql = 'DELETE FROM ' . $this->push_subscriptions_table . '
395+
WHERE ' . $this->db->sql_in_set('subscription_id', $subscription_ids);
396+
$this->db->sql_query($sql);
397+
}
398+
}
399+
400+
/**
401+
* Clean expired subscriptions from the database
402+
*
403+
* @param array $user_subscription_map User subscription map
404+
* @param array $expired_endpoints Expired endpoints
405+
* @return void
406+
*/
407+
protected function clean_expired_subscriptions(array $user_subscription_map, array $expired_endpoints): void
408+
{
409+
if (!count($expired_endpoints))
410+
{
411+
return;
412+
}
413+
414+
$remove_subscriptions = [];
415+
foreach ($expired_endpoints as $endpoint)
416+
{
417+
foreach ($user_subscription_map as $subscriptions)
418+
{
419+
foreach ($subscriptions as $subscription)
420+
{
421+
if (isset($subscription['endpoint']) && $subscription['endpoint'] == $endpoint)
422+
{
423+
$remove_subscriptions[] = $subscription['subscription_id'];
424+
}
425+
}
426+
}
427+
}
428+
429+
$this->remove_subscriptions($remove_subscriptions);
430+
}
375431
}

package-lock.json

Lines changed: 39 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"author": "phpBB Limited",
44
"license": "GPL-2.0",
55
"devDependencies": {
6-
"web-push-testing": "^1.0.0"
6+
"web-push-testing": "^1.1.1"
77
}
88
}

tests/notification/fixtures/webpush_notification.type.post.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<dataset>
33
<table name="phpbb_banlist">
44
</table>
5+
<table name="phpbb_log">
6+
</table>
57
<table name="phpbb_forums_watch">
68
<column>forum_id</column>
79
<column>user_id</column>
@@ -135,6 +137,12 @@
135137
<column>username_clean</column>
136138
<column>user_permissions</column>
137139
<column>user_sig</column>
140+
<row>
141+
<value>1</value>
142+
<value>Anonymous</value>
143+
<value></value>
144+
<value></value>
145+
</row>
138146
<row>
139147
<value>2</value>
140148
<value>poster</value>

0 commit comments

Comments
 (0)