Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added new FavouriteWidget to display favorite files in dashboard widget #49534

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/files/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions apps/files/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class ComposerStaticInitFiles
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
{
public static $prefixLengthsPsr4 = array (
'O' =>
'O' =>
array (
'OCA\\Files\\' => 10,
),
);

public static $prefixDirsPsr4 = array (
'OCA\\Files\\' =>
'OCA\\Files\\' =>
array (
0 => __DIR__ . '/..' . '/../lib',
),
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
157 changes: 157 additions & 0 deletions apps/files/lib/Dashboard/FavouriteWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files\Dashboard;

use OCA\Files\AppInfo\Application;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IWidget;
use OCP\Dashboard\Model\WidgetButton;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetItems;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IPreview;
use OCP\ITagManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;

class FavouriteWidget implements IWidget, IIconWidget, IAPIWidget, IAPIWidgetV2, IButtonWidget {
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
private IUserSession $userSession;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
private IMimeTypeDetector $mimeTypeDetector;
private IUserManager $userManager;
private ITagManager $tagManager;
private IRootFolder $rootFolder;
private IPreview $previewManager;
public const FAVORITE_LIMIT = 50;

public function __construct(
IUserSession $userSession,
IL10N $l10n,
IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector,
IUserManager $userManager,
ITagManager $tagManager,
IRootFolder $rootFolder,
IPreview $previewManager,
) {
$this->userSession = $userSession;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->userManager = $userManager;
$this->tagManager = $tagManager;
$this->rootFolder = $rootFolder;
$this->previewManager = $previewManager;
}
yemkareems marked this conversation as resolved.
Show resolved Hide resolved

public function getId(): string {
return Application::APP_ID;
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
}

public function getTitle(): string {
return $this->l10n->t('Favorites');
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
}

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;
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
}

public function load(): void {
$user = $this->userSession->getUser();
if ($user === null) {
return;
}
return;
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
//Util::addScript(Application::APP_ID, 'recommendations-dashboard');
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
}

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 [];
}
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
$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')
),
];
}
}
Loading