1
0

OutOfOfficeEventDispatcherJob.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\BackgroundJob;
  8. use OCA\DAV\CalDAV\TimezoneService;
  9. use OCA\DAV\Db\AbsenceMapper;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\QueuedJob;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IUserManager;
  15. use OCP\User\Events\OutOfOfficeEndedEvent;
  16. use OCP\User\Events\OutOfOfficeStartedEvent;
  17. use Psr\Log\LoggerInterface;
  18. class OutOfOfficeEventDispatcherJob extends QueuedJob {
  19. public const EVENT_START = 'start';
  20. public const EVENT_END = 'end';
  21. public function __construct(
  22. ITimeFactory $time,
  23. private AbsenceMapper $absenceMapper,
  24. private LoggerInterface $logger,
  25. private IEventDispatcher $eventDispatcher,
  26. private IUserManager $userManager,
  27. private TimezoneService $timezoneService,
  28. ) {
  29. parent::__construct($time);
  30. }
  31. public function run($argument): void {
  32. $id = $argument['id'];
  33. $event = $argument['event'];
  34. try {
  35. $absence = $this->absenceMapper->findById($id);
  36. } catch (DoesNotExistException|\OCP\DB\Exception $e) {
  37. $this->logger->error('Failed to dispatch out-of-office event: ' . $e->getMessage(), [
  38. 'exception' => $e,
  39. 'argument' => $argument,
  40. ]);
  41. return;
  42. }
  43. $userId = $absence->getUserId();
  44. $user = $this->userManager->get($userId);
  45. if ($user === null) {
  46. $this->logger->error("Failed to dispatch out-of-office event: User $userId does not exist", [
  47. 'argument' => $argument,
  48. ]);
  49. return;
  50. }
  51. $data = $absence->toOutOufOfficeData(
  52. $user,
  53. $this->timezoneService->getUserTimezone($userId) ?? $this->timezoneService->getDefaultTimezone(),
  54. );
  55. if ($event === self::EVENT_START) {
  56. $this->eventDispatcher->dispatchTyped(new OutOfOfficeStartedEvent($data));
  57. } elseif ($event === self::EVENT_END) {
  58. $this->eventDispatcher->dispatchTyped(new OutOfOfficeEndedEvent($data));
  59. } else {
  60. $this->logger->error("Invalid out-of-office event: $event", [
  61. 'argument' => $argument,
  62. ]);
  63. }
  64. }
  65. }