1
0

AvailabilityCoordinator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $cachedData['replacementUserId'],
  55. $cachedData['replacementUserDisplayName'],
  56. );
  57. }
  58. private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
  59. try {
  60. $cachedString = json_encode([
  61. 'id' => $data->getId(),
  62. 'startDate' => $data->getStartDate(),
  63. 'endDate' => $data->getEndDate(),
  64. 'shortMessage' => $data->getShortMessage(),
  65. 'message' => $data->getMessage(),
  66. 'replacementUserId' => $data->getReplacementUserId(),
  67. 'replacementUserDisplayName' => $data->getReplacementUserDisplayName(),
  68. ], JSON_THROW_ON_ERROR);
  69. } catch (JsonException $e) {
  70. $this->logger->error('Failed to serialize out-of-office data: ' . $e->getMessage(), [
  71. 'exception' => $e,
  72. ]);
  73. return;
  74. }
  75. $this->cache->set($data->getUser()->getUID(), $cachedString, 300);
  76. }
  77. public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
  78. $timezone = $this->getCachedTimezone($user->getUID());
  79. if ($timezone === null) {
  80. $timezone = $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone();
  81. $this->setCachedTimezone($user->getUID(), $timezone);
  82. }
  83. $data = $this->getCachedOutOfOfficeData($user);
  84. if ($data === null) {
  85. $absenceData = $this->absenceService->getAbsence($user->getUID());
  86. if ($absenceData === null) {
  87. return null;
  88. }
  89. $data = $absenceData->toOutOufOfficeData($user, $timezone);
  90. }
  91. $this->setCachedOutOfOfficeData($data);
  92. return $data;
  93. }
  94. private function getCachedTimezone(string $userId): ?string {
  95. return $this->cache->get($userId . '_timezone') ?? null;
  96. }
  97. private function setCachedTimezone(string $userId, string $timezone): void {
  98. $this->cache->set($userId . '_timezone', $timezone, 3600);
  99. }
  100. public function clearCache(string $userId): void {
  101. $this->cache->set($userId, null, 300);
  102. $this->cache->set($userId . '_timezone', null, 3600);
  103. }
  104. public function isInEffect(IOutOfOfficeData $data): bool {
  105. return $this->absenceService->isInEffect($data);
  106. }
  107. }