Skip to content

Commit 341e377

Browse files
committed
feat: added new FavouriteWidget to display favorite files in dashboard widget
Signed-off-by: yemkareems <yemkareems@gmail.com>
1 parent 659cd12 commit 341e377

File tree

4 files changed

+163
-3
lines changed

4 files changed

+163
-3
lines changed

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'OCA\\Files\\Controller\\TemplateController' => $baseDir . '/../lib/Controller/TemplateController.php',
4949
'OCA\\Files\\Controller\\TransferOwnershipController' => $baseDir . '/../lib/Controller/TransferOwnershipController.php',
5050
'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php',
51+
'OCA\\Files\\Dashboard\\FavouriteWidget' => $baseDir . '/../lib/Dashboard/FavouriteWidet.php',
5152
'OCA\\Files\\Db\\OpenLocalEditor' => $baseDir . '/../lib/Db/OpenLocalEditor.php',
5253
'OCA\\Files\\Db\\OpenLocalEditorMapper' => $baseDir . '/../lib/Db/OpenLocalEditorMapper.php',
5354
'OCA\\Files\\Db\\TransferOwnership' => $baseDir . '/../lib/Db/TransferOwnership.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
class ComposerStaticInitFiles
88
{
99
public static $prefixLengthsPsr4 = array (
10-
'O' =>
10+
'O' =>
1111
array (
1212
'OCA\\Files\\' => 10,
1313
),
1414
);
1515

1616
public static $prefixDirsPsr4 = array (
17-
'OCA\\Files\\' =>
17+
'OCA\\Files\\' =>
1818
array (
1919
0 => __DIR__ . '/..' . '/../lib',
2020
),
@@ -63,7 +63,7 @@ class ComposerStaticInitFiles
6363
'OCA\\Files\\Controller\\TemplateController' => __DIR__ . '/..' . '/../lib/Controller/TemplateController.php',
6464
'OCA\\Files\\Controller\\TransferOwnershipController' => __DIR__ . '/..' . '/../lib/Controller/TransferOwnershipController.php',
6565
'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php',
66-
'OCA\\Files\\Db\\OpenLocalEditor' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditor.php',
66+
'OCA\\Files\\Dashboard\\FavouriteWidget' => __DIR__ . '/..' . '/../lib/Dashboard/FavouriteWidget.php', 'OCA\\Files\\Db\\OpenLocalEditor' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditor.php',
6767
'OCA\\Files\\Db\\OpenLocalEditorMapper' => __DIR__ . '/..' . '/../lib/Db/OpenLocalEditorMapper.php',
6868
'OCA\\Files\\Db\\TransferOwnership' => __DIR__ . '/..' . '/../lib/Db/TransferOwnership.php',
6969
'OCA\\Files\\Db\\TransferOwnershipMapper' => __DIR__ . '/..' . '/../lib/Db/TransferOwnershipMapper.php',

apps/files/lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Files\Collaboration\Resources\Listener;
1414
use OCA\Files\Collaboration\Resources\ResourceProvider;
1515
use OCA\Files\Controller\ApiController;
16+
use OCA\Files\Dashboard\FavouriteWidget;
1617
use OCA\Files\DirectEditingCapabilities;
1718
use OCA\Files\Event\LoadSearchPlugins;
1819
use OCA\Files\Event\LoadSidebar;
@@ -120,6 +121,7 @@ public function register(IRegistrationContext $context): void {
120121
$context->registerSearchProvider(FilesSearchProvider::class);
121122

122123
$context->registerNotifierService(Notifier::class);
124+
$context->registerDashboardWidget(FavouriteWidget::class);
123125
}
124126

125127
public function boot(IBootContext $context): void {
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files\Dashboard;
11+
12+
use OCA\Files\AppInfo\Application;
13+
use OCP\Dashboard\IAPIWidget;
14+
use OCP\Dashboard\IAPIWidgetV2;
15+
use OCP\Dashboard\IButtonWidget;
16+
use OCP\Dashboard\IIconWidget;
17+
use OCP\Dashboard\IWidget;
18+
use OCP\Dashboard\Model\WidgetButton;
19+
use OCP\Dashboard\Model\WidgetItem;
20+
use OCP\Dashboard\Model\WidgetItems;
21+
use OCP\Files\IMimeTypeDetector;
22+
use OCP\Files\IRootFolder;
23+
use OCP\IL10N;
24+
use OCP\IPreview;
25+
use OCP\ITagManager;
26+
use OCP\IURLGenerator;
27+
use OCP\IUserManager;
28+
use OCP\IUserSession;
29+
30+
class FavouriteWidget implements IWidget, IIconWidget, IAPIWidget, IAPIWidgetV2, IButtonWidget {
31+
private IUserSession $userSession;
32+
private IL10N $l10n;
33+
private IURLGenerator $urlGenerator;
34+
private IMimeTypeDetector $mimeTypeDetector;
35+
private IUserManager $userManager;
36+
private ITagManager $tagManager;
37+
private IRootFolder $rootFolder;
38+
private IPreview $previewManager;
39+
public const FAVORITE_LIMIT = 50;
40+
41+
public function __construct(
42+
IUserSession $userSession,
43+
IL10N $l10n,
44+
IURLGenerator $urlGenerator,
45+
IMimeTypeDetector $mimeTypeDetector,
46+
IUserManager $userManager,
47+
ITagManager $tagManager,
48+
IRootFolder $rootFolder,
49+
IPreview $previewManager,
50+
) {
51+
$this->userSession = $userSession;
52+
$this->l10n = $l10n;
53+
$this->urlGenerator = $urlGenerator;
54+
$this->mimeTypeDetector = $mimeTypeDetector;
55+
$this->userManager = $userManager;
56+
$this->tagManager = $tagManager;
57+
$this->rootFolder = $rootFolder;
58+
$this->previewManager = $previewManager;
59+
}
60+
61+
public function getId(): string {
62+
return Application::APP_ID;
63+
}
64+
65+
public function getTitle(): string {
66+
return $this->l10n->t('Favorites');
67+
}
68+
69+
public function getOrder(): int {
70+
return 0;
71+
}
72+
73+
public function getIconClass(): string {
74+
return 'icon-files-dark';
75+
}
76+
77+
public function getIconUrl(): string {
78+
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'app-dark.svg'));
79+
}
80+
81+
public function getUrl(): ?string {
82+
return null;
83+
}
84+
85+
public function load(): void {
86+
$user = $this->userSession->getUser();
87+
if ($user === null) {
88+
return;
89+
}
90+
return;
91+
//Util::addScript(Application::APP_ID, 'recommendations-dashboard');
92+
}
93+
94+
public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
95+
$user = $this->userManager->get($userId);
96+
97+
if (!$user) {
98+
return [];
99+
}
100+
$tags = $this->tagManager->load('files', [], false, $userId);
101+
$favorites = $tags->getFavorites();
102+
if (empty($favorites)) {
103+
return [];
104+
} elseif (isset($favorites[self::FAVORITE_LIMIT])) {
105+
return [];
106+
}
107+
$favoriteNodes = [];
108+
$userFolder = $this->rootFolder->getUserFolder($userId);
109+
foreach ($favorites as $favorite) {
110+
$node = $userFolder->getFirstNodeById($favorite);
111+
if ($node) {
112+
$url = $this->urlGenerator->linkToRouteAbsolute(
113+
'files.viewcontroller.showFile', ['fileid' => $node->getId()]
114+
);
115+
if ($this->previewManager->isAvailable($node)) {
116+
$icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [
117+
'x' => 256,
118+
'y' => 256,
119+
'fileId' => $node->getId(),
120+
'c' => $node->getEtag(),
121+
]);
122+
} else {
123+
$icon = $this->urlGenerator->getAbsoluteURL(
124+
$this->mimeTypeDetector->mimeTypeIcon($node->getMimetype())
125+
);
126+
}
127+
$favoriteNodes[] = new WidgetItem(
128+
$node->getName(),
129+
'',
130+
$url,
131+
$icon,
132+
(string)$node->getCreationTime()
133+
);
134+
}
135+
}
136+
137+
return $favoriteNodes;
138+
}
139+
140+
public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
141+
$items = $this->getItems($userId, $since, $limit);
142+
return new WidgetItems(
143+
$items,
144+
count($items) === 0 ? $this->l10n->t('No favorites') : '',
145+
);
146+
}
147+
148+
public function getWidgetButtons(string $userId): array {
149+
return [
150+
new WidgetButton(
151+
WidgetButton::TYPE_MORE,
152+
$this->urlGenerator->getAbsoluteURL('index.php/apps/files/favorites'),
153+
$this->l10n->t('More favorites')
154+
),
155+
];
156+
}
157+
}

0 commit comments

Comments
 (0)