From 341e377ee2dfd996327cc7e9326a38c2304105a2 Mon Sep 17 00:00:00 2001 From: yemkareems Date: Thu, 28 Nov 2024 12:28:39 +0530 Subject: [PATCH] feat: added new FavouriteWidget to display favorite files in dashboard widget Signed-off-by: yemkareems --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 6 +- apps/files/lib/AppInfo/Application.php | 2 + apps/files/lib/Dashboard/FavouriteWidget.php | 157 ++++++++++++++++++ 4 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 apps/files/lib/Dashboard/FavouriteWidget.php diff --git a/apps/files/composer/composer/autoload_classmap.php b/apps/files/composer/composer/autoload_classmap.php index e9e45bbd84abc..86eab376ab0ad 100644 --- a/apps/files/composer/composer/autoload_classmap.php +++ b/apps/files/composer/composer/autoload_classmap.php @@ -48,6 +48,7 @@ 'OCA\\Files\\Controller\\TemplateController' => $baseDir . '/../lib/Controller/TemplateController.php', 'OCA\\Files\\Controller\\TransferOwnershipController' => $baseDir . '/../lib/Controller/TransferOwnershipController.php', 'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php', + 'OCA\\Files\\Dashboard\\FavouriteWidget' => $baseDir . '/../lib/Dashboard/FavouriteWidet.php', 'OCA\\Files\\Db\\OpenLocalEditor' => $baseDir . '/../lib/Db/OpenLocalEditor.php', 'OCA\\Files\\Db\\OpenLocalEditorMapper' => $baseDir . '/../lib/Db/OpenLocalEditorMapper.php', 'OCA\\Files\\Db\\TransferOwnership' => $baseDir . '/../lib/Db/TransferOwnership.php', diff --git a/apps/files/composer/composer/autoload_static.php b/apps/files/composer/composer/autoload_static.php index 59695e60fc792..f191cd97c7c3a 100644 --- a/apps/files/composer/composer/autoload_static.php +++ b/apps/files/composer/composer/autoload_static.php @@ -7,14 +7,14 @@ class ComposerStaticInitFiles { public static $prefixLengthsPsr4 = array ( - 'O' => + 'O' => array ( 'OCA\\Files\\' => 10, ), ); public static $prefixDirsPsr4 = array ( - 'OCA\\Files\\' => + 'OCA\\Files\\' => array ( 0 => __DIR__ . '/..' . '/../lib', ), @@ -63,7 +63,7 @@ class ComposerStaticInitFiles 'OCA\\Files\\Controller\\TemplateController' => __DIR__ . '/..' . '/../lib/Controller/TemplateController.php', 'OCA\\Files\\Controller\\TransferOwnershipController' => __DIR__ . '/..' . '/../lib/Controller/TransferOwnershipController.php', 'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php', - 'OCA\\Files\\Db\\OpenLocalEditor' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditor.php', + 'OCA\\Files\\Dashboard\\FavouriteWidget' => __DIR__ . '/..' . '/../lib/Dashboard/FavouriteWidget.php', 'OCA\\Files\\Db\\OpenLocalEditor' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditor.php', 'OCA\\Files\\Db\\OpenLocalEditorMapper' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditorMapper.php', 'OCA\\Files\\Db\\TransferOwnership' => __DIR__ . '/..' . '/../lib/Db/TransferOwnership.php', 'OCA\\Files\\Db\\TransferOwnershipMapper' => __DIR__ . '/..' . '/../lib/Db/TransferOwnershipMapper.php', diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index d50a8d14f5ae9..1115b3394657b 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -13,6 +13,7 @@ use OCA\Files\Collaboration\Resources\Listener; use OCA\Files\Collaboration\Resources\ResourceProvider; use OCA\Files\Controller\ApiController; +use OCA\Files\Dashboard\FavouriteWidget; use OCA\Files\DirectEditingCapabilities; use OCA\Files\Event\LoadSearchPlugins; use OCA\Files\Event\LoadSidebar; @@ -120,6 +121,7 @@ public function register(IRegistrationContext $context): void { $context->registerSearchProvider(FilesSearchProvider::class); $context->registerNotifierService(Notifier::class); + $context->registerDashboardWidget(FavouriteWidget::class); } public function boot(IBootContext $context): void { diff --git a/apps/files/lib/Dashboard/FavouriteWidget.php b/apps/files/lib/Dashboard/FavouriteWidget.php new file mode 100644 index 0000000000000..8236f4c52c14f --- /dev/null +++ b/apps/files/lib/Dashboard/FavouriteWidget.php @@ -0,0 +1,157 @@ +userSession = $userSession; + $this->l10n = $l10n; + $this->urlGenerator = $urlGenerator; + $this->mimeTypeDetector = $mimeTypeDetector; + $this->userManager = $userManager; + $this->tagManager = $tagManager; + $this->rootFolder = $rootFolder; + $this->previewManager = $previewManager; + } + + public function getId(): string { + return Application::APP_ID; + } + + public function getTitle(): string { + return $this->l10n->t('Favorites'); + } + + public function getOrder(): int { + return 0; + } + + public function getIconClass(): string { + return 'icon-files-dark'; + } + + public function getIconUrl(): string { + return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'app-dark.svg')); + } + + public function getUrl(): ?string { + return null; + } + + public function load(): void { + $user = $this->userSession->getUser(); + if ($user === null) { + return; + } + return; + //Util::addScript(Application::APP_ID, 'recommendations-dashboard'); + } + + public function getItems(string $userId, ?string $since = null, int $limit = 7): array { + $user = $this->userManager->get($userId); + + if (!$user) { + return []; + } + $tags = $this->tagManager->load('files', [], false, $userId); + $favorites = $tags->getFavorites(); + if (empty($favorites)) { + return []; + } elseif (isset($favorites[self::FAVORITE_LIMIT])) { + return []; + } + $favoriteNodes = []; + $userFolder = $this->rootFolder->getUserFolder($userId); + foreach ($favorites as $favorite) { + $node = $userFolder->getFirstNodeById($favorite); + if ($node) { + $url = $this->urlGenerator->linkToRouteAbsolute( + 'files.viewcontroller.showFile', ['fileid' => $node->getId()] + ); + if ($this->previewManager->isAvailable($node)) { + $icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [ + 'x' => 256, + 'y' => 256, + 'fileId' => $node->getId(), + 'c' => $node->getEtag(), + ]); + } else { + $icon = $this->urlGenerator->getAbsoluteURL( + $this->mimeTypeDetector->mimeTypeIcon($node->getMimetype()) + ); + } + $favoriteNodes[] = new WidgetItem( + $node->getName(), + '', + $url, + $icon, + (string)$node->getCreationTime() + ); + } + } + + return $favoriteNodes; + } + + public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems { + $items = $this->getItems($userId, $since, $limit); + return new WidgetItems( + $items, + count($items) === 0 ? $this->l10n->t('No favorites') : '', + ); + } + + public function getWidgetButtons(string $userId): array { + return [ + new WidgetButton( + WidgetButton::TYPE_MORE, + $this->urlGenerator->getAbsoluteURL('index.php/apps/files/favorites'), + $this->l10n->t('More favorites') + ), + ]; + } +}