From 13da4e1ca08ae7caf1b045a4024b0d4eeb24f2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?= Date: Thu, 19 Oct 2023 23:00:51 +0200 Subject: [PATCH] feat: Autoload subscriberId --- .../manager/PushNotificationManager.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/module/push-notification/manager/PushNotificationManager.php b/app/module/push-notification/manager/PushNotificationManager.php index 1cd50684..8ef4017b 100644 --- a/app/module/push-notification/manager/PushNotificationManager.php +++ b/app/module/push-notification/manager/PushNotificationManager.php @@ -6,12 +6,14 @@ use Minishlink\WebPush\MessageSentReport; use Minishlink\WebPush\Subscription; use Minishlink\WebPush\WebPush; +use Nette\Http\Url; use Nette\NotImplementedException; use Tracy\Debugger; use Tracy\ILogger; use Tymy\Module\Core\Factory\ManagerFactory; use Tymy\Module\Core\Manager\BaseManager; use Tymy\Module\Core\Model\BaseModel; +use Tymy\Module\Core\Model\Field; use Tymy\Module\PushNotification\Mapper\SubscriberMapper; use Tymy\Module\PushNotification\Model\PushNotification; use Tymy\Module\PushNotification\Model\Subscriber; @@ -29,13 +31,22 @@ public function __construct(ManagerFactory $managerFactory, private WebPush $web /** * Get Push Notification subscription based on user ID and subscription */ - public function getByUserAndSubscription(int $userId, string $subscription): ?\Tymy\Module\Core\Model\BaseModel + public function getByUserAndSubscription(int $userId, string $subscription): ?BaseModel { return $this->map($this->database->table(Subscriber::TABLE) ->where("user_id", $userId) ->where("subscription", $subscription)->fetch()); } + /** + * Get Push Notification subscription based on user ID and subscription + */ + public function getIdByEndpoint(string $endpoint): ?int + { + return $this->database->table(Subscriber::TABLE) + ->where("subscription LIKE ?", "%$endpoint%")->fetch()?->id; + } + /** * Get Subscribers by userIds * @@ -64,7 +75,7 @@ protected function getClassName(): string } /** - * @return \Tymy\Module\Core\Model\Field[] + * @return Field[] */ protected function getScheme(): array { @@ -140,7 +151,7 @@ private function webPushBulk(PushNotification $notification, array $subscribers) } foreach ($this->webPush->flush() as $report) { - $this->processReport($subscriber, $report); + $this->processReport($report); } } catch (ErrorException $e) { Debugger::log('WebPush ErrorException: ' . $e->getMessage(), ILogger::EXCEPTION); @@ -265,10 +276,13 @@ public function notifyEveryone(PushNotification $notification): void * Deletes subscriber from database if its already expired. * May contain another post-processing tasks */ - private function processReport(Subscriber $subscriber, MessageSentReport $report): void + private function processReport(MessageSentReport $report): void { - if (!$report->isSuccess() && $report->isSubscriptionExpired()) { - $this->delete($subscriber->getId()); //sending to void subscription - delete it from DB to avoid ghosts + $endpointUrl = new Url($report->getEndpoint()); + $subscriberId = $this->getIdByEndpoint(str_replace("/fcm/send/", "", $endpointUrl->getPath())); + + if ($subscriberId && !$report->isSuccess() && $report->isSubscriptionExpired()) { + $this->delete($subscriberId, $report->getId()); //sending to void subscription - delete it from DB to avoid ghosts } } }