From 2fc9844ac7b2430095ee446a0aec62481258bec9 Mon Sep 17 00:00:00 2001 From: binsky Date: Fri, 5 Apr 2024 03:13:02 +0200 Subject: [PATCH] move already existing notification check from cron service into notification service --- lib/Service/CronService.php | 11 +---------- lib/Service/NotificationService.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/Service/CronService.php b/lib/Service/CronService.php index 058e961b..6459950b 100644 --- a/lib/Service/CronService.php +++ b/lib/Service/CronService.php @@ -26,8 +26,6 @@ namespace OCA\Passman\Service; use OCA\Passman\Activity; use OCA\Passman\Utility\Utils; use OCP\DB\Exception; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; use Psr\Log\LoggerInterface; class CronService { @@ -38,7 +36,6 @@ class CronService { private Utils $utils, private NotificationService $notificationService, private ActivityService $activityService, - private IDBConnection $db, ) { } @@ -46,16 +43,10 @@ class CronService { $expired_credentials = $this->credentialService->getExpiredCredentials($this->utils->getTime()); foreach ($expired_credentials as $credential) { $link = ''; // @TODO create direct link to credential - $qb = $this->db->getQueryBuilder(); - $qb->select('*') - ->from('notifications') - ->where($qb->expr()->eq('object_id', $qb->createNamedParameter($credential->getId(), IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq('subject', $qb->createNamedParameter('credential_expired', IQueryBuilder::PARAM_STR))); try { $this->logger->debug($credential->getLabel() . ' is expired, checking notifications!', ['app' => 'passman']); - $notificationCount = $qb->execute()->rowCount(); - if ($notificationCount === 0) { + if (!$this->notificationService->hasCredentialExpirationNotification($credential)) { $this->logger->debug($credential->getLabel() . ' is expired, adding notification!', ['app' => 'passman']); $this->activityService->add( Activity::SUBJECT_ITEM_EXPIRED, [$credential->getLabel(), $credential->getUserId()], diff --git a/lib/Service/NotificationService.php b/lib/Service/NotificationService.php index 718fd656..80d0f265 100644 --- a/lib/Service/NotificationService.php +++ b/lib/Service/NotificationService.php @@ -24,6 +24,8 @@ namespace OCA\Passman\Service; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use OCP\IURLGenerator; use OCP\Notification\IManager; @@ -31,6 +33,7 @@ class NotificationService { public function __construct( private IManager $manager, private IURLGenerator $urlGenerator, + private IDBConnection $db, private VaultService $vaultService, ) { } @@ -103,4 +106,12 @@ class NotificationService { $this->manager->notify($notification); } + function hasCredentialExpirationNotification($credential) { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from('notifications') + ->where($qb->expr()->eq('object_id', $qb->createNamedParameter($credential->getId(), IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->eq('subject', $qb->createNamedParameter('credential_expired', IQueryBuilder::PARAM_STR))); + return $qb->execute()->rowCount() !== 0; + } }