From f53eda501bb71be42dfb19de9877b6ff5d08edcf Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 29 Aug 2022 16:14:48 +0200 Subject: [PATCH 1/4] implement client dashboard api --- lib/AppInfo/DashboardWidget.php | 39 +++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/AppInfo/DashboardWidget.php b/lib/AppInfo/DashboardWidget.php index f87b2c71f..c80665717 100644 --- a/lib/AppInfo/DashboardWidget.php +++ b/lib/AppInfo/DashboardWidget.php @@ -4,20 +4,28 @@ namespace OCA\Notes\AppInfo; +use OCA\Notes\Service\Note; +use OCA\Notes\Service\NotesService; +use OCP\Dashboard\IAPIWidget; +use OCP\Dashboard\IButtonWidget; use OCP\Dashboard\IWidget; +use OCP\Dashboard\Model\WidgetItem; use OCP\IL10N; use OCP\IURLGenerator; -class DashboardWidget implements IWidget { +class DashboardWidget implements IWidget, IButtonWidget, IAPIWidget { private IURLGenerator $url; private IL10N $l10n; + private NotesService $notesService; public function __construct( IURLGenerator $url, - IL10N $l10n + IL10N $l10n, + NotesService $notesService ) { $this->url = $url; $this->l10n = $l10n; + $this->notesService = $notesService; } /** @@ -61,4 +69,31 @@ public function getUrl(): ?string { public function load(): void { \OCP\Util::addScript('notes', 'notes-dashboard'); } + + public function getButtonUrl(): string { + return $this->url->linkToRouteAbsolute('notes.page.create'); + } + + public function getButtonIconUrl(): ?string { + return null; + } + + public function getButtonText(): string { + return $this->l10n->t('New notes'); + } + + public function getItems(string $userId, ?string $since = null, int $limit = 7): array { + $notes = $this->notesService->getTopNotes($userId); + $notes = array_slice($notes, 0, $limit); + return array_map(function (Note $note) { + $excerpt = ''; + try { + $excerpt = $note->getExcerpt(); + } catch (\Throwable $e) { + } + $link = $this->url->linkToRouteAbsolute('notes.page.index', ['id' => $note->getId()]); + $icon = $note->getFavorite() ? $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')) : ''; + return new WidgetItem($note->getTitle(), $excerpt, $link, $icon, (string)$note->getModified()); + }, $notes); + } } From 883663fe386c36cded5caa945f4ab3f0ffab4473 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 1 Sep 2022 17:11:53 +0200 Subject: [PATCH 2/4] adjust to updated button api --- lib/AppInfo/DashboardWidget.php | 19 +++++++++---------- lib/Service/NotesService.php | 11 +++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/AppInfo/DashboardWidget.php b/lib/AppInfo/DashboardWidget.php index c80665717..ef9a1e383 100644 --- a/lib/AppInfo/DashboardWidget.php +++ b/lib/AppInfo/DashboardWidget.php @@ -9,6 +9,7 @@ use OCP\Dashboard\IAPIWidget; use OCP\Dashboard\IButtonWidget; use OCP\Dashboard\IWidget; +use OCP\Dashboard\Model\WidgetButton; use OCP\Dashboard\Model\WidgetItem; use OCP\IL10N; use OCP\IURLGenerator; @@ -70,16 +71,14 @@ public function load(): void { \OCP\Util::addScript('notes', 'notes-dashboard'); } - public function getButtonUrl(): string { - return $this->url->linkToRouteAbsolute('notes.page.create'); - } - - public function getButtonIconUrl(): ?string { - return null; - } - - public function getButtonText(): string { - return $this->l10n->t('New notes'); + public function getWidgetButtons(string $userId): array { + $buttons = [ + new WidgetButton(WidgetButton::TYPE_NEW, $this->url->linkToRouteAbsolute('notes.page.create'), $this->l10n->t('Create new note')) + ]; + if ($this->notesService->countNotes($userId) > 7) { + $buttons[] = new WidgetButton(WidgetButton::TYPE_MORE, $this->url->linkToRouteAbsolute('notes.page.index'), $this->l10n->t('More notes')); + } + return $buttons; } public function getItems(string $userId, ?string $since = null, int $limit = 7): array { diff --git a/lib/Service/NotesService.php b/lib/Service/NotesService.php index f5696ca7d..d691e447c 100644 --- a/lib/Service/NotesService.php +++ b/lib/Service/NotesService.php @@ -56,6 +56,17 @@ public function getTopNotes(string $userId) : array { return $notes; } + public function countNotes(string $userId) : int { + $customExtension = $this->getCustomExtension($userId); + try { + $notesFolder = $this->getNotesFolder($userId, false); + $data = self::gatherNoteFiles($customExtension, $notesFolder); + return count($data['files']); + } catch (NotesFolderException $e) { + return 0; + } + } + public function get(string $userId, int $id) : Note { $customExtension = $this->getCustomExtension($userId); $notesFolder = $this->getNotesFolder($userId); From e08d9f771ffb1a85661abf144db061b23659dac9 Mon Sep 17 00:00:00 2001 From: Tobias Kaminsky Date: Mon, 5 Sep 2022 09:22:04 +0200 Subject: [PATCH 3/4] Update lib/AppInfo/DashboardWidget.php Co-authored-by: korelstar Signed-off-by: Tobias Kaminsky --- lib/AppInfo/DashboardWidget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/AppInfo/DashboardWidget.php b/lib/AppInfo/DashboardWidget.php index ef9a1e383..48ce88ab2 100644 --- a/lib/AppInfo/DashboardWidget.php +++ b/lib/AppInfo/DashboardWidget.php @@ -90,7 +90,7 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7): $excerpt = $note->getExcerpt(); } catch (\Throwable $e) { } - $link = $this->url->linkToRouteAbsolute('notes.page.index', ['id' => $note->getId()]); + $link = $this->url->linkToRouteAbsolute('notes.page.indexnote', ['id' => $note->getId()]); $icon = $note->getFavorite() ? $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')) : ''; return new WidgetItem($note->getTitle(), $excerpt, $link, $icon, (string)$note->getModified()); }, $notes); From 9e3f6657a7d0e9825e680fae1cd51146b49d3469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 3 Jan 2023 17:17:24 +0100 Subject: [PATCH 4/4] fix(widget): Implement icon handling for widget api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/AppInfo/DashboardWidget.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/AppInfo/DashboardWidget.php b/lib/AppInfo/DashboardWidget.php index 48ce88ab2..24c6058de 100644 --- a/lib/AppInfo/DashboardWidget.php +++ b/lib/AppInfo/DashboardWidget.php @@ -8,13 +8,14 @@ use OCA\Notes\Service\NotesService; use OCP\Dashboard\IAPIWidget; use OCP\Dashboard\IButtonWidget; +use OCP\Dashboard\IIconWidget; use OCP\Dashboard\IWidget; use OCP\Dashboard\Model\WidgetButton; use OCP\Dashboard\Model\WidgetItem; use OCP\IL10N; use OCP\IURLGenerator; -class DashboardWidget implements IWidget, IButtonWidget, IAPIWidget { +class DashboardWidget implements IWidget, IButtonWidget, IAPIWidget, IIconWidget { private IURLGenerator $url; private IL10N $l10n; private NotesService $notesService; @@ -91,8 +92,16 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7): } catch (\Throwable $e) { } $link = $this->url->linkToRouteAbsolute('notes.page.indexnote', ['id' => $note->getId()]); - $icon = $note->getFavorite() ? $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')) : ''; + $icon = $this->url->getAbsoluteURL( + $note->getFavorite() + ? $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')) + : $this->getIconUrl() + ); return new WidgetItem($note->getTitle(), $excerpt, $link, $icon, (string)$note->getModified()); }, $notes); } + + public function getIconUrl(): string { + return $this->url->getAbsoluteURL($this->url->imagePath('notes', 'notes-dark.svg')); + } }