From f994a182d37d163a9e2850c4ae399c1ac116ecbc Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Tue, 23 Jul 2024 02:05:38 +0200 Subject: [PATCH] add entity, mapper and service for subscriptions_webpush --- lib/Db/Subscription.php | 2 - lib/Db/WebPushSubscription.php | 23 ++++++++ lib/Db/WebPushSubscriptionMapper.php | 49 +++++++++++++++++ lib/Errors/WebPushSubscriptionNotFound.php | 6 ++ lib/Service/WebPushSubscriptionService.php | 64 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 lib/Db/WebPushSubscription.php create mode 100644 lib/Db/WebPushSubscriptionMapper.php create mode 100644 lib/Errors/WebPushSubscriptionNotFound.php create mode 100644 lib/Service/WebPushSubscriptionService.php diff --git a/lib/Db/Subscription.php b/lib/Db/Subscription.php index 7670c66..b136915 100644 --- a/lib/Db/Subscription.php +++ b/lib/Db/Subscription.php @@ -10,7 +10,6 @@ class Subscription extends Entity implements JsonSerializable { protected $userId; protected $collectionName; protected $transport; - protected $data; protected $creationTimestamp; protected $expirationTimestamp; @@ -25,7 +24,6 @@ class Subscription extends Entity implements JsonSerializable { 'userId' => $this->userId, 'collectionName' => $this->collectionName, 'transport' => $this->transport, - 'data' => $this->data, 'creationTimestamp' => $this->creationTimestamp, 'expirationTimestamp' => $this->expirationTimestamp ]; diff --git a/lib/Db/WebPushSubscription.php b/lib/Db/WebPushSubscription.php new file mode 100644 index 0000000..3a818a6 --- /dev/null +++ b/lib/Db/WebPushSubscription.php @@ -0,0 +1,23 @@ +addType('subscriptionId','integer'); + } + + public function jsonSerialize(): array { + return [ + 'subscriptionId' => $this->subscriptionId, + 'pushResource' => $this->pushResource, + ]; + } +} \ No newline at end of file diff --git a/lib/Db/WebPushSubscriptionMapper.php b/lib/Db/WebPushSubscriptionMapper.php new file mode 100644 index 0000000..be848ad --- /dev/null +++ b/lib/Db/WebPushSubscriptionMapper.php @@ -0,0 +1,49 @@ +db->getQueryBuilder(); + $qb->select('*') + ->from(self::TABLENAME) + ->where($qb->expr()->eq('subscription_id', $qb->createNamedParameter($subscriptionId, IQueryBuilder::PARAM_INT))); + + return $this->findEntity($qb); + } + + /** + * @param string $pushResource + * @return Entity|WebPushSubscription + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException + * @throws DoesNotExistException + */ + public function findByPushResource(string $pushResource): Subscription { + /* @var $qb IQueryBuilder */ + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from(self::TABLENAME) + ->where($qb->expr()->eq('push_resource', $qb->createNamedParameter($pushResource))); + + return $this->findEntity($qb); + } +} \ No newline at end of file diff --git a/lib/Errors/WebPushSubscriptionNotFound.php b/lib/Errors/WebPushSubscriptionNotFound.php new file mode 100644 index 0000000..2f7865d --- /dev/null +++ b/lib/Errors/WebPushSubscriptionNotFound.php @@ -0,0 +1,6 @@ +getMessage()); + } else { + throw $e; + } + } + + public function findBySubscriptionId(int $subscriptionId): ?WebPushSubscription { + try { + return $this->mapper->findBySubscriptionId($subscriptionId); + } catch (Exception $e) { + $this->handleException($e); + } + } + + public function findByPushResource(string $pushResource): ?WebPushSubscription { + try { + return $this->mapper->findByPushResource($pushResource); + } catch (Exception $e) { + $this->handleException($e); + } + } + + public function create(int $subscriptionId, string $pushResource): WebPushSubscription { + $webPushSubscription = new WebPushSubscription(); + $webPushSubscription->setSubscriptionId($subscriptionId); + $webPushSubscription->setPushResource($pushResource); + $webPushSubscription = $this->mapper->insert($webPushSubscription); + + return $webPushSubscription; + } + + public function deleteBySubscriptionId(int $subscriptionId): ?WebPushSubscription { + try { + $webPushSubscription = $this->mapper->findBySubscriptionId($subscriptionId); + $this->mapper->delete($webPushSubscription); + return $webPushSubscription; + } catch (Exception $e) { + $this->handleException($e); + } + } +} \ No newline at end of file