-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat: create example event when a user logs in for the first time
- Loading branch information
Showing
5 changed files
with
79 additions
and
47 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
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 was deleted.
Oops, something went wrong.
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Calendar\Listener; | ||
|
||
use OCA\Calendar\Exception\ServiceException; | ||
use OCA\Calendar\Service\ExampleEventService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\ServerVersion; | ||
use OCP\User\Events\UserFirstTimeLoggedInEvent; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @template-implements IEventListener<UserFirstTimeLoggedInEvent> */ | ||
class UserFirstLoginListener implements IEventListener { | ||
private bool $is31OrAbove; | ||
|
||
public function __construct( | ||
private readonly ExampleEventService $exampleEventService, | ||
private readonly LoggerInterface $logger, | ||
ContainerInterface $container, | ||
) { | ||
$this->is31OrAbove = self::isNextcloud31OrAbove($container); | ||
} | ||
|
||
private static function isNextcloud31OrAbove(ContainerInterface $container): bool { | ||
// ServerVersion was added in 31, but we don't care about older versions anyway | ||
try { | ||
/** @var ServerVersion $serverVersion */ | ||
$serverVersion = $container->get(ServerVersion::class); | ||
Check failure on line 38 in lib/Listener/UserFirstLoginListener.php
|
||
} catch (ContainerExceptionInterface $e) { | ||
return false; | ||
} | ||
|
||
return $serverVersion->getMajorVersion() >= 31; | ||
Check failure on line 43 in lib/Listener/UserFirstLoginListener.php
|
||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof UserFirstTimeLoggedInEvent)) { | ||
return; | ||
} | ||
|
||
// TODO: drop condition once we only support Nextcloud >= 31 | ||
if (!$this->is31OrAbove) { | ||
return; | ||
} | ||
|
||
if (!$this->exampleEventService->shouldCreateExampleEvent()) { | ||
return; | ||
} | ||
|
||
$userId = $event->getUser()->getUID(); | ||
try { | ||
$this->exampleEventService->createExampleEvent($userId); | ||
} catch (ServiceException $e) { | ||
$this->logger->error( | ||
"Failed to create example event for user $userId: " . $e->getMessage(), | ||
[ | ||
'exception' => $e, | ||
'userId' => $userId, | ||
], | ||
); | ||
} | ||
} | ||
} |