diff options
author | Anna Larch <anna@nextcloud.com> | 2023-11-24 01:49:30 +0100 |
---|---|---|
committer | Anna Larch <anna@nextcloud.com> | 2023-11-28 10:28:06 +0100 |
commit | f19645adab404a9c2642b42ec335bf2830dd8aa7 (patch) | |
tree | 3f97f43788aadabce26c1a859c669274639fa695 /apps/user_status/lib | |
parent | 53f31498049cf0dcd61c6ef1d840801bd81f055c (diff) | |
download | nextcloud-server-f19645adab404a9c2642b42ec335bf2830dd8aa7.tar.gz nextcloud-server-f19645adab404a9c2642b42ec335bf2830dd8aa7.zip |
enh(userstatus): add OOO automation and remove calendar automation
Signed-off-by: Anna Larch <anna@nextcloud.com>
Diffstat (limited to 'apps/user_status/lib')
6 files changed, 92 insertions, 70 deletions
diff --git a/apps/user_status/lib/AppInfo/Application.php b/apps/user_status/lib/AppInfo/Application.php index 68e5e6169ee..26f736bbc24 100644 --- a/apps/user_status/lib/AppInfo/Application.php +++ b/apps/user_status/lib/AppInfo/Application.php @@ -29,6 +29,7 @@ use OCA\UserStatus\Capabilities; use OCA\UserStatus\Connector\UserStatusProvider; use OCA\UserStatus\Dashboard\UserStatusWidget; use OCA\UserStatus\Listener\BeforeTemplateRenderedListener; +use OCA\UserStatus\Listener\OutOfOfficeStatusListener; use OCA\UserStatus\Listener\UserDeletedListener; use OCA\UserStatus\Listener\UserLiveStatusListener; use OCP\AppFramework\App; @@ -37,6 +38,9 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\IConfig; +use OCP\User\Events\OutOfOfficeChangedEvent; +use OCP\User\Events\OutOfOfficeClearedEvent; +use OCP\User\Events\OutOfOfficeScheduledEvent; use OCP\User\Events\UserDeletedEvent; use OCP\User\Events\UserLiveStatusEvent; use OCP\UserStatus\IManager; @@ -71,6 +75,9 @@ class Application extends App implements IBootstrap { $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); $context->registerEventListener(UserLiveStatusEvent::class, UserLiveStatusListener::class); $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); + $context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeStatusListener::class); + $context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeStatusListener::class); + $context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeStatusListener::class); $config = $this->getContainer()->query(IConfig::class); $shareeEnumeration = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php index 4bc2645dceb..700cd3c10fc 100644 --- a/apps/user_status/lib/Connector/UserStatusProvider.php +++ b/apps/user_status/lib/Connector/UserStatusProvider.php @@ -57,8 +57,8 @@ class UserStatusProvider implements IProvider, ISettableProvider { return $userStatuses; } - public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup): void { - $this->service->setUserStatus($userId, $status, $messageId, $createBackup); + public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup, ?string $customMessage = null): void { + $this->service->setUserStatus($userId, $status, $messageId, $createBackup, $customMessage); } public function revertUserStatus(string $userId, string $messageId, string $status): void { diff --git a/apps/user_status/lib/Db/UserStatusMapper.php b/apps/user_status/lib/Db/UserStatusMapper.php index febee32c821..3621c1bfa96 100644 --- a/apps/user_status/lib/Db/UserStatusMapper.php +++ b/apps/user_status/lib/Db/UserStatusMapper.php @@ -30,7 +30,6 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\UserStatus\IUserStatus; -use Sabre\CalDAV\Schedule\Plugin; /** * @template-extends QBMapper<UserStatus> @@ -211,23 +210,4 @@ class UserStatusMapper extends QBMapper { $qb->executeStatement(); } - - public function getAvailabilityFromPropertiesTable(string $userId): ?string { - $propertyPath = 'calendars/' . $userId . '/inbox'; - $propertyName = '{' . Plugin::NS_CALDAV . '}calendar-availability'; - - $query = $this->db->getQueryBuilder(); - $query->select('propertyvalue') - ->from('properties') - ->where($query->expr()->eq('userid', $query->createNamedParameter($userId))) - ->andWhere($query->expr()->eq('propertypath', $query->createNamedParameter($propertyPath))) - ->andWhere($query->expr()->eq('propertyname', $query->createNamedParameter($propertyName))) - ->setMaxResults(1); - - $result = $query->executeQuery(); - $property = $result->fetchOne(); - $result->closeCursor(); - - return ($property === false ? null : $property); - } } diff --git a/apps/user_status/lib/Listener/OutOfOfficeStatusListener.php b/apps/user_status/lib/Listener/OutOfOfficeStatusListener.php new file mode 100644 index 00000000000..0d793c3f306 --- /dev/null +++ b/apps/user_status/lib/Listener/OutOfOfficeStatusListener.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Anna Larch <anna.larch@gmx.net> + * + * @author Anna Larch <anna.larch@gmx.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\UserStatus\Listener; + +use OCA\DAV\BackgroundJob\UserStatusAutomation; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\OutOfOfficeChangedEvent; +use OCP\User\Events\OutOfOfficeClearedEvent; +use OCP\User\Events\OutOfOfficeScheduledEvent; +use OCP\UserStatus\IManager; +use OCP\UserStatus\IUserStatus; + +/** + * Class UserDeletedListener + * + * @template-implements IEventListener<OutOfOfficeScheduledEvent|OutOfOfficeChangedEvent|OutOfOfficeClearedEvent> + * + */ +class OutOfOfficeStatusListener implements IEventListener { + public function __construct(private IJobList $jobsList, + private ITimeFactory $time, + private IManager $manager) { + } + + /** + * @inheritDoc + */ + public function handle(Event $event): void { + if($event instanceof OutOfOfficeClearedEvent) { + $this->manager->revertUserStatus($event->getData()->getUser()->getUID(), IUserStatus::MESSAGE_VACATION, IUserStatus::DND); + $this->jobsList->scheduleAfter(UserStatusAutomation::class, $this->time->getTime(), ['userId' => $event->getData()->getUser()->getUID()]); + return; + } + + if ($event instanceof OutOfOfficeScheduledEvent + || $event instanceof OutOfOfficeChangedEvent) { + // This might be overwritten by the office hours automation, but that is ok. This is just in case no office hours are set + $this->jobsList->scheduleAfter(UserStatusAutomation::class, $this->time->getTime(), ['userId' => $event->getData()->getUser()->getUID()]); + } + } +} diff --git a/apps/user_status/lib/Service/PredefinedStatusService.php b/apps/user_status/lib/Service/PredefinedStatusService.php index 516680ba683..c77ca588ebb 100644 --- a/apps/user_status/lib/Service/PredefinedStatusService.php +++ b/apps/user_status/lib/Service/PredefinedStatusService.php @@ -202,6 +202,7 @@ class PredefinedStatusService { self::REMOTE_WORK, IUserStatus::MESSAGE_CALL, IUserStatus::MESSAGE_AVAILABILITY, + IUserStatus::MESSAGE_VACATION, IUserStatus::MESSAGE_CALENDAR_BUSY, IUserStatus::MESSAGE_CALENDAR_BUSY_TENTATIVE, ], true); diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 829c6c58570..114764f193d 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -137,37 +137,8 @@ class StatusService { * @return UserStatus * @throws DoesNotExistException */ - public function findByUserId(string $userId): UserStatus { - $userStatus = $this->mapper->findByUserId($userId); - // If the status is user-defined and one of the persistent status, we - // will not override it. - if ($userStatus->getIsUserDefined() && \in_array($userStatus->getStatus(), StatusService::PERSISTENT_STATUSES, true)) { - return $this->processStatus($userStatus); - } - - $calendarStatus = $this->getCalendarStatus($userId); - // We found no status from the calendar, proceed with the existing status - if($calendarStatus === null) { - return $this->processStatus($userStatus); - } - - // if we have the same status result for the calendar and the current status, - // and a custom message to boot, we leave the existing status alone - // as to not overwrite a custom message / emoji - if($userStatus->getIsUserDefined() - && $calendarStatus->getStatus() === $userStatus->getStatus() - && !empty($userStatus->getCustomMessage())) { - return $this->processStatus($userStatus); - } - - // If the new status is null, there's already an identical status in place - $newUserStatus = $this->setUserStatus($userId, - $calendarStatus->getStatus(), - $calendarStatus->getMessage() ?? IUserStatus::MESSAGE_AVAILABILITY, - true, - $calendarStatus->getCustomMessage() ?? ''); - - return $newUserStatus === null ? $this->processStatus($userStatus) : $this->processStatus($newUserStatus); + public function findByUserId(string $userId):UserStatus { + return $this->processStatus($this->mapper->findByUserId($userId)); } /** @@ -271,6 +242,7 @@ class StatusService { * @param string $status * @param string $messageId * @param bool $createBackup + * @param string|null $customMessage * @throws InvalidStatusTypeException * @throws InvalidMessageIdException */ @@ -278,7 +250,7 @@ class StatusService { string $status, string $messageId, bool $createBackup, - string $customMessage = null): ?UserStatus { + ?string $customMessage = null): ?UserStatus { // Check if status-type is valid if (!in_array($status, self::PRIORITY_ORDERED_STATUSES, true)) { throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported'); @@ -313,13 +285,7 @@ class StatusService { $userStatus->setCustomIcon(null); $userStatus->setCustomMessage($customMessage); $userStatus->setClearAt(null); - if ($this->predefinedStatusService->getTranslatedStatusForId($messageId) !== null - || ($customMessage !== null && $customMessage !== '')) { - // Only track status message ID if there is one - $userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp()); - } else { - $userStatus->setStatusMessageTimestamp(0); - } + $userStatus->setStatusMessageTimestamp($this->timeFactory->now()->getTimestamp()); if ($userStatus->getId() !== null) { return $this->mapper->update($userStatus); @@ -506,8 +472,14 @@ class StatusService { private function addDefaultMessage(UserStatus $status): void { // If the message is predefined, insert the translated message and icon $predefinedMessage = $this->predefinedStatusService->getDefaultStatusById($status->getMessageId()); - if ($predefinedMessage !== null) { + if ($predefinedMessage === null) { + return; + } + // If there is a custom message, don't overwrite it + if(empty($status->getCustomMessage())) { $status->setCustomMessage($predefinedMessage['message']); + } + if(empty($status->getCustomIcon())) { $status->setCustomIcon($predefinedMessage['icon']); } } @@ -588,8 +560,7 @@ class StatusService { } /** - * Calculate a users' status according to their availabilit settings and their calendar - * events + * Calculate a users' status according to their calendar events * * There are 4 predefined types of FBTYPE - 'FREE', 'BUSY', 'BUSY-UNAVAILABLE', 'BUSY-TENTATIVE', * but 'X-' properties are possible @@ -598,11 +569,10 @@ class StatusService { * * The status will be changed for types * - 'BUSY' - * - 'BUSY-UNAVAILABLE' (ex.: when a VAVILABILITY setting is in effect) * - 'BUSY-TENTATIVE' (ex.: an event has been accepted tentatively) * and all FREEBUSY components without a type (implicitly a 'BUSY' status) * - * 'X-' properties are not handled for now + * 'X-' properties and BUSY-UNAVAILABLE is not handled * * @param string $userId * @return CalendarStatus|null @@ -612,9 +582,6 @@ class StatusService { if ($user === null) { return null; } - - $availability = $this->mapper->getAvailabilityFromPropertiesTable($userId); - - return $this->calendarStatusService->processCalendarAvailability($user, $availability); + return $this->calendarStatusService->processCalendarAvailability($user); } } |