Skip to content

Commit

Permalink
Merge pull request #506 from nextcloud/backport/505/stable17
Browse files Browse the repository at this point in the history
[stable17] Do not send push notifications when nothing was deleted
  • Loading branch information
nickvergessen authored Dec 9, 2019
2 parents 7b909fd + a72fb72 commit 9f978be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ public function deleteNotification(int $id): DataResponse {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

$this->handler->deleteById($id, $this->getCurrentUser());
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id);
$deleted = $this->handler->deleteById($id, $this->getCurrentUser());
if ($deleted) {
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id);
}
return new DataResponse();
}

Expand All @@ -175,8 +177,10 @@ public function deleteNotification(int $id): DataResponse {
* @return DataResponse
*/
public function deleteAllNotifications(): DataResponse {
$this->handler->deleteByUser($this->getCurrentUser());
$this->push->pushDeleteToDevice($this->getCurrentUser(), 0);
$deletedSomething = $this->handler->deleteByUser($this->getCurrentUser());
if ($deletedSomething) {
$this->push->pushDeleteToDevice($this->getCurrentUser(), 0);
}
return new DataResponse();
}

Expand Down
12 changes: 7 additions & 5 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,31 @@ public function delete(INotification $notification): array {
* Delete the notification of a given user
*
* @param string $user
* @return bool
*/
public function deleteByUser(string $user) {
public function deleteByUser(string $user): bool {
$notification = $this->manager->createNotification();
try {
$notification->setUser($user);
} catch (\InvalidArgumentException $e) {
return;
return false;
}
$this->delete($notification);
return !empty($this->delete($notification));
}

/**
* Delete the notification matching the given id
*
* @param int $id
* @param string $user
* @return bool
*/
public function deleteById(int $id, string $user) {
public function deleteById(int $id, string $user): bool {
$sql = $this->connection->getQueryBuilder();
$sql->delete('notifications')
->where($sql->expr()->eq('notification_id', $sql->createNamedParameter($id)))
->andWhere($sql->expr()->eq('user', $sql->createNamedParameter($user)));
$sql->execute();
return (bool) $sql->execute();
}

/**
Expand Down

0 comments on commit 9f978be

Please sign in to comment.