diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 39216789b3..94c4948f3d 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -5,6 +5,7 @@ /** * @author Christoph Wurst * @author Thomas Müller + * @author Richard Steinmetz * * Mail * @@ -35,7 +36,9 @@ use OCA\Mail\Contracts\ITrustedSenderService; use OCA\Mail\Contracts\IUserPreferences; use OCA\Mail\Dashboard\ImportantMailWidget; +use OCA\Mail\Dashboard\ImportantMailWidgetV2; use OCA\Mail\Dashboard\UnreadMailWidget; +use OCA\Mail\Dashboard\UnreadMailWidgetV2; use OCA\Mail\Events\BeforeImapClientCreated; use OCA\Mail\Events\BeforeMessageSentEvent; use OCA\Mail\Events\DraftMessageCreatedEvent; @@ -81,6 +84,7 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Dashboard\IAPIWidgetV2; use OCP\IServerContainer; use OCP\User\Events\UserDeletedEvent; use OCP\Util; @@ -138,8 +142,14 @@ public function register(IRegistrationContext $context): void { $context->registerMiddleWare(ErrorMiddleware::class); $context->registerMiddleWare(ProvisioningMiddleware::class); - $context->registerDashboardWidget(ImportantMailWidget::class); - $context->registerDashboardWidget(UnreadMailWidget::class); + if (interface_exists(IAPIWidgetV2::class)) { + $context->registerDashboardWidget(ImportantMailWidgetV2::class); + $context->registerDashboardWidget(UnreadMailWidgetV2::class); + } else { + $context->registerDashboardWidget(ImportantMailWidget::class); + $context->registerDashboardWidget(UnreadMailWidget::class); + } + $context->registerSearchProvider(Provider::class); $context->registerNotifierService(Notifier::class); diff --git a/lib/Dashboard/ImportantMailWidgetV2.php b/lib/Dashboard/ImportantMailWidgetV2.php new file mode 100644 index 0000000000..9eda75c87f --- /dev/null +++ b/lib/Dashboard/ImportantMailWidgetV2.php @@ -0,0 +1,53 @@ + + * + * @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\Mail\Dashboard; + +/** + * Requires Nextcloud >= 27.1.0 + */ +class ImportantMailWidgetV2 extends MailWidgetV2 { + /** + * @inheritDoc + */ + public function getId(): string { + return 'mail'; + } + + /** + * @inheritDoc + */ + public function getTitle(): string { + return $this->l10n->t('Important mail'); + } + + /** + * @inheritDoc + */ + public function getSearchFilter(): string { + return 'is:important'; + } +} diff --git a/lib/Dashboard/MailWidget.php b/lib/Dashboard/MailWidget.php index 124c2e34e0..676590b1cb 100644 --- a/lib/Dashboard/MailWidget.php +++ b/lib/Dashboard/MailWidget.php @@ -45,12 +45,12 @@ use OCP\Util; abstract class MailWidget implements IAPIWidget, IIconWidget, IOptionWidget { - private IURLGenerator $urlGenerator; - private IUserManager $userManager; - private AccountService $accountService; - private IMailSearch $mailSearch; - private IInitialState $initialState; - private ?string $userId; + protected IURLGenerator $urlGenerator; + protected IUserManager $userManager; + protected AccountService $accountService; + protected IMailSearch $mailSearch; + protected IInitialState $initialState; + protected ?string $userId; protected IL10N $l10n; public function __construct(IL10N $l10n, diff --git a/lib/Dashboard/MailWidgetV2.php b/lib/Dashboard/MailWidgetV2.php new file mode 100644 index 0000000000..7139e73bc5 --- /dev/null +++ b/lib/Dashboard/MailWidgetV2.php @@ -0,0 +1,76 @@ + + * + * @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\Mail\Dashboard; + +use OCP\Dashboard\IAPIWidgetV2; +use OCP\Dashboard\IButtonWidget; +use OCP\Dashboard\Model\WidgetButton; +use OCP\Dashboard\Model\WidgetItems; + +/** + * Requires Nextcloud >= 27.1.0 + */ +abstract class MailWidgetV2 extends MailWidget implements IAPIWidgetV2, IButtonWidget { + + /** + * @inheritDoc + */ + public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems { + $items = $this->getItems($userId, $since, $limit); + return new WidgetItems( + $items, + empty($items) ? $this->l10n->t('No message found yet') : '', + ); + } + + /** + * @inheritDoc + */ + public function load(): void { + // No assets need to be loaded anymore as the widget is rendered from the API + } + + /** + * @inheritDoc + */ + public function getWidgetButtons(string $userId): array { + $buttons = []; + + if ($this->userId !== null) { + $accounts = $this->accountService->findByUserId($this->userId); + if (empty($accounts)) { + $buttons[] = new WidgetButton( + WidgetButton::TYPE_SETUP, + $this->urlGenerator->linkToRouteAbsolute('mail.page.setup'), + $this->l10n->t('Set up an account'), + ); + } + } + + return $buttons; + } +} diff --git a/lib/Dashboard/UnreadMailWidgetV2.php b/lib/Dashboard/UnreadMailWidgetV2.php new file mode 100644 index 0000000000..3a934ae4e9 --- /dev/null +++ b/lib/Dashboard/UnreadMailWidgetV2.php @@ -0,0 +1,53 @@ + + * + * @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\Mail\Dashboard; + +/** + * Requires Nextcloud >= 27.1.0 + */ +class UnreadMailWidgetV2 extends MailWidgetV2 { + /** + * @inheritDoc + */ + public function getId(): string { + return 'mail-unread'; + } + + /** + * @inheritDoc + */ + public function getTitle(): string { + return $this->l10n->t('Unread mail'); + } + + /** + * @inheritDoc + */ + public function getSearchFilter(): string { + return 'is:unread'; + } +} diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index d4fbe557b3..fa697a1c2a 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,5 +1,14 @@ - + + + + ImportantMailWidgetV2 + UnreadMailWidgetV2 + + + IAPIWidgetV2 + + _data[$mailbox]]]> @@ -13,9 +22,19 @@ _slicemap[$mailbox]]]> - + + + MailWidgetV2 + + + - AbstractRector + IAPIWidgetV2 + + + MailWidgetV2 + +