From a10ca51583b874ac35ed78699463348d7218d628 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 9 Dec 2019 13:41:22 +0100 Subject: Dismiss notifications properly Signed-off-by: Roeland Jago Douma --- lib/Handler.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Handler.php b/lib/Handler.php index 864f435..3eb7168 100644 --- a/lib/Handler.php +++ b/lib/Handler.php @@ -137,6 +137,9 @@ class Handler { * @return bool */ public function deleteById(int $id, string $user): bool { + $notifcation = $this->getById($id, $user); + $this->manager->dismissNotification($notifcation); + $sql = $this->connection->getQueryBuilder(); $sql->delete('notifications') ->where($sql->expr()->eq('notification_id', $sql->createNamedParameter($id))) -- cgit v1.2.3 From 3a97b78a280c47719d6b2529e3d9194f3cff30b8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 10 Dec 2019 09:24:08 +0100 Subject: Use known INotification instances when possible Signed-off-by: Joas Schilling --- lib/Handler.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Handler.php b/lib/Handler.php index 3eb7168..a40496e 100644 --- a/lib/Handler.php +++ b/lib/Handler.php @@ -88,25 +88,27 @@ class Handler { */ public function delete(INotification $notification): array { $sql = $this->connection->getQueryBuilder(); - $sql->select('notification_id', 'user') + $sql->select('*') ->from('notifications'); $this->sqlWhere($sql, $notification); $statement = $sql->execute(); $deleted = []; + $notifications = []; while ($row = $statement->fetch()) { if (!isset($deleted[$row['user']])) { $deleted[$row['user']] = []; } $deleted[$row['user']][] = (int) $row['notification_id']; + $notifications[(int) $row['notification_id']] = $this->notificationFromRow($row); } $statement->closeCursor(); - foreach ($deleted as $user => $notifications) { - foreach ($notifications as $notificationId) { - $this->deleteById($notificationId, $user); + foreach ($deleted as $user => $notificationIds) { + foreach ($notificationIds as $notificationId) { + $this->deleteById($notificationId, $user, $notifications[$notificationId]); } } @@ -134,11 +136,15 @@ class Handler { * * @param int $id * @param string $user + * @param INotification|null $notification * @return bool */ - public function deleteById(int $id, string $user): bool { - $notifcation = $this->getById($id, $user); - $this->manager->dismissNotification($notifcation); + public function deleteById(int $id, string $user, ?INotification $notification = null): bool { + if (!$notification instanceof INotification) { + $notification = $this->getById($id, $user); + } + + $this->manager->dismissNotification($notification); $sql = $this->connection->getQueryBuilder(); $sql->delete('notifications') -- cgit v1.2.3 From 0a256573c0fe261ed25bb4a46787f55adcad3a46 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 10 Dec 2019 09:27:58 +0100 Subject: Make sure we dont fail with an exception Signed-off-by: Joas Schilling --- lib/Controller/EndpointController.php | 11 ++++++++--- lib/Handler.php | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Controller/EndpointController.php b/lib/Controller/EndpointController.php index 1cde7ef..167b5bb 100644 --- a/lib/Controller/EndpointController.php +++ b/lib/Controller/EndpointController.php @@ -164,10 +164,15 @@ class EndpointController extends OCSController { return new DataResponse(null, Http::STATUS_NOT_FOUND); } - $deleted = $this->handler->deleteById($id, $this->getCurrentUser()); - if ($deleted) { - $this->push->pushDeleteToDevice($this->getCurrentUser(), $id); + try { + $deleted = $this->handler->deleteById($id, $this->getCurrentUser()); + + if ($deleted) { + $this->push->pushDeleteToDevice($this->getCurrentUser(), $id); + } + } catch (NotificationNotFoundException $e) { } + return new DataResponse(); } diff --git a/lib/Handler.php b/lib/Handler.php index a40496e..f1f3db1 100644 --- a/lib/Handler.php +++ b/lib/Handler.php @@ -138,6 +138,7 @@ class Handler { * @param string $user * @param INotification|null $notification * @return bool + * @throws NotificationNotFoundException */ public function deleteById(int $id, string $user, ?INotification $notification = null): bool { if (!$notification instanceof INotification) { -- cgit v1.2.3 From 36b64ec1c5a94a0011c07a35fc8aa7269400269d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 10 Dec 2019 15:37:37 +0100 Subject: Fix tests Signed-off-by: Roeland Jago Douma --- tests/Unit/HandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/HandlerTest.php b/tests/Unit/HandlerTest.php index f56b2cf..0914b6a 100644 --- a/tests/Unit/HandlerTest.php +++ b/tests/Unit/HandlerTest.php @@ -158,7 +158,7 @@ class HandlerTest extends TestCase { } // Delete with wrong user - $this->handler->deleteById($notificationId, 'test_user2'); + $this->handler->deleteById($notificationId, 'test_user2', $notification); $this->assertSame(1, $this->handler->count($limitedNotification), 'Wrong notification count for user1 after trying to delete for user2'); // Get with correct user -- cgit v1.2.3