aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php
diff options
context:
space:
mode:
authorRichard Steinmetz <richard@steinmetz.cloud>2023-11-13 17:36:24 +0100
committerChristoph Wurst <christoph@winzerhof-wurst.at>2023-11-23 17:18:49 +0100
commit8191295f66cdea5da7854bfad01ad9540c4a55f4 (patch)
tree93fd27fe91ab0f40b4c765139a957b420806dedf /apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php
parent953382e937a4085c1099449b29c40c7aab02fc3e (diff)
downloadnextcloud-server-8191295f66cdea5da7854bfad01ad9540c4a55f4.tar.gz
nextcloud-server-8191295f66cdea5da7854bfad01ad9540c4a55f4.zip
feat(dav): dispatch out-of-office started and ended events
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
Diffstat (limited to 'apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php')
-rw-r--r--apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php b/apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php
new file mode 100644
index 00000000000..9b219cf30da
--- /dev/null
+++ b/apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php
@@ -0,0 +1,92 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
+ *
+ * @author Richard Steinmetz <richard@steinmetz.cloud>
+ *
+ * @license AGPL-3.0-or-later
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\DAV\BackgroundJob;
+
+use OCA\DAV\CalDAV\TimezoneService;
+use OCA\DAV\Db\AbsenceMapper;
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\QueuedJob;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\IUserManager;
+use OCP\User\Events\OutOfOfficeEndedEvent;
+use OCP\User\Events\OutOfOfficeStartedEvent;
+use Psr\Log\LoggerInterface;
+
+class OutOfOfficeEventDispatcherJob extends QueuedJob {
+ public const EVENT_START = 'start';
+ public const EVENT_END = 'end';
+
+ public function __construct(
+ ITimeFactory $time,
+ private AbsenceMapper $absenceMapper,
+ private LoggerInterface $logger,
+ private IEventDispatcher $eventDispatcher,
+ private IUserManager $userManager,
+ private TimezoneService $timezoneService,
+ ) {
+ parent::__construct($time);
+ }
+
+ public function run($argument): void {
+ $id = $argument['id'];
+ $event = $argument['event'];
+
+ try {
+ $absence = $this->absenceMapper->findById($id);
+ } catch (DoesNotExistException | \OCP\DB\Exception $e) {
+ $this->logger->error('Failed to dispatch out-of-office event: ' . $e->getMessage(), [
+ 'exception' => $e,
+ 'argument' => $argument,
+ ]);
+ return;
+ }
+
+ $userId = $absence->getUserId();
+ $user = $this->userManager->get($userId);
+ if ($user === null) {
+ $this->logger->error("Failed to dispatch out-of-office event: User $userId does not exist", [
+ 'argument' => $argument,
+ ]);
+ return;
+ }
+
+ $data = $absence->toOutOufOfficeData(
+ $user,
+ $this->timezoneService->getUserTimezone($userId) ?? $this->timezoneService->getDefaultTimezone(),
+ );
+ if ($event === self::EVENT_START) {
+ $this->eventDispatcher->dispatchTyped(new OutOfOfficeStartedEvent($data));
+ } elseif ($event === self::EVENT_END) {
+ $this->eventDispatcher->dispatchTyped(new OutOfOfficeEndedEvent($data));
+ } else {
+ $this->logger->error("Invalid out-of-office event: $event", [
+ 'argument' => $argument,
+ ]);
+ }
+ }
+}