diff --git a/lib/Notification/Listener.php b/lib/Notification/Listener.php index b078879e90f..49ec2cbdb8e 100644 --- a/lib/Notification/Listener.php +++ b/lib/Notification/Listener.php @@ -45,6 +45,7 @@ use OCP\IUser; use OCP\IUserSession; use OCP\Notification\IManager; +use OCP\Notification\INotification; use Psr\Log\LoggerInterface; /** @@ -53,10 +54,13 @@ class Listener implements IEventListener { protected bool $shouldSendCallNotification = false; + /** @var array $preparedCallNotifications Map of language => parsed notification in that language */ + protected array $preparedCallNotifications = []; public function __construct( protected IDBConnection $connection, protected IManager $notificationManager, + protected Notifier $notificationProvider, protected ParticipantService $participantsService, protected IEventDispatcher $dispatcher, protected IUserSession $userSession, @@ -290,7 +294,24 @@ protected function sendCallNotifications(Room $room): void { return; } + $this->preparedCallNotifications = []; $userIds = $this->participantsService->getParticipantUserIdsForCallNotifications($room); + // Room name depends on the notification user for one-to-one, + // so we avoid preparsing it there. Also, it comes with some base load, + // so we only do it for "big enough" calls. + $preparseNotificationForPush = count($userIds) > 10; + if ($preparseNotificationForPush) { + $fallbackLang = $this->config->getSystemValue('force_language', null); + if (is_string($fallbackLang)) { + /** @psalm-var array $userLanguages */ + $userLanguages = []; + } else { + $fallbackLang = $this->config->getSystemValueString('default_language', 'en'); + /** @psalm-var array $userLanguages */ + $userLanguages = $this->config->getUserValueForUsers('core', 'lang', $userIds); + } + } + $this->connection->beginTransaction(); try { foreach ($userIds as $userId) { @@ -298,6 +319,22 @@ protected function sendCallNotifications(Room $room): void { continue; } + if ($preparseNotificationForPush) { + // Get the settings for this particular user, then check if we have notifications to email them + $languageCode = $userLanguages[$userId] ?? $fallbackLang; + + if (!isset($this->preparedCallNotifications[$languageCode])) { + $translatedNotification = clone $notification; + + $this->notificationManager->setPreparingPushNotification(true); + $this->preparedCallNotifications[$languageCode] = $this->notificationProvider->prepare($translatedNotification, $languageCode); + $this->notificationManager->setPreparingPushNotification(false); + $notification = $translatedNotification; + } else { + $notification = $this->preparedCallNotifications[$languageCode]; + } + } + try { $notification->setUser($userId); $this->notificationManager->notify($notification);