Merge pull request #45435 from nextcloud/feat/dav/upcoming-events-api
feat(dav): Add an API for upcoming eventspull/47209/head
commit
7641e768b3
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\CalDAV;
|
||||
|
||||
use JsonSerializable;
|
||||
use OCA\DAV\ResponseDefinitions;
|
||||
|
||||
class UpcomingEvent implements JsonSerializable {
|
||||
public function __construct(private string $uri,
|
||||
private ?int $recurrenceId,
|
||||
private string $calendarUri,
|
||||
private ?int $start,
|
||||
private ?string $summary,
|
||||
private ?string $location,
|
||||
private ?string $calendarAppUrl) {
|
||||
}
|
||||
|
||||
public function getUri(): string {
|
||||
return $this->uri;
|
||||
}
|
||||
|
||||
public function getRecurrenceId(): ?int {
|
||||
return $this->recurrenceId;
|
||||
}
|
||||
|
||||
public function getCalendarUri(): string {
|
||||
return $this->calendarUri;
|
||||
}
|
||||
|
||||
public function getStart(): ?int {
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
public function getSummary(): ?string {
|
||||
return $this->summary;
|
||||
}
|
||||
|
||||
public function getLocation(): ?string {
|
||||
return $this->location;
|
||||
}
|
||||
|
||||
public function getCalendarAppUrl(): ?string {
|
||||
return $this->calendarAppUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ResponseDefinitions
|
||||
*/
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'uri' => $this->uri,
|
||||
'recurrenceId' => $this->recurrenceId,
|
||||
'calendarUri' => $this->calendarUri,
|
||||
'start' => $this->start,
|
||||
'summary' => $this->summary,
|
||||
'location' => $this->location,
|
||||
'calendarAppUrl' => $this->calendarAppUrl,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\CalDAV;
|
||||
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\Calendar\IManager;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUserManager;
|
||||
use function array_map;
|
||||
|
||||
class UpcomingEventsService {
|
||||
public function __construct(private IManager $calendarManager,
|
||||
private ITimeFactory $timeFactory,
|
||||
private IUserManager $userManager,
|
||||
private IAppManager $appManager,
|
||||
private IURLGenerator $urlGenerator) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UpcomingEvent[]
|
||||
*/
|
||||
public function getEvents(string $userId, ?string $location = null): array {
|
||||
$searchQuery = $this->calendarManager->newQuery('principals/users/' . $userId);
|
||||
if ($location !== null) {
|
||||
$searchQuery->addSearchProperty('LOCATION');
|
||||
$searchQuery->setSearchPattern($location);
|
||||
}
|
||||
$searchQuery->addType('VEVENT');
|
||||
$searchQuery->setLimit(3);
|
||||
$now = $this->timeFactory->now();
|
||||
$searchQuery->setTimerangeStart($now->modify('-1 minute'));
|
||||
$searchQuery->setTimerangeEnd($now->modify('+1 month'));
|
||||
|
||||
$events = $this->calendarManager->searchForPrincipal($searchQuery);
|
||||
$calendarAppEnabled = $this->appManager->isEnabledForUser(
|
||||
'calendar',
|
||||
$this->userManager->get($userId),
|
||||
);
|
||||
|
||||
return array_map(fn (array $event) => new UpcomingEvent(
|
||||
$event['uri'],
|
||||
($event['objects'][0]['RECURRENCE-ID'][0] ?? null)?->getTimeStamp(),
|
||||
$event['calendar-uri'],
|
||||
$event['objects'][0]['DTSTART'][0]?->getTimestamp(),
|
||||
$event['objects'][0]['SUMMARY'][0] ?? null,
|
||||
$event['objects'][0]['LOCATION'][0] ?? null,
|
||||
match ($calendarAppEnabled) {
|
||||
// TODO: create a named, deep route in calendar
|
||||
// TODO: it's a code smell to just assume this route exists, find an abstraction
|
||||
true => $this->urlGenerator->linkToRouteAbsolute('calendar.view.index'),
|
||||
false => null,
|
||||
},
|
||||
), $events);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Controller;
|
||||
|
||||
use OCA\DAV\AppInfo\Application;
|
||||
use OCA\DAV\CalDAV\UpcomingEvent;
|
||||
use OCA\DAV\CalDAV\UpcomingEventsService;
|
||||
use OCA\DAV\ResponseDefinitions;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\OCSController;
|
||||
use OCP\IRequest;
|
||||
|
||||
/**
|
||||
* @psalm-import-type DAVUpcomingEvent from ResponseDefinitions
|
||||
*/
|
||||
class UpcomingEventsController extends OCSController {
|
||||
private ?string $userId;
|
||||
private UpcomingEventsService $service;
|
||||
|
||||
public function __construct(
|
||||
IRequest $request,
|
||||
?string $userId,
|
||||
UpcomingEventsService $service) {
|
||||
parent::__construct(Application::APP_ID, $request);
|
||||
|
||||
$this->userId = $userId;
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about upcoming events
|
||||
*
|
||||
* @param string|null $location location/URL to filter by
|
||||
* @return DataResponse<Http::STATUS_OK, array{events: DAVUpcomingEvent[]}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, null, array{}>
|
||||
*
|
||||
* 200: Upcoming events
|
||||
* 401: When not authenticated
|
||||
*/
|
||||
#[NoAdminRequired]
|
||||
public function getEvents(?string $location = null): DataResponse {
|
||||
if ($this->userId === null) {
|
||||
return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return new DataResponse([
|
||||
'events' => array_map(fn (UpcomingEvent $e) => $e->jsonSerialize(), $this->service->getEvents(
|
||||
$this->userId,
|
||||
$location,
|
||||
)),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Tests\Unit\DAV\Service;
|
||||
|
||||
use OCA\DAV\CalDAV\UpcomingEvent;
|
||||
use OCA\DAV\CalDAV\UpcomingEventsService;
|
||||
use OCA\DAV\Controller\UpcomingEventsController;
|
||||
use OCP\IRequest;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UpcomingEventsControllerTest extends TestCase {
|
||||
|
||||
private IRequest|MockObject $request;
|
||||
private UpcomingEventsService|MockObject $service;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->request = $this->createMock(IRequest::class);
|
||||
$this->service = $this->createMock(UpcomingEventsService::class);
|
||||
}
|
||||
|
||||
public function testGetEventsAnonymously() {
|
||||
$controller = new UpcomingEventsController(
|
||||
$this->request,
|
||||
null,
|
||||
$this->service,
|
||||
);
|
||||
|
||||
$response = $controller->getEvents('https://cloud.example.com/call/123');
|
||||
|
||||
self::assertNull($response->getData());
|
||||
self::assertSame(401, $response->getStatus());
|
||||
}
|
||||
|
||||
public function testGetEventsByLocation() {
|
||||
$controller = new UpcomingEventsController(
|
||||
$this->request,
|
||||
'u1',
|
||||
$this->service,
|
||||
);
|
||||
$this->service->expects(self::once())
|
||||
->method('getEvents')
|
||||
->with('u1', 'https://cloud.example.com/call/123')
|
||||
->willReturn([
|
||||
new UpcomingEvent(
|
||||
'abc-123',
|
||||
null,
|
||||
'personal',
|
||||
123,
|
||||
'Test',
|
||||
'https://cloud.example.com/call/123',
|
||||
null,
|
||||
),
|
||||
]);
|
||||
|
||||
$response = $controller->getEvents('https://cloud.example.com/call/123');
|
||||
|
||||
self::assertNotNull($response->getData());
|
||||
self::assertIsArray($response->getData());
|
||||
self::assertCount(1, $response->getData()['events']);
|
||||
self::assertSame(200, $response->getStatus());
|
||||
$event1 = $response->getData()['events'][0];
|
||||
self::assertEquals('abc-123', $event1['uri']);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Tests\Unit\DAV\Service;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use OCA\DAV\CalDAV\UpcomingEventsService;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\Calendar\ICalendarQuery;
|
||||
use OCP\Calendar\IManager;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUserManager;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UpcomingEventsServiceTest extends TestCase {
|
||||
|
||||
private MockObject|IManager $calendarManager;
|
||||
private ITimeFactory|MockObject $timeFactory;
|
||||
private IUserManager|MockObject $userManager;
|
||||
private IAppManager|MockObject $appManager;
|
||||
private IURLGenerator|MockObject $urlGenerator;
|
||||
private UpcomingEventsService $service;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->calendarManager = $this->createMock(IManager::class);
|
||||
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->appManager = $this->createMock(IAppManager::class);
|
||||
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||
|
||||
$this->service = new UpcomingEventsService(
|
||||
$this->calendarManager,
|
||||
$this->timeFactory,
|
||||
$this->userManager,
|
||||
$this->appManager,
|
||||
$this->urlGenerator,
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetEventsByLocation(): void {
|
||||
$now = new DateTimeImmutable('2024-07-08T18:20:20Z');
|
||||
$this->timeFactory->method('now')
|
||||
->willReturn($now);
|
||||
$query = $this->createMock(ICalendarQuery::class);
|
||||
$this->appManager->method('isEnabledForUser')->willReturn(false);
|
||||
$this->calendarManager->method('newQuery')
|
||||
->with('principals/users/user1')
|
||||
->willReturn($query);
|
||||
$query->expects(self::once())
|
||||
->method('addSearchProperty')
|
||||
->with('LOCATION');
|
||||
$query->expects(self::once())
|
||||
->method('setSearchPattern')
|
||||
->with('https://cloud.example.com/call/123');
|
||||
$this->calendarManager->expects(self::once())
|
||||
->method('searchForPrincipal')
|
||||
->with($query)
|
||||
->willReturn([
|
||||
[
|
||||
'uri' => 'ev1',
|
||||
'calendar-key' => '1',
|
||||
'calendar-uri' => 'personal',
|
||||
'objects' => [
|
||||
0 => [
|
||||
'DTSTART' => [
|
||||
new DateTimeImmutable('now'),
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$events = $this->service->getEvents('user1', 'https://cloud.example.com/call/123');
|
||||
|
||||
self::assertCount(1, $events);
|
||||
$event1 = $events[0];
|
||||
self::assertEquals('ev1', $event1->getUri());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue