Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable23] Improve mass notification processing #1156

Merged
merged 2 commits into from
Feb 28, 2022
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
3 changes: 1 addition & 2 deletions lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public function notify(INotification $notification): void {
$notificationId = $this->handler->add($notification);

try {
$notificationToPush = $this->handler->getById($notificationId, $notification->getUser());
$this->push->pushToDevice($notificationId, $notificationToPush);
$this->push->pushToDevice($notificationId, $notification);
} catch (NotificationNotFoundException $e) {
throw new \InvalidArgumentException('Error while preparing push notification');
}
Expand Down
34 changes: 31 additions & 3 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,27 @@ public function delete(INotification $notification): array {
}
$statement->closeCursor();

foreach ($deleted as $user => $entries) {
foreach ($entries as $entry) {
$this->deleteById($entry['id'], (string) $user, $notifications[$entry['id']]);
$this->connection->beginTransaction();
try {
$shouldFlush = $this->manager->defer();

foreach ($notifications as $n) {
$this->manager->dismissNotification($n);
}

$notificationIds = array_keys($notifications);
foreach (array_chunk($notificationIds, 1000) as $chunk) {
$this->deleteIds($chunk);
}

if ($shouldFlush) {
$this->manager->flush();
}
} catch (\Throwable $e) {
$this->connection->rollBack();
throw $e;
}
$this->connection->commit();

return $deleted;
}
Expand Down Expand Up @@ -156,6 +172,18 @@ public function deleteById(int $id, string $user, ?INotification $notification =
return (bool) $sql->executeStatement();
}

/**
* Delete the notification matching the given ids
*
* @param int[] $ids
*/
public function deleteIds(array $ids): void {
$sql = $this->connection->getQueryBuilder();
$sql->delete('notifications')
->where($sql->expr()->in('notification_id', $sql->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
$sql->executeStatement();
}

/**
* Get the notification matching the given id
*
Expand Down
15 changes: 3 additions & 12 deletions tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,21 @@ protected function setUp(): void {

public function dataNotify() {
return [
[23, 'user1'],
[42, 'user2'],
[23],
[42],
];
}

/**
* @dataProvider dataNotify
*
* @param int $id
* @param string $user
*/
public function testNotify($id, $user) {
$this->notification->expects($this->once())
->method('getUser')
->willReturn($user);

public function testNotify($id) {
$this->handler->expects($this->once())
->method('add')
->with($this->notification)
->willReturn($id);
$this->handler->expects($this->once())
->method('getById')
->with($id, $user)
->willReturn($this->notification);
$this->push->expects($this->once())
->method('pushToDevice')
->with($id, $this->notification);
Expand Down