AvailabilityCoordinator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\User;
  8. use JsonException;
  9. use OCA\DAV\AppInfo\Application;
  10. use OCA\DAV\CalDAV\TimezoneService;
  11. use OCA\DAV\Service\AbsenceService;
  12. use OCP\ICache;
  13. use OCP\ICacheFactory;
  14. use OCP\IConfig;
  15. use OCP\IUser;
  16. use OCP\User\IAvailabilityCoordinator;
  17. use OCP\User\IOutOfOfficeData;
  18. use Psr\Log\LoggerInterface;
  19. class AvailabilityCoordinator implements IAvailabilityCoordinator {
  20. private ICache $cache;
  21. public function __construct(
  22. ICacheFactory $cacheFactory,
  23. private IConfig $config,
  24. private AbsenceService $absenceService,
  25. private LoggerInterface $logger,
  26. private TimezoneService $timezoneService,
  27. ) {
  28. $this->cache = $cacheFactory->createLocal('OutOfOfficeData');
  29. }
  30. public function isEnabled(): bool {
  31. return $this->config->getAppValue(Application::APP_ID, 'hide_absence_settings', 'no') === 'no';
  32. }
  33. private function getCachedOutOfOfficeData(IUser $user): ?OutOfOfficeData {
  34. $cachedString = $this->cache->get($user->getUID());
  35. if ($cachedString === null) {
  36. return null;
  37. }
  38. try {
  39. $cachedData = json_decode($cachedString, true, 10, JSON_THROW_ON_ERROR);
  40. } catch (JsonException $e) {
  41. $this->logger->error('Failed to deserialize cached out-of-office data: ' . $e->getMessage(), [
  42. 'exception' => $e,
  43. 'json' => $cachedString,
  44. ]);
  45. return null;
  46. }
  47. return new OutOfOfficeData(
  48. $cachedData['id'],
  49. $user,
  50. $cachedData['startDate'],
  51. $cachedData['endDate'],
  52. $cachedData['shortMessage'],
  53. $cachedData['message'],
  54. );
  55. }
  56. private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
  57. try {
  58. $cachedString = json_encode([
  59. 'id' => $data->getId(),
  60. 'startDate' => $data->getStartDate(),
  61. 'endDate' => $data->getEndDate(),
  62. 'shortMessage' => $data->getShortMessage(),
  63. 'message' => $data->getMessage(),
  64. ], JSON_THROW_ON_ERROR);
  65. } catch (JsonException $e) {
  66. $this->logger->error('Failed to serialize out-of-office data: ' . $e->getMessage(), [
  67. 'exception' => $e,
  68. ]);
  69. return;
  70. }
  71. $this->cache->set($data->getUser()->getUID(), $cachedString, 300);
  72. }
  73. public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
  74. $timezone = $this->getCachedTimezone($user->getUID());
  75. if ($timezone === null) {
  76. $timezone = $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone();
  77. $this->setCachedTimezone($user->getUID(), $timezone);
  78. }
  79. $data = $this->getCachedOutOfOfficeData($user);
  80. if ($data === null) {
  81. $absenceData = $this->absenceService->getAbsence($user->getUID());
  82. if ($absenceData === null) {
  83. return null;
  84. }
  85. $data = $absenceData->toOutOufOfficeData($user, $timezone);
  86. }
  87. $this->setCachedOutOfOfficeData($data);
  88. return $data;
  89. }
  90. private function getCachedTimezone(string $userId): ?string {
  91. return $this->cache->get($userId . '_timezone') ?? null;
  92. }
  93. private function setCachedTimezone(string $userId, string $timezone): void {
  94. $this->cache->set($userId . '_timezone', $timezone, 3600);
  95. }
  96. public function clearCache(string $userId): void {
  97. $this->cache->set($userId, null, 300);
  98. $this->cache->set($userId . '_timezone', null, 3600);
  99. }
  100. public function isInEffect(IOutOfOfficeData $data): bool {
  101. return $this->absenceService->isInEffect($data);
  102. }
  103. }