Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,15 @@ public function deleteNotification(int $id): DataResponse {
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();
}

Expand Down
20 changes: 15 additions & 5 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,27 @@ public function count(INotification $notification): int {
*/
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]);
}
}

Expand Down Expand Up @@ -134,9 +136,17 @@ public function deleteByUser(string $user): bool {
*
* @param int $id
* @param string $user
* @param INotification|null $notification
* @return bool
* @throws NotificationNotFoundException
*/
public function deleteById(int $id, string $user): bool {
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')
->where($sql->expr()->eq('notification_id', $sql->createNamedParameter($id)))
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function testDeleteById() {
}

// 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
Expand Down