AvailabilityCoordinator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Richard Steinmetz <richard@steinmetz.cloud>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. namespace OC\User;
  25. use JsonException;
  26. use OCA\DAV\AppInfo\Application;
  27. use OCA\DAV\CalDAV\TimezoneService;
  28. use OCA\DAV\Service\AbsenceService;
  29. use OCP\ICache;
  30. use OCP\ICacheFactory;
  31. use OCP\IConfig;
  32. use OCP\IUser;
  33. use OCP\User\IAvailabilityCoordinator;
  34. use OCP\User\IOutOfOfficeData;
  35. use Psr\Log\LoggerInterface;
  36. class AvailabilityCoordinator implements IAvailabilityCoordinator {
  37. private ICache $cache;
  38. public function __construct(
  39. ICacheFactory $cacheFactory,
  40. private IConfig $config,
  41. private AbsenceService $absenceService,
  42. private LoggerInterface $logger,
  43. private TimezoneService $timezoneService,
  44. ) {
  45. $this->cache = $cacheFactory->createLocal('OutOfOfficeData');
  46. }
  47. public function isEnabled(): bool {
  48. return $this->config->getAppValue(Application::APP_ID, 'hide_absence_settings', 'no') === 'no';
  49. }
  50. private function getCachedOutOfOfficeData(IUser $user): ?OutOfOfficeData {
  51. $cachedString = $this->cache->get($user->getUID());
  52. if ($cachedString === null) {
  53. return null;
  54. }
  55. try {
  56. $cachedData = json_decode($cachedString, true, 10, JSON_THROW_ON_ERROR);
  57. } catch (JsonException $e) {
  58. $this->logger->error('Failed to deserialize cached out-of-office data: ' . $e->getMessage(), [
  59. 'exception' => $e,
  60. 'json' => $cachedString,
  61. ]);
  62. return null;
  63. }
  64. return new OutOfOfficeData(
  65. $cachedData['id'],
  66. $user,
  67. $cachedData['startDate'],
  68. $cachedData['endDate'],
  69. $cachedData['shortMessage'],
  70. $cachedData['message'],
  71. );
  72. }
  73. private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
  74. try {
  75. $cachedString = json_encode([
  76. 'id' => $data->getId(),
  77. 'startDate' => $data->getStartDate(),
  78. 'endDate' => $data->getEndDate(),
  79. 'shortMessage' => $data->getShortMessage(),
  80. 'message' => $data->getMessage(),
  81. ], JSON_THROW_ON_ERROR);
  82. } catch (JsonException $e) {
  83. $this->logger->error('Failed to serialize out-of-office data: ' . $e->getMessage(), [
  84. 'exception' => $e,
  85. ]);
  86. return;
  87. }
  88. $this->cache->set($data->getUser()->getUID(), $cachedString, 300);
  89. }
  90. public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
  91. $timezone = $this->getCachedTimezone($user->getUID());
  92. if ($timezone === null) {
  93. $timezone = $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone();
  94. $this->setCachedTimezone($user->getUID(), $timezone);
  95. }
  96. $data = $this->getCachedOutOfOfficeData($user);
  97. if ($data === null) {
  98. $absenceData = $this->absenceService->getAbsence($user->getUID());
  99. if ($absenceData === null) {
  100. return null;
  101. }
  102. $data = $absenceData->toOutOufOfficeData($user, $timezone);
  103. }
  104. $this->setCachedOutOfOfficeData($data);
  105. return $data;
  106. }
  107. private function getCachedTimezone(string $userId): ?string {
  108. return $this->cache->get($userId . '_timezone') ?? null;
  109. }
  110. private function setCachedTimezone(string $userId, string $timezone): void {
  111. $this->cache->set($userId . '_timezone', $timezone, 3600);
  112. }
  113. public function clearCache(string $userId): void {
  114. $this->cache->set($userId, null, 300);
  115. $this->cache->set($userId . '_timezone', null, 3600);
  116. }
  117. public function isInEffect(IOutOfOfficeData $data): bool {
  118. return $this->absenceService->isInEffect($data);
  119. }
  120. }