123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- declare(strict_types=1);
- namespace OCA\DAV\CalDAV\Status;
- use DateTimeImmutable;
- use OC\Calendar\CalendarQuery;
- use OCA\DAV\CalDAV\CalendarImpl;
- use OCA\UserStatus\Service\StatusService as UserStatusService;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Calendar\IManager;
- use OCP\DB\Exception;
- use OCP\ICache;
- use OCP\ICacheFactory;
- use OCP\IUser as User;
- use OCP\IUserManager;
- use OCP\User\IAvailabilityCoordinator;
- use OCP\UserStatus\IUserStatus;
- use Psr\Log\LoggerInterface;
- use Sabre\CalDAV\Xml\Property\ScheduleCalendarTransp;
- class StatusService {
- private ICache $cache;
- public function __construct(
- private ITimeFactory $timeFactory,
- private IManager $calendarManager,
- private IUserManager $userManager,
- private UserStatusService $userStatusService,
- private IAvailabilityCoordinator $availabilityCoordinator,
- private ICacheFactory $cacheFactory,
- private LoggerInterface $logger,
- ) {
- $this->cache = $cacheFactory->createLocal('CalendarStatusService');
- }
- public function processCalendarStatus(string $userId): void {
- $user = $this->userManager->get($userId);
- if ($user === null) {
- return;
- }
- $availability = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
- if ($availability !== null && $this->availabilityCoordinator->isInEffect($availability)) {
- $this->logger->debug('An Absence is in effect, skipping calendar status check', ['user' => $userId]);
- return;
- }
- $calendarEvents = $this->cache->get($userId);
- if ($calendarEvents === null) {
- $calendarEvents = $this->getCalendarEvents($user);
- $this->cache->set($userId, $calendarEvents, 300);
- }
- if (empty($calendarEvents)) {
- try {
- $this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
- } catch (Exception $e) {
- if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
-
-
-
-
- $this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
- return;
- }
- }
- $this->logger->debug('No calendar events found for status check', ['user' => $userId]);
- return;
- }
- try {
- $currentStatus = $this->userStatusService->findByUserId($userId);
-
- $userStatusTimestamp = $currentStatus->getIsUserDefined() && $currentStatus->getMessageId() !== IUserStatus::MESSAGE_CALENDAR_BUSY ? $currentStatus->getStatusTimestamp() : null;
- } catch (DoesNotExistException) {
- $userStatusTimestamp = null;
- $currentStatus = null;
- }
- if (($currentStatus !== null && $currentStatus->getMessageId() === IUserStatus::MESSAGE_CALL)
- || ($currentStatus !== null && $currentStatus->getStatus() === IUserStatus::DND)
- || ($currentStatus !== null && $currentStatus->getStatus() === IUserStatus::INVISIBLE)) {
-
- $this->logger->debug('Higher priority status detected, skipping calendar status change', ['user' => $userId]);
- return;
- }
-
- $applicableEvents = array_filter($calendarEvents, static function (array $calendarEvent) use ($userStatusTimestamp): bool {
- if (empty($calendarEvent['objects'])) {
- return false;
- }
- $component = $calendarEvent['objects'][0];
- if (isset($component['X-NEXTCLOUD-OUT-OF-OFFICE'])) {
- return false;
- }
- if (isset($component['DTSTART']) && $userStatusTimestamp !== null) {
-
- $dateTime = $component['DTSTART'][0];
- if ($dateTime instanceof DateTimeImmutable && $userStatusTimestamp > $dateTime->getTimestamp()) {
- return false;
- }
- }
-
- if (isset($component['TRANSP']) && strcasecmp($component['TRANSP'][0], 'TRANSPARENT') === 0) {
- return false;
- }
- return true;
- });
- if (empty($applicableEvents)) {
- try {
- $this->userStatusService->revertUserStatus($userId, IUserStatus::MESSAGE_CALENDAR_BUSY);
- } catch (Exception $e) {
- if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
-
-
-
-
- $this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
- return;
- }
- }
- $this->logger->debug('No status relevant events found, skipping calendar status change', ['user' => $userId]);
- return;
- }
-
- if ($currentStatus === null || $currentStatus->getMessageId() !== IUserStatus::MESSAGE_CALENDAR_BUSY) {
-
-
-
-
- $count = count($applicableEvents);
- $this->logger->debug("Found $count applicable event(s), changing user status", ['user' => $userId]);
- $this->userStatusService->setUserStatus(
- $userId,
- IUserStatus::AWAY,
- IUserStatus::MESSAGE_CALENDAR_BUSY,
- true
- );
- }
- }
- private function getCalendarEvents(User $user): array {
- $calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $user->getUID());
- if (empty($calendars)) {
- return [];
- }
- $query = $this->calendarManager->newQuery('principals/users/' . $user->getUID());
- foreach ($calendars as $calendarObject) {
-
- if (!$calendarObject instanceof CalendarImpl) {
- continue;
- }
- $sct = $calendarObject->getSchedulingTransparency();
- if ($sct !== null && strtolower($sct->getValue()) == ScheduleCalendarTransp::TRANSPARENT) {
-
-
- continue;
- }
- $query->addSearchCalendar($calendarObject->getUri());
- }
- $dtStart = DateTimeImmutable::createFromMutable($this->timeFactory->getDateTime());
- $dtEnd = DateTimeImmutable::createFromMutable($this->timeFactory->getDateTime('+5 minutes'));
-
- if ($query instanceof CalendarQuery && !empty($query->getCalendarUris())) {
-
- $query->setTimerangeStart($dtStart);
- $query->setTimerangeEnd($dtEnd);
- return $this->calendarManager->searchForPrincipal($query);
- }
- return [];
- }
- }
|