diff --git a/lib/Controller/EndpointController.php b/lib/Controller/EndpointController.php index 6a2402930..6734509b3 100644 --- a/lib/Controller/EndpointController.php +++ b/lib/Controller/EndpointController.php @@ -30,6 +30,7 @@ use OCP\Notification\IManager; use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; +use OCP\Notification\NotificationPreloadReason; use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IUserStatus; @@ -97,6 +98,8 @@ public function listNotifications(string $apiVersion): DataResponse { ); } + $this->manager->preloadDataForParsing($notifications, $language, NotificationPreloadReason::EndpointController); + $data = []; $notificationIds = []; foreach ($notifications as $notificationId => $notification) { @@ -155,6 +158,8 @@ public function getNotification(string $apiVersion, int $id): DataResponse { $user = $this->session->getUser(); $language = $this->l10nFactory->getUserLanguage($user); + $this->manager->preloadDataForParsing([$notification], $language, NotificationPreloadReason::EndpointController); + try { $notification = $this->manager->prepare($notification, $language); } catch (AlreadyProcessedException|IncompleteParsedNotificationException|\InvalidArgumentException) { diff --git a/lib/MailNotifications.php b/lib/MailNotifications.php index 6bfc75ab4..101fdff19 100644 --- a/lib/MailNotifications.php +++ b/lib/MailNotifications.php @@ -29,6 +29,7 @@ use OCP\Notification\IManager; use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; +use OCP\Notification\NotificationPreloadReason; use OCP\Util; use Psr\Log\LoggerInterface; @@ -142,6 +143,8 @@ protected function sendEmailToUser(Settings $settings, array $notifications, str $lastSendId = array_key_first($notifications); $lastSendTime = $this->timeFactory->getTime(); + $this->manager->preloadDataForParsing($notifications, $language, NotificationPreloadReason::Email); + $preparedNotifications = []; foreach ($notifications as $notification) { /** @var INotification $preparedNotification */ diff --git a/lib/Push.php b/lib/Push.php index 0acff4003..4c6825080 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -37,6 +37,7 @@ use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; +use OCP\Notification\NotificationPreloadReason; use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IUserStatus; @@ -361,8 +362,10 @@ public function pushToDevice(int $id, INotification $notification): void { $language = $this->l10nFactory->getUserLanguage($user); $this->printInfo('Language is set to ' . $language); + $this->notificationManager->setPreparingPushNotification(true); + $this->notificationManager->preloadDataForParsing([$notification], $language, NotificationPreloadReason::Push); + try { - $this->notificationManager->setPreparingPushNotification(true); $notification = $this->notificationManager->prepare($notification, $language); } catch (AlreadyProcessedException|IncompleteParsedNotificationException|\InvalidArgumentException $e) { // FIXME remove \InvalidArgumentException in Nextcloud 39 diff --git a/tests/Unit/Controller/EndpointControllerTest.php b/tests/Unit/Controller/EndpointControllerTest.php index fb1497f61..def6ab94c 100644 --- a/tests/Unit/Controller/EndpointControllerTest.php +++ b/tests/Unit/Controller/EndpointControllerTest.php @@ -149,6 +149,9 @@ public function testListNotifications(string $apiVersion, array $notifications, $this->manager->expects($this->once()) ->method('createNotification') ->willReturn($filter); + $this->manager->expects(self::once()) + ->method('preloadDataForParsing') + ->with($notifications, 'en'); $this->manager->expects($this->exactly(\count($notifications))) ->method('prepare') ->willReturnArgument(0); @@ -216,6 +219,10 @@ public function testListNotificationsThrows(string $apiVersion, array $notificat $this->manager->expects($this->once()) ->method('flush'); + $this->manager->expects(self::once()) + ->method('preloadDataForParsing') + ->with($notifications, 'en'); + $throw = true; $this->manager->expects($this->exactly(2)) ->method('prepare') @@ -285,6 +292,9 @@ public function testGetNotification(string $apiVersion, int $id, string $usernam $this->manager->expects($this->once()) ->method('hasNotifiers') ->willReturn(true); + $this->manager->expects(self::once()) + ->method('preloadDataForParsing') + ->with([$notification], 'en'); $this->manager->expects($this->once()) ->method('prepare') ->with($notification) @@ -335,6 +345,10 @@ public function testGetNotificationNoId(string $apiVersion, bool $hasNotifiers, ->method('getById') ->willThrowException($notification); + $this->manager->expects(self::never()) + ->method('preloadDataForParsing') + ->with([$notification], 'en'); + $this->manager->expects($called && !$notification instanceof NotificationNotFoundException ? $this->once() : $this->never()) ->method('prepare') ->willThrowException(new \InvalidArgumentException()); @@ -348,6 +362,10 @@ public function testGetNotificationNoId(string $apiVersion, bool $hasNotifiers, ->with($this->user) ->willReturn('en'); + $this->manager->expects(self::once()) + ->method('preloadDataForParsing') + ->with([$notification], 'en'); + $this->manager->expects($this->once()) ->method('prepare') ->willThrowException(new \InvalidArgumentException());