1
0

OutOfOfficeEventDispatcherJob.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\BackgroundJob;
  25. use OCA\DAV\CalDAV\TimezoneService;
  26. use OCA\DAV\Db\AbsenceMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\QueuedJob;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\IUserManager;
  32. use OCP\User\Events\OutOfOfficeEndedEvent;
  33. use OCP\User\Events\OutOfOfficeStartedEvent;
  34. use Psr\Log\LoggerInterface;
  35. class OutOfOfficeEventDispatcherJob extends QueuedJob {
  36. public const EVENT_START = 'start';
  37. public const EVENT_END = 'end';
  38. public function __construct(
  39. ITimeFactory $time,
  40. private AbsenceMapper $absenceMapper,
  41. private LoggerInterface $logger,
  42. private IEventDispatcher $eventDispatcher,
  43. private IUserManager $userManager,
  44. private TimezoneService $timezoneService,
  45. ) {
  46. parent::__construct($time);
  47. }
  48. public function run($argument): void {
  49. $id = $argument['id'];
  50. $event = $argument['event'];
  51. try {
  52. $absence = $this->absenceMapper->findById($id);
  53. } catch (DoesNotExistException | \OCP\DB\Exception $e) {
  54. $this->logger->error('Failed to dispatch out-of-office event: ' . $e->getMessage(), [
  55. 'exception' => $e,
  56. 'argument' => $argument,
  57. ]);
  58. return;
  59. }
  60. $userId = $absence->getUserId();
  61. $user = $this->userManager->get($userId);
  62. if ($user === null) {
  63. $this->logger->error("Failed to dispatch out-of-office event: User $userId does not exist", [
  64. 'argument' => $argument,
  65. ]);
  66. return;
  67. }
  68. $data = $absence->toOutOufOfficeData(
  69. $user,
  70. $this->timezoneService->getUserTimezone($userId) ?? $this->timezoneService->getDefaultTimezone(),
  71. );
  72. if ($event === self::EVENT_START) {
  73. $this->eventDispatcher->dispatchTyped(new OutOfOfficeStartedEvent($data));
  74. } elseif ($event === self::EVENT_END) {
  75. $this->eventDispatcher->dispatchTyped(new OutOfOfficeEndedEvent($data));
  76. } else {
  77. $this->logger->error("Invalid out-of-office event: $event", [
  78. 'argument' => $argument,
  79. ]);
  80. }
  81. }
  82. }