Skip to content

Commit

Permalink
feat: Autoload subscriberId
Browse files Browse the repository at this point in the history
  • Loading branch information
KminekMatej committed Oct 19, 2023
1 parent 8472d0d commit 13da4e1
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/module/push-notification/manager/PushNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -64,7 +75,7 @@ protected function getClassName(): string
}

/**
* @return \Tymy\Module\Core\Model\Field[]
* @return Field[]
*/
protected function getScheme(): array
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 13da4e1

Please sign in to comment.