Skip to content

Commit 4719f50

Browse files
Merge pull request #1154 from nextcloud/techdebt/noid/improve-mass-notification-processing
Improve mass notification processing
2 parents a2f2832 + 240a5cd commit 4719f50

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

lib/App.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public function notify(INotification $notification): void {
5454
$notificationId = $this->handler->add($notification);
5555

5656
try {
57-
$notificationToPush = $this->handler->getById($notificationId, $notification->getUser());
58-
$this->push->pushToDevice($notificationId, $notificationToPush);
57+
$this->push->pushToDevice($notificationId, $notification);
5958
} catch (NotificationNotFoundException $e) {
6059
throw new \InvalidArgumentException('Error while preparing push notification');
6160
}

lib/Handler.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,27 @@ public function delete(INotification $notification): array {
108108
}
109109
$statement->closeCursor();
110110

111-
foreach ($deleted as $user => $entries) {
112-
foreach ($entries as $entry) {
113-
$this->deleteById($entry['id'], (string) $user, $notifications[$entry['id']]);
111+
$this->connection->beginTransaction();
112+
try {
113+
$shouldFlush = $this->manager->defer();
114+
115+
foreach ($notifications as $n) {
116+
$this->manager->dismissNotification($n);
117+
}
118+
119+
$notificationIds = array_keys($notifications);
120+
foreach (array_chunk($notificationIds, 1000) as $chunk) {
121+
$this->deleteIds($chunk);
114122
}
123+
124+
if ($shouldFlush) {
125+
$this->manager->flush();
126+
}
127+
} catch (\Throwable $e) {
128+
$this->connection->rollBack();
129+
throw $e;
115130
}
131+
$this->connection->commit();
116132

117133
return $deleted;
118134
}
@@ -156,6 +172,18 @@ public function deleteById(int $id, string $user, ?INotification $notification =
156172
return (bool) $sql->executeStatement();
157173
}
158174

175+
/**
176+
* Delete the notification matching the given ids
177+
*
178+
* @param int[] $ids
179+
*/
180+
public function deleteIds(array $ids): void {
181+
$sql = $this->connection->getQueryBuilder();
182+
$sql->delete('notifications')
183+
->where($sql->expr()->in('notification_id', $sql->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
184+
$sql->executeStatement();
185+
}
186+
159187
/**
160188
* Get the notification matching the given id
161189
*

tests/Unit/AppTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,21 @@ protected function setUp(): void {
5353

5454
public function dataNotify() {
5555
return [
56-
[23, 'user1'],
57-
[42, 'user2'],
56+
[23],
57+
[42],
5858
];
5959
}
6060

6161
/**
6262
* @dataProvider dataNotify
6363
*
6464
* @param int $id
65-
* @param string $user
6665
*/
67-
public function testNotify($id, $user) {
68-
$this->notification->expects($this->once())
69-
->method('getUser')
70-
->willReturn($user);
71-
66+
public function testNotify($id) {
7267
$this->handler->expects($this->once())
7368
->method('add')
7469
->with($this->notification)
7570
->willReturn($id);
76-
$this->handler->expects($this->once())
77-
->method('getById')
78-
->with($id, $user)
79-
->willReturn($this->notification);
8071
$this->push->expects($this->once())
8172
->method('pushToDevice')
8273
->with($id, $this->notification);

0 commit comments

Comments
 (0)