AvailabilityCoordinator.php 3.4 KB

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