absenceMapper->findByUserId($user->getUID()); } catch (DoesNotExistException) { $absence = new Absence(); } $absence->setUserId($user->getUID()); $absence->setFirstDay($firstDay); $absence->setLastDay($lastDay); $absence->setStatus($status); $absence->setMessage($message); $absence->setReplacementUserId($replacementUserId); $absence->setReplacementUserDisplayName($replacementUserDisplayName); if ($absence->getId() === null) { $absence = $this->absenceMapper->insert($absence); $eventData = $absence->toOutOufOfficeData( $user, $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(), ); $this->eventDispatcher->dispatchTyped(new OutOfOfficeScheduledEvent($eventData)); } else { $absence = $this->absenceMapper->update($absence); $eventData = $absence->toOutOufOfficeData( $user, $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(), ); $this->eventDispatcher->dispatchTyped(new OutOfOfficeChangedEvent($eventData)); } $now = $this->timeFactory->getTime(); if ($eventData->getStartDate() > $now) { $this->jobList->scheduleAfter( OutOfOfficeEventDispatcherJob::class, $eventData->getStartDate(), [ 'id' => $absence->getId(), 'event' => OutOfOfficeEventDispatcherJob::EVENT_START, ], ); } if ($eventData->getEndDate() > $now) { $this->jobList->scheduleAfter( OutOfOfficeEventDispatcherJob::class, $eventData->getEndDate(), [ 'id' => $absence->getId(), 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, ], ); } return $absence; } /** * @throws \OCP\DB\Exception */ public function clearAbsence(IUser $user): void { try { $absence = $this->absenceMapper->findByUserId($user->getUID()); } catch (DoesNotExistException $e) { // Nothing to clear return; } $this->absenceMapper->delete($absence); $this->jobList->remove(OutOfOfficeEventDispatcherJob::class); $eventData = $absence->toOutOufOfficeData( $user, $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(), ); $this->eventDispatcher->dispatchTyped(new OutOfOfficeClearedEvent($eventData)); } public function getAbsence(string $userId): ?Absence { try { return $this->absenceMapper->findByUserId($userId); } catch (DoesNotExistException $e) { return null; } } public function getCurrentAbsence(IUser $user): ?IOutOfOfficeData { try { $absence = $this->absenceMapper->findByUserId($user->getUID()); $oooData = $absence->toOutOufOfficeData( $user, $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(), ); if ($this->isInEffect($oooData)) { return $oooData; } } catch (DoesNotExistException) { // Nothing there to process } return null; } public function isInEffect(IOutOfOfficeData $absence): bool { $now = $this->timeFactory->getTime(); return $absence->getStartDate() <= $now && $absence->getEndDate() >= $now; } }