|
24 | 24 | use OCP\EventDispatcher\Event; |
25 | 25 | use OCP\EventDispatcher\IEventDispatcher; |
26 | 26 | use OCP\EventDispatcher\IEventListener; |
| 27 | +use OCP\IConfig; |
27 | 28 | use OCP\IDBConnection; |
28 | 29 | use OCP\IUser; |
29 | 30 | use OCP\IUserSession; |
30 | 31 | use OCP\Notification\IManager; |
| 32 | +use OCP\Notification\INotification; |
31 | 33 | use Psr\Log\LoggerInterface; |
32 | 34 |
|
33 | 35 | /** |
|
36 | 38 | class Listener implements IEventListener { |
37 | 39 |
|
38 | 40 | protected bool $shouldSendCallNotification = false; |
| 41 | + /** @var array<string, INotification> $preparedCallNotifications Map of language => parsed notification in that language */ |
| 42 | + protected array $preparedCallNotifications = []; |
39 | 43 |
|
40 | 44 | public function __construct( |
| 45 | + protected IConfig $serverConfig, |
41 | 46 | protected IDBConnection $connection, |
42 | 47 | protected IManager $notificationManager, |
| 48 | + protected Notifier $notificationProvider, |
43 | 49 | protected ParticipantService $participantsService, |
44 | 50 | protected IEventDispatcher $dispatcher, |
45 | 51 | protected IUserSession $userSession, |
@@ -273,17 +279,50 @@ protected function sendCallNotifications(Room $room): void { |
273 | 279 | return; |
274 | 280 | } |
275 | 281 |
|
| 282 | + $this->preparedCallNotifications = []; |
276 | 283 | $userIds = $this->participantsService->getParticipantUserIdsForCallNotifications($room); |
| 284 | + // Room name depends on the notification user for one-to-one, |
| 285 | + // so we avoid pre-parsing it there. Also, it comes with some base load, |
| 286 | + // so we only do it for "big enough" calls. |
| 287 | + $preparseNotificationForPush = count($userIds) > 10; |
| 288 | + if ($preparseNotificationForPush) { |
| 289 | + $fallbackLang = $this->serverConfig->getSystemValue('force_language', null); |
| 290 | + if (is_string($fallbackLang)) { |
| 291 | + /** @psalm-var array<string, string> $userLanguages */ |
| 292 | + $userLanguages = []; |
| 293 | + } else { |
| 294 | + $fallbackLang = $this->serverConfig->getSystemValueString('default_language', 'en'); |
| 295 | + /** @psalm-var array<string, string> $userLanguages */ |
| 296 | + $userLanguages = $this->serverConfig->getUserValueForUsers('core', 'lang', $userIds); |
| 297 | + } |
| 298 | + } |
| 299 | + |
277 | 300 | $this->connection->beginTransaction(); |
278 | 301 | try { |
279 | 302 | foreach ($userIds as $userId) { |
280 | 303 | if ($actorId === $userId) { |
281 | 304 | continue; |
282 | 305 | } |
283 | 306 |
|
| 307 | + if ($preparseNotificationForPush) { |
| 308 | + // Get the settings for this particular user, then check if we have notifications to email them |
| 309 | + $languageCode = $userLanguages[$userId] ?? $fallbackLang; |
| 310 | + |
| 311 | + if (!isset($this->preparedCallNotifications[$languageCode])) { |
| 312 | + $translatedNotification = clone $notification; |
| 313 | + |
| 314 | + $this->notificationManager->setPreparingPushNotification(true); |
| 315 | + $this->preparedCallNotifications[$languageCode] = $this->notificationProvider->prepare($translatedNotification, $languageCode); |
| 316 | + $this->notificationManager->setPreparingPushNotification(false); |
| 317 | + } |
| 318 | + $userNotification = $this->preparedCallNotifications[$languageCode]; |
| 319 | + } else { |
| 320 | + $userNotification = $notification; |
| 321 | + } |
| 322 | + |
284 | 323 | try { |
285 | | - $notification->setUser($userId); |
286 | | - $this->notificationManager->notify($notification); |
| 324 | + $userNotification->setUser($userId); |
| 325 | + $this->notificationManager->notify($userNotification); |
287 | 326 | } catch (\InvalidArgumentException $e) { |
288 | 327 | $this->logger->error($e->getMessage(), ['exception' => $e]); |
289 | 328 | } |
|
0 commit comments