-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dav): dispatch out-of-office started and ended events
Signed-off-by: Richard Steinmetz <[email protected]>
- Loading branch information
Showing
10 changed files
with
278 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
apps/dav/lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]> | ||
* | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @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\Db\AbsenceMapper; | ||
use OCA\Mail\Vendor\Psr\Log\LoggerInterface; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\QueuedJob; | ||
use OCP\EventDispatcher\IEventDispatcher; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use OCP\User\Events\OutOfOfficeEndedEvent; | ||
use OCP\User\Events\OutOfOfficeStartedEvent; | ||
|
||
class OutOfOfficeEventDispatcherJob extends QueuedJob { | ||
public const EVENT_START = 'start'; | ||
public const EVENT_END = 'end'; | ||
|
||
/** @var array<string, IUser> $users */ | ||
private array $userCache = []; | ||
|
||
public function __construct( | ||
ITimeFactory $time, | ||
private AbsenceMapper $absenceMapper, | ||
private LoggerInterface $logger, | ||
private IEventDispatcher $eventDispatcher, | ||
private IUserManager $userManager, | ||
) { | ||
parent::__construct($time); | ||
} | ||
|
||
protected function run($argument) { | ||
$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->getUser($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); | ||
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, | ||
]); | ||
} | ||
} | ||
|
||
private function getUser(string $userId): ?IUser { | ||
if (isset($this->userCache[$userId])) { | ||
return $this->userCache[$userId]; | ||
} | ||
|
||
$user = $this->userManager->get($userId); | ||
if ($user === null) { | ||
return null; | ||
} | ||
|
||
$this->userCache[$userId] = $user; | ||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]> | ||
* | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @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 OCP\User\Events; | ||
|
||
use OCP\EventDispatcher\Event; | ||
use OCP\User\IOutOfOfficeData; | ||
|
||
/** | ||
* Emitted when a user's out-of-office period ended | ||
* | ||
* @since 28.0.0 | ||
*/ | ||
class OutOfOfficeEndedEvent extends Event { | ||
/** | ||
* @since 28.0.0 | ||
*/ | ||
public function __construct(private IOutOfOfficeData $data) { | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @since 28.0.0 | ||
*/ | ||
public function getData(): IOutOfOfficeData { | ||
return $this->data; | ||
} | ||
} |
Oops, something went wrong.