Skip to content

Commit

Permalink
fixup! feat: create example event when a user logs in for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
st3iny committed Jan 23, 2025
1 parent c2e3090 commit bb61fc9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 47 deletions.
4 changes: 0 additions & 4 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
['name' => 'contact#getContactGroupMembers', 'url' => '/v1/autocompletion/groupmembers', 'verb' => 'POST'],
// Settings
['name' => 'settings#setConfig', 'url' => '/v1/config/{key}', 'verb' => 'POST'],
// Admin settings
['name' => 'exampleEvent#setCreateExampleEvent', 'url' => '/v1/exampleEvent/enable', 'verb' => 'POST'],
['name' => 'exampleEvent#uploadExampleEvent', 'url' => '/v1/exampleEvent/event', 'verb' => 'POST'],
['name' => 'exampleEvent#deleteExampleEvent', 'url' => '/v1/exampleEvent/event', 'verb' => 'DELETE'],
// Tools
['name' => 'email#sendEmailPublicLink', 'url' => '/v1/public/sendmail', 'verb' => 'POST'],
],
Expand Down
4 changes: 2 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use OCA\Calendar\Events\BeforeAppointmentBookedEvent;
use OCA\Calendar\Listener\AppointmentBookedListener;
use OCA\Calendar\Listener\CalendarReferenceListener;
use OCA\Calendar\Listener\CreateExampleEventListener;
use OCA\Calendar\Listener\UserFirstLoginListener;
use OCA\Calendar\Listener\UserDeletedListener;
use OCA\Calendar\Notification\Notifier;
use OCA\Calendar\Profile\AppointmentsAction;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeAppointmentBookedEvent::class, AppointmentBookedListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(RenderReferenceEvent::class, CalendarReferenceListener::class);
$context->registerEventListener(UserFirstTimeLoggedInEvent::class, CreateExampleEventListener::class);
$context->registerEventListener(UserFirstTimeLoggedInEvent::class, UserFirstLoginListener::class);

$context->registerNotifierService(Notifier::class);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Controller/ExampleEventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Calendar\Service\ExampleEventService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\IRequest;

class ExampleEventController extends Controller {
Expand All @@ -24,11 +25,13 @@ public function __construct(
parent::__construct(Application::APP_ID, $request);
}

#[FrontpageRoute(verb: 'POST', url: '/v1/exampleEvent/enable')]
public function setCreateExampleEvent(bool $enable): JSONResponse {
$this->exampleEventService->setCreateExampleEvent($enable);
return JsonResponse::success([]);
}

#[FrontpageRoute(verb: 'POST', url: '/v1/exampleEvent/event')]
public function uploadExampleEvent(string $ics): JSONResponse {
if (!$this->exampleEventService->shouldCreateExampleEvent()) {
return JSONResponse::fail([], Http::STATUS_FORBIDDEN);
Expand All @@ -38,6 +41,7 @@ public function uploadExampleEvent(string $ics): JSONResponse {
return JsonResponse::success([]);
}

#[FrontpageRoute(verb: 'DELETE', url: '/v1/exampleEvent/event')]
public function deleteExampleEvent(): JSONResponse {
$this->exampleEventService->deleteCustomExampleEvent();
return JsonResponse::success([]);
Expand Down
41 changes: 0 additions & 41 deletions lib/Listener/CreateExampleEventListener.php

This file was deleted.

73 changes: 73 additions & 0 deletions lib/Listener/UserFirstLoginListener.php
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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

UndefinedDocblockClass

lib/Listener/UserFirstLoginListener.php:38:4: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\ServerVersion does not exist (see https://psalm.dev/200)

Check failure on line 38 in lib/Listener/UserFirstLoginListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

UndefinedClass

lib/Listener/UserFirstLoginListener.php:38:37: UndefinedClass: Class, interface or enum named OCP\ServerVersion does not exist (see https://psalm.dev/019)
} catch (ContainerExceptionInterface $e) {
return false;
}

return $serverVersion->getMajorVersion() >= 31;

Check failure on line 43 in lib/Listener/UserFirstLoginListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

UndefinedDocblockClass

lib/Listener/UserFirstLoginListener.php:43:10: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\ServerVersion does not exist (see https://psalm.dev/200)
}

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,
],
);
}
}
}

0 comments on commit bb61fc9

Please sign in to comment.