AbsenceService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Service;
  25. use InvalidArgumentException;
  26. use OCA\DAV\BackgroundJob\OutOfOfficeEventDispatcherJob;
  27. use OCA\DAV\CalDAV\TimezoneService;
  28. use OCA\DAV\Db\Absence;
  29. use OCA\DAV\Db\AbsenceMapper;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IUser;
  35. use OCP\User\Events\OutOfOfficeChangedEvent;
  36. use OCP\User\Events\OutOfOfficeClearedEvent;
  37. use OCP\User\Events\OutOfOfficeScheduledEvent;
  38. use OCP\User\IOutOfOfficeData;
  39. class AbsenceService {
  40. public function __construct(
  41. private AbsenceMapper $absenceMapper,
  42. private IEventDispatcher $eventDispatcher,
  43. private IJobList $jobList,
  44. private TimezoneService $timezoneService,
  45. private ITimeFactory $timeFactory,
  46. ) {
  47. }
  48. /**
  49. * @param string $firstDay The first day (inclusive) of the absence formatted as YYYY-MM-DD.
  50. * @param string $lastDay The last day (inclusive) of the absence formatted as YYYY-MM-DD.
  51. *
  52. * @throws \OCP\DB\Exception
  53. * @throws InvalidArgumentException If no user with the given user id exists.
  54. */
  55. public function createOrUpdateAbsence(
  56. IUser $user,
  57. string $firstDay,
  58. string $lastDay,
  59. string $status,
  60. string $message,
  61. ): Absence {
  62. try {
  63. $absence = $this->absenceMapper->findByUserId($user->getUID());
  64. } catch (DoesNotExistException) {
  65. $absence = new Absence();
  66. }
  67. $absence->setUserId($user->getUID());
  68. $absence->setFirstDay($firstDay);
  69. $absence->setLastDay($lastDay);
  70. $absence->setStatus($status);
  71. $absence->setMessage($message);
  72. if ($absence->getId() === null) {
  73. $absence = $this->absenceMapper->insert($absence);
  74. $eventData = $absence->toOutOufOfficeData(
  75. $user,
  76. $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
  77. );
  78. $this->eventDispatcher->dispatchTyped(new OutOfOfficeScheduledEvent($eventData));
  79. } else {
  80. $absence = $this->absenceMapper->update($absence);
  81. $eventData = $absence->toOutOufOfficeData(
  82. $user,
  83. $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
  84. );
  85. $this->eventDispatcher->dispatchTyped(new OutOfOfficeChangedEvent($eventData));
  86. }
  87. $now = $this->timeFactory->getTime();
  88. if ($eventData->getStartDate() > $now) {
  89. $this->jobList->scheduleAfter(
  90. OutOfOfficeEventDispatcherJob::class,
  91. $eventData->getStartDate(),
  92. [
  93. 'id' => $absence->getId(),
  94. 'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
  95. ],
  96. );
  97. }
  98. if ($eventData->getEndDate() > $now) {
  99. $this->jobList->scheduleAfter(
  100. OutOfOfficeEventDispatcherJob::class,
  101. $eventData->getEndDate(),
  102. [
  103. 'id' => $absence->getId(),
  104. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  105. ],
  106. );
  107. }
  108. return $absence;
  109. }
  110. /**
  111. * @throws \OCP\DB\Exception
  112. */
  113. public function clearAbsence(IUser $user): void {
  114. try {
  115. $absence = $this->absenceMapper->findByUserId($user->getUID());
  116. } catch (DoesNotExistException $e) {
  117. // Nothing to clear
  118. return;
  119. }
  120. $this->absenceMapper->delete($absence);
  121. $this->jobList->remove(OutOfOfficeEventDispatcherJob::class);
  122. $eventData = $absence->toOutOufOfficeData(
  123. $user,
  124. $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
  125. );
  126. $this->eventDispatcher->dispatchTyped(new OutOfOfficeClearedEvent($eventData));
  127. }
  128. public function getAbsence(string $userId): ?Absence {
  129. try {
  130. return $this->absenceMapper->findByUserId($userId);
  131. } catch (DoesNotExistException $e) {
  132. return null;
  133. }
  134. }
  135. public function getCurrentAbsence(IUser $user): ?IOutOfOfficeData {
  136. try {
  137. $absence = $this->absenceMapper->findByUserId($user->getUID());
  138. $oooData = $absence->toOutOufOfficeData(
  139. $user,
  140. $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
  141. );
  142. if ($this->isInEffect($oooData)) {
  143. return $oooData;
  144. }
  145. } catch (DoesNotExistException) {
  146. // Nothing there to process
  147. }
  148. return null;
  149. }
  150. public function isInEffect(IOutOfOfficeData $absence): bool {
  151. $now = $this->timeFactory->getTime();
  152. return $absence->getStartDate() <= $now && $absence->getEndDate() >= $now;
  153. }
  154. }