AvailabilityCoordinator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Db\AbsenceMapper;
  29. use OCP\AppFramework\Db\DoesNotExistException;
  30. use OCP\ICache;
  31. use OCP\ICacheFactory;
  32. use OCP\IConfig;
  33. use OCP\IUser;
  34. use OCP\User\IAvailabilityCoordinator;
  35. use OCP\User\IOutOfOfficeData;
  36. use Psr\Log\LoggerInterface;
  37. class AvailabilityCoordinator implements IAvailabilityCoordinator {
  38. private ICache $cache;
  39. public function __construct(
  40. ICacheFactory $cacheFactory,
  41. private AbsenceMapper $absenceMapper,
  42. private IConfig $config,
  43. private LoggerInterface $logger,
  44. private TimezoneService $timezoneService,
  45. ) {
  46. $this->cache = $cacheFactory->createLocal('OutOfOfficeData');
  47. }
  48. public function isEnabled(): bool {
  49. return $this->config->getAppValue(
  50. Application::APP_ID,
  51. 'hide_absence_settings',
  52. 'no',
  53. ) === 'no';
  54. }
  55. private function getCachedOutOfOfficeData(IUser $user): ?OutOfOfficeData {
  56. $cachedString = $this->cache->get($user->getUID());
  57. if ($cachedString === null) {
  58. return null;
  59. }
  60. try {
  61. $cachedData = json_decode($cachedString, true, 10, JSON_THROW_ON_ERROR);
  62. } catch (JsonException $e) {
  63. $this->logger->error('Failed to deserialize cached out-of-office data: ' . $e->getMessage(), [
  64. 'exception' => $e,
  65. 'json' => $cachedString,
  66. ]);
  67. return null;
  68. }
  69. return new OutOfOfficeData(
  70. $cachedData['id'],
  71. $user,
  72. $cachedData['startDate'],
  73. $cachedData['endDate'],
  74. $cachedData['shortMessage'],
  75. $cachedData['message'],
  76. );
  77. }
  78. private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
  79. try {
  80. $cachedString = json_encode([
  81. 'id' => $data->getId(),
  82. 'startDate' => $data->getStartDate(),
  83. 'endDate' => $data->getEndDate(),
  84. 'shortMessage' => $data->getShortMessage(),
  85. 'message' => $data->getMessage(),
  86. ], JSON_THROW_ON_ERROR);
  87. } catch (JsonException $e) {
  88. $this->logger->error('Failed to serialize out-of-office data: ' . $e->getMessage(), [
  89. 'exception' => $e,
  90. ]);
  91. return;
  92. }
  93. $this->cache->set($data->getUser()->getUID(), $cachedString, 300);
  94. }
  95. public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
  96. $cachedData = $this->getCachedOutOfOfficeData($user);
  97. if ($cachedData !== null) {
  98. return $cachedData;
  99. }
  100. try {
  101. $absenceData = $this->absenceMapper->findByUserId($user->getUID());
  102. } catch (DoesNotExistException $e) {
  103. return null;
  104. }
  105. $data = $absenceData->toOutOufOfficeData(
  106. $user,
  107. $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
  108. );
  109. $this->setCachedOutOfOfficeData($data);
  110. return $data;
  111. }
  112. }