@@ -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}
0 commit comments