Skip to content

Commit

Permalink
perf(dashboard): implement widget item api v2
Browse files Browse the repository at this point in the history
The old, bundled widget has to stay until we exclusively support the
corresponding server versions.

Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
  • Loading branch information
st3iny committed Aug 18, 2023
1 parent 79a24df commit 757d948
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Calendar App
*
* @author Georg Ehrke
* @author Richard Steinmetz <richard@steinmetz.cloud>
* @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
*
* This library is free software; you can redistribute it and/or
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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)) {

Check failure on line 58 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/AppInfo/Application.php:58:24: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 58 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedClass

lib/AppInfo/Application.php:58:24: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 58 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedClass

lib/AppInfo/Application.php:58:24: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 58 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable25

UndefinedClass

lib/AppInfo/Application.php:58:24: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)
$context->registerDashboardWidget(CalendarWidgetV2::class);

Check failure on line 59 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

MissingDependency

lib/AppInfo/Application.php:59:38: MissingDependency: OCA\Calendar\Dashboard\CalendarWidgetV2 depends on class or interface ocp\dashboard\iapiwidgetv2 that does not exist (see https://psalm.dev/157)

Check failure on line 59 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

MissingDependency

lib/AppInfo/Application.php:59:38: MissingDependency: OCA\Calendar\Dashboard\CalendarWidgetV2 depends on class or interface ocp\dashboard\iapiwidgetv2 that does not exist (see https://psalm.dev/157)

Check failure on line 59 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

MissingDependency

lib/AppInfo/Application.php:59:38: MissingDependency: OCA\Calendar\Dashboard\CalendarWidgetV2 depends on class or interface ocp\dashboard\iapiwidgetv2 that does not exist (see https://psalm.dev/157)

Check failure on line 59 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable25

MissingDependency

lib/AppInfo/Application.php:59:38: MissingDependency: OCA\Calendar\Dashboard\CalendarWidgetV2 depends on class or interface ocp\dashboard\iapiwidgetv2 that does not exist (see https://psalm.dev/157)
} else {
$context->registerDashboardWidget(CalendarWidget::class);
}

// TODO: drop conditional code when the app is 23+
if (method_exists($context, 'registerProfileLinkAction')) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Calendar App
*
* @author Georg Ehrke
* @author Richard Steinmetz <richard@steinmetz.cloud>
* @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
* @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
*
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions lib/Dashboard/CalendarWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -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.
Expand Down
81 changes: 81 additions & 0 deletions lib/Dashboard/CalendarWidgetV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
*
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @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\Calendar\Dashboard;

use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\IReloadableWidget;
use OCP\Dashboard\Model\WidgetItems;

/**
* Requires Nextcloud >= 27.1.0
*/
class CalendarWidgetV2 extends CalendarWidget implements IAPIWidgetV2, IReloadableWidget {

Check failure on line 36 in lib/Dashboard/CalendarWidgetV2.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/Dashboard/CalendarWidgetV2.php:36:58: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 36 in lib/Dashboard/CalendarWidgetV2.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedClass

lib/Dashboard/CalendarWidgetV2.php:36:58: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 36 in lib/Dashboard/CalendarWidgetV2.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedClass

lib/Dashboard/CalendarWidgetV2.php:36:58: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

Check failure on line 36 in lib/Dashboard/CalendarWidgetV2.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable25

UndefinedClass

lib/Dashboard/CalendarWidgetV2.php:36:58: UndefinedClass: Class, interface or enum named OCP\Dashboard\IAPIWidgetV2 does not exist (see https://psalm.dev/019)

/**
* @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 getReloadInterval(): int {
return 60;
}

/**
* @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';
}
}

0 comments on commit 757d948

Please sign in to comment.