From 0edace16cc97771864ca691e3d12450952e0096f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 24 Feb 2022 11:35:39 +0100 Subject: [PATCH 1/2] Handle notification deletion less unique Signed-off-by: Joas Schilling --- lib/Handler.php | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/Handler.php b/lib/Handler.php index ca7c18ef8..4d1690892 100644 --- a/lib/Handler.php +++ b/lib/Handler.php @@ -108,11 +108,27 @@ public function delete(INotification $notification): array { } $statement->closeCursor(); - foreach ($deleted as $user => $entries) { - foreach ($entries as $entry) { - $this->deleteById($entry['id'], (string) $user, $notifications[$entry['id']]); + $this->connection->beginTransaction(); + try { + $shouldFlush = $this->manager->defer(); + + foreach ($notifications as $n) { + $this->manager->dismissNotification($n); + } + + $notificationIds = array_keys($notifications); + foreach (array_chunk($notificationIds, 1000) as $chunk) { + $this->deleteIds($chunk); } + + if ($shouldFlush) { + $this->manager->flush(); + } + } catch (\Throwable $e) { + $this->connection->rollBack(); + throw $e; } + $this->connection->commit(); return $deleted; } @@ -156,6 +172,18 @@ public function deleteById(int $id, string $user, ?INotification $notification = return (bool) $sql->executeStatement(); } + /** + * Delete the notification matching the given ids + * + * @param int[] $ids + */ + public function deleteIds(array $ids): void { + $sql = $this->connection->getQueryBuilder(); + $sql->delete('notifications') + ->where($sql->expr()->in('notification_id', $sql->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); + $sql->executeStatement(); + } + /** * Get the notification matching the given id * From 5846d977a3cd438a47978ed8de6de41f5c91452f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 24 Feb 2022 11:39:13 +0100 Subject: [PATCH 2/2] Do not read the notification we just inserted Signed-off-by: Joas Schilling --- lib/App.php | 3 +-- tests/Unit/AppTest.php | 15 +++------------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/App.php b/lib/App.php index c14a6b5c7..6e5017bc1 100644 --- a/lib/App.php +++ b/lib/App.php @@ -54,8 +54,7 @@ public function notify(INotification $notification): void { $notificationId = $this->handler->add($notification); try { - $notificationToPush = $this->handler->getById($notificationId, $notification->getUser()); - $this->push->pushToDevice($notificationId, $notificationToPush); + $this->push->pushToDevice($notificationId, $notification); } catch (NotificationNotFoundException $e) { throw new \InvalidArgumentException('Error while preparing push notification'); } diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php index 4b97dd6c4..26d540d41 100644 --- a/tests/Unit/AppTest.php +++ b/tests/Unit/AppTest.php @@ -53,8 +53,8 @@ protected function setUp(): void { public function dataNotify() { return [ - [23, 'user1'], - [42, 'user2'], + [23], + [42], ]; } @@ -62,21 +62,12 @@ public function dataNotify() { * @dataProvider dataNotify * * @param int $id - * @param string $user */ - public function testNotify($id, $user) { - $this->notification->expects($this->once()) - ->method('getUser') - ->willReturn($user); - + public function testNotify($id) { $this->handler->expects($this->once()) ->method('add') ->with($this->notification) ->willReturn($id); - $this->handler->expects($this->once()) - ->method('getById') - ->with($id, $user) - ->willReturn($this->notification); $this->push->expects($this->once()) ->method('pushToDevice') ->with($id, $this->notification);