diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2020-06-08 15:54:21 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 15:54:21 +0300 |
commit | 0a31e0fbfafac67657001b998e8a2c510be1a617 (patch) | |
tree | 639f8b490157b1b2601913a795decbe4c1ca67ae /lib/Notification | |
parent | 6ef8e0b85767436ed99ed8976370ab9227755cf2 (diff) | |
parent | 839bb6104244575453a16087227d8c3af2a21acb (diff) |
Merge pull request #3745 from nextcloud/bugfix/noid/less-database-queries
Less database queries when parsing multiple notifications
Diffstat (limited to 'lib/Notification')
-rw-r--r-- | lib/Notification/Notifier.php | 88 |
1 files changed, 77 insertions, 11 deletions
diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index c336103e6..bdacd49cb 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -73,6 +73,11 @@ class Notifier implements INotifier { /** @var Definitions */ protected $definitions; + /** @var Room[] */ + protected $rooms = []; + /** @var Participant[][] */ + protected $participants = []; + public function __construct(IFactory $lFactory, IURLGenerator $url, Config $config, @@ -118,6 +123,65 @@ class Notifier implements INotifier { } /** + * @param string $objectId + * @return Room + * @throws RoomNotFoundException + */ + protected function getRoom(string $objectId): Room { + if (array_key_exists($objectId, $this->rooms)) { + if ($this->rooms[$objectId] === null) { + throw new RoomNotFoundException('Room does not exist'); + } + + return $this->rooms[$objectId]; + } + + try { + $room = $this->manager->getRoomByToken($objectId); + $this->rooms[$objectId] = $room; + return $room; + } catch (RoomNotFoundException $e) { + try { + // Before 3.2.3 the id was passed in notifications + $room = $this->manager->getRoomById((int) $objectId); + $this->rooms[$objectId] = $room; + return $room; + } catch (RoomNotFoundException $e) { + // Room does not exist + $this->rooms[$objectId] = null; + throw $e; + } + } + } + + /** + * @param Room $room + * @param string $userId + * @return Participant + * @throws ParticipantNotFoundException + */ + protected function getParticipant(Room $room, string $userId): Participant { + $roomId = $room->getId(); + if (array_key_exists($roomId, $this->participants) && array_key_exists($userId, $this->participants[$roomId])) { + if ($this->participants[$roomId][$userId] === null) { + throw new ParticipantNotFoundException('Participant does not exist'); + } + + return $this->participants[$roomId][$userId]; + } + + try { + $participant = $room->getParticipant($userId); + $this->participants[$roomId][$userId] = $participant; + return $participant; + } catch (ParticipantNotFoundException $e) { + // Participant does not exist + $this->participants[$roomId][$userId] = null; + throw $e; + } + } + + /** * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification @@ -138,24 +202,26 @@ class Notifier implements INotifier { $l = $this->lFactory->get('spreed', $languageCode); try { - $room = $this->manager->getRoomByToken($notification->getObjectId()); + $room = $this->getRoom($notification->getObjectId()); } catch (RoomNotFoundException $e) { + // Room does not exist + throw new AlreadyProcessedException(); + } + + if ($this->notificationManager->isPreparingPushNotification() && $notification->getSubject() === 'call') { + // Skip the participant check when we generate push notifications + // we just looped over the participants to create the notification, + // they can not be removed between these 2 steps, but we can save + // n queries. + } else { try { - // Before 3.2.3 the id was passed in notifications - $room = $this->manager->getRoomById((int) $notification->getObjectId()); - } catch (RoomNotFoundException $e) { + $participant = $this->getParticipant($room, $userId); + } catch (ParticipantNotFoundException $e) { // Room does not exist throw new AlreadyProcessedException(); } } - try { - $participant = $room->getParticipant($userId); - } catch (ParticipantNotFoundException $e) { - // Room does not exist - throw new AlreadyProcessedException(); - } - $notification ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('spreed', 'app-dark.svg'))) ->setLink($this->url->linkToRouteAbsolute('spreed.Page.showCall', ['token' => $room->getToken()])); |