From 571c493aa3acb1995480571e492c94de541b6561 Mon Sep 17 00:00:00 2001 From: Richard Steinmetz Date: Thu, 17 Aug 2023 15:29:16 +0200 Subject: [PATCH] perf(dashboard): implement widget item api v2 The old, bundled widget has to stay until we exclusively support the corresponding server versions. Signed-off-by: Richard Steinmetz --- lib/AppInfo/Application.php | 10 +++- lib/Controller/ViewController.php | 3 ++ lib/Dashboard/CalendarWidget.php | 15 +++--- lib/Dashboard/CalendarWidgetV2.php | 73 ++++++++++++++++++++++++++++++ psalm.xml | 1 - tests/psalm-baseline.xml | 15 +++++- 6 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 lib/Dashboard/CalendarWidgetV2.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 8a21072f3e..c668e4acb1 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -5,6 +5,7 @@ * Calendar App * * @author Georg Ehrke + * @author Richard Steinmetz * @copyright 2019 Georg Ehrke * * This library is free software; you can redistribute it and/or @@ -24,6 +25,7 @@ namespace OCA\Calendar\AppInfo; use OCA\Calendar\Dashboard\CalendarWidget; +use OCA\Calendar\Dashboard\CalendarWidgetV2; use OCA\Calendar\Events\BeforeAppointmentBookedEvent; use OCA\Calendar\Listener\AppointmentBookedListener; use OCA\Calendar\Listener\UserDeletedListener; @@ -33,6 +35,7 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Dashboard\IAPIWidgetV2; use OCP\User\Events\UserDeletedEvent; use function method_exists; @@ -51,7 +54,12 @@ public function __construct(array $params = []) { * @inheritDoc */ public function register(IRegistrationContext $context): void { - $context->registerDashboardWidget(CalendarWidget::class); + // TODO: drop conditional code when the app is 27.1+ + if (interface_exists(IAPIWidgetV2::class)) { + $context->registerDashboardWidget(CalendarWidgetV2::class); + } else { + $context->registerDashboardWidget(CalendarWidget::class); + } // TODO: drop conditional code when the app is 23+ if (method_exists($context, 'registerProfileLinkAction')) { diff --git a/lib/Controller/ViewController.php b/lib/Controller/ViewController.php index f0afba9b71..fde305e5b2 100644 --- a/lib/Controller/ViewController.php +++ b/lib/Controller/ViewController.php @@ -5,6 +5,7 @@ * Calendar App * * @author Georg Ehrke + * @author Richard Steinmetz * @copyright 2019 Georg Ehrke * @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/ * @@ -201,6 +202,8 @@ public function getCalendarDotSvg(string $color = "#0082c9"): FileDisplayRespons } $file = $folder->newFile($color . '.svg', $svg); $response = new FileDisplayResponse($file); + // Some browsers won't render SVGs without content types (for security reasons) + $response->addHeader('Content-Type', 'image/svg+xml'); $response->cacheFor(24 * 3600); // 1 day return $response; } diff --git a/lib/Dashboard/CalendarWidget.php b/lib/Dashboard/CalendarWidget.php index 2fee8811e1..d0b6f3c31a 100644 --- a/lib/Dashboard/CalendarWidget.php +++ b/lib/Dashboard/CalendarWidget.php @@ -5,6 +5,7 @@ * @copyright Copyright (c) 2020 Julius Härtl * * @author Julius Härtl + * @author Richard Steinmetz * * @license GNU AGPL version 3 or any later version * @@ -46,13 +47,13 @@ use OCP\Util; class CalendarWidget implements IAPIWidget, IButtonWidget, IIconWidget, IOptionWidget { - private IL10N $l10n; - private IInitialState $initialStateService; - private JSDataService $dataService; - private IDateTimeFormatter $dateTimeFormatter; - private IURLGenerator $urlGenerator; - private IManager $calendarManager; - private ITimeFactory $timeFactory; + protected IL10N $l10n; + protected IInitialState $initialStateService; + protected JSDataService $dataService; + protected IDateTimeFormatter $dateTimeFormatter; + protected IURLGenerator $urlGenerator; + protected IManager $calendarManager; + protected ITimeFactory $timeFactory; /** * CalendarWidget constructor. diff --git a/lib/Dashboard/CalendarWidgetV2.php b/lib/Dashboard/CalendarWidgetV2.php new file mode 100644 index 0000000000..f6e84f8da5 --- /dev/null +++ b/lib/Dashboard/CalendarWidgetV2.php @@ -0,0 +1,73 @@ + + * + * @author Richard Steinmetz + * + * @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 . + * + */ + +namespace OCA\Calendar\Dashboard; + +use OCP\Dashboard\IAPIWidgetV2; +use OCP\Dashboard\Model\WidgetItems; + +/** + * Requires Nextcloud >= 27.1.0 + */ +class CalendarWidgetV2 extends CalendarWidget implements IAPIWidgetV2 { + + /** + * @inheritDoc + */ + public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems { + $widgetItems = $this->getItems($userId, $since, $limit); + + $halfEmptyContentMessage = ''; + if (!empty($widgetItems)) { + $startOfTomorrow = $this->timeFactory->getDateTime('tomorrow')->getTimestamp(); + foreach ($widgetItems as $item) { + if ((int)$item->getSinceId() >= $startOfTomorrow) { + $halfEmptyContentMessage = $this->l10n->t('No more events today'); + } + } + } + + return new WidgetItems( + $widgetItems, + empty($widgetItems) ? $this->l10n->t('No upcoming events') : '', + $halfEmptyContentMessage, + ); + } + + /** + * @inheritDoc + */ + public function load(): void { + // No assets need to be loaded anymore as the widget is rendered from the API + } + + /** + * @inheritDoc + */ + public function getIconClass(): string { + return 'icon-calendar-dark'; + } +} diff --git a/psalm.xml b/psalm.xml index bd7028d866..7f70f8020a 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,7 +5,6 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config" errorBaseline="tests/psalm-baseline.xml" - findUnusedBaselineEntry="true" findUnusedCode="false" > diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 01bbab5e73..7655a6ae66 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,11 +1,24 @@ - + + + + CalendarWidgetV2 + + + IAPIWidgetV2 + + $expectedDayKeys !== $actualDayKeys + + + IAPIWidgetV2 + + IEventListener