Skip to content

Commit 9f978be

Browse files
Merge pull request #506 from nextcloud/backport/505/stable17
[stable17] Do not send push notifications when nothing was deleted
2 parents 7b909fd + a72fb72 commit 9f978be

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/Controller/EndpointController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ public function deleteNotification(int $id): DataResponse {
164164
return new DataResponse(null, Http::STATUS_NOT_FOUND);
165165
}
166166

167-
$this->handler->deleteById($id, $this->getCurrentUser());
168-
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id);
167+
$deleted = $this->handler->deleteById($id, $this->getCurrentUser());
168+
if ($deleted) {
169+
$this->push->pushDeleteToDevice($this->getCurrentUser(), $id);
170+
}
169171
return new DataResponse();
170172
}
171173

@@ -175,8 +177,10 @@ public function deleteNotification(int $id): DataResponse {
175177
* @return DataResponse
176178
*/
177179
public function deleteAllNotifications(): DataResponse {
178-
$this->handler->deleteByUser($this->getCurrentUser());
179-
$this->push->pushDeleteToDevice($this->getCurrentUser(), 0);
180+
$deletedSomething = $this->handler->deleteByUser($this->getCurrentUser());
181+
if ($deletedSomething) {
182+
$this->push->pushDeleteToDevice($this->getCurrentUser(), 0);
183+
}
180184
return new DataResponse();
181185
}
182186

lib/Handler.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,31 @@ public function delete(INotification $notification): array {
117117
* Delete the notification of a given user
118118
*
119119
* @param string $user
120+
* @return bool
120121
*/
121-
public function deleteByUser(string $user) {
122+
public function deleteByUser(string $user): bool {
122123
$notification = $this->manager->createNotification();
123124
try {
124125
$notification->setUser($user);
125126
} catch (\InvalidArgumentException $e) {
126-
return;
127+
return false;
127128
}
128-
$this->delete($notification);
129+
return !empty($this->delete($notification));
129130
}
130131

131132
/**
132133
* Delete the notification matching the given id
133134
*
134135
* @param int $id
135136
* @param string $user
137+
* @return bool
136138
*/
137-
public function deleteById(int $id, string $user) {
139+
public function deleteById(int $id, string $user): bool {
138140
$sql = $this->connection->getQueryBuilder();
139141
$sql->delete('notifications')
140142
->where($sql->expr()->eq('notification_id', $sql->createNamedParameter($id)))
141143
->andWhere($sql->expr()->eq('user', $sql->createNamedParameter($user)));
142-
$sql->execute();
144+
return (bool) $sql->execute();
143145
}
144146

145147
/**

0 commit comments

Comments
 (0)