OutOfOfficeStatusListener.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Anna Larch <anna.larch@gmx.net>
  5. *
  6. * @author Anna Larch <anna.larch@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (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 Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\UserStatus\Listener;
  25. use OCA\DAV\BackgroundJob\UserStatusAutomation;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\EventDispatcher\Event;
  29. use OCP\EventDispatcher\IEventListener;
  30. use OCP\User\Events\OutOfOfficeChangedEvent;
  31. use OCP\User\Events\OutOfOfficeClearedEvent;
  32. use OCP\User\Events\OutOfOfficeScheduledEvent;
  33. use OCP\UserStatus\IManager;
  34. use OCP\UserStatus\IUserStatus;
  35. /**
  36. * Class UserDeletedListener
  37. *
  38. * @template-implements IEventListener<OutOfOfficeScheduledEvent|OutOfOfficeChangedEvent|OutOfOfficeClearedEvent>
  39. *
  40. */
  41. class OutOfOfficeStatusListener implements IEventListener {
  42. public function __construct(private IJobList $jobsList,
  43. private ITimeFactory $time,
  44. private IManager $manager) {
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. public function handle(Event $event): void {
  50. if($event instanceof OutOfOfficeClearedEvent) {
  51. $this->manager->revertUserStatus($event->getData()->getUser()->getUID(), IUserStatus::MESSAGE_OUT_OF_OFFICE, IUserStatus::DND);
  52. $this->jobsList->scheduleAfter(UserStatusAutomation::class, $this->time->getTime(), ['userId' => $event->getData()->getUser()->getUID()]);
  53. return;
  54. }
  55. if ($event instanceof OutOfOfficeScheduledEvent
  56. || $event instanceof OutOfOfficeChangedEvent) {
  57. // This might be overwritten by the office hours automation, but that is ok. This is just in case no office hours are set
  58. $this->jobsList->scheduleAfter(UserStatusAutomation::class, $this->time->getTime(), ['userId' => $event->getData()->getUser()->getUID()]);
  59. }
  60. }
  61. }