Skip to content

Commit 9e786c5

Browse files
committed
fix(files): Make sure to add the fileid on favorite folders navigation entries
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent feba364 commit 9e786c5

4 files changed

Lines changed: 82 additions & 47 deletions

File tree

apps/files/lib/Activity/Helper.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
66
* @author Joas Schilling <coding@schilljs.com>
7+
* @author Ferdinand Thiessen <opensource@fthiessen.de>
78
*
89
* @license AGPL-3.0
910
*
@@ -23,30 +24,30 @@
2324
namespace OCA\Files\Activity;
2425

2526
use OCP\Files\Folder;
27+
use OCP\Files\IRootFolder;
28+
use OCP\Files\Node;
2629
use OCP\ITagManager;
2730

2831
class Helper {
2932
/** If a user has a lot of favorites the query might get too slow and long */
3033
public const FAVORITE_LIMIT = 50;
3134

32-
/** @var ITagManager */
33-
protected $tagManager;
34-
35-
/**
36-
* @param ITagManager $tagManager
37-
*/
38-
public function __construct(ITagManager $tagManager) {
39-
$this->tagManager = $tagManager;
35+
public function __construct(
36+
protected ITagManager $tagManager,
37+
protected IRootFolder $rootFolder,
38+
) {
4039
}
4140

4241
/**
43-
* Returns an array with the favorites
42+
* Return an array with nodes marked as favorites
4443
*
45-
* @param string $user
46-
* @return array
44+
* @param string $user User ID
45+
* @param bool $foldersOnly Only return folders (default false)
46+
* @return Node[]
47+
* @psalm-return ($foldersOnly is true ? Folder[] : Node[])
4748
* @throws \RuntimeException when too many or no favorites where found
4849
*/
49-
public function getFavoriteFilePaths($user) {
50+
public function getFavoriteNodes(string $user, bool $foldersOnly = false): array {
5051
$tags = $this->tagManager->load('files', [], false, $user);
5152
$favorites = $tags->getFavorites();
5253

@@ -57,26 +58,45 @@ public function getFavoriteFilePaths($user) {
5758
}
5859

5960
// Can not DI because the user is not known on instantiation
60-
$rootFolder = \OC::$server->getUserFolder($user);
61-
$folders = $items = [];
61+
$userFolder = $this->rootFolder->getUserFolder($user);
62+
$nodes = [];
6263
foreach ($favorites as $favorite) {
63-
$nodes = $rootFolder->getById($favorite);
64+
$nodes = $userFolder->getById($favorite);
6465
if (!empty($nodes)) {
65-
/** @var \OCP\Files\Node $node */
6666
$node = array_shift($nodes);
67-
$path = substr($node->getPath(), strlen($user . '/files/'));
68-
69-
$items[] = $path;
70-
if ($node instanceof Folder) {
71-
$folders[] = $path;
67+
if (!$foldersOnly || $node instanceof Folder) {
68+
$nodes[] = $node;
7269
}
7370
}
7471
}
7572

76-
if (empty($items)) {
73+
if (empty($nodes)) {
7774
throw new \RuntimeException('No favorites', 1);
7875
}
7976

77+
return $nodes;
78+
}
79+
80+
/**
81+
* Returns an array with the favorites
82+
*
83+
* @param string $user
84+
* @return array
85+
* @throws \RuntimeException when too many or no favorites where found
86+
*/
87+
public function getFavoriteFilePaths(string $user): array {
88+
$userFolder = $this->rootFolder->getUserFolder($user);
89+
$nodes = $this->getFavoriteNodes($user);
90+
$folders = $items = [];
91+
foreach ($nodes as $node) {
92+
$path = $userFolder->getRelativePath($node->getPath());
93+
94+
$items[] = $path;
95+
if ($node instanceof Folder) {
96+
$folders[] = $path;
97+
}
98+
}
99+
80100
return [
81101
'items' => $items,
82102
'folders' => $folders,

apps/files/lib/Controller/ViewController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,14 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
226226

227227
// Get all the user favorites to create a submenu
228228
try {
229-
$favElements = $this->activityHelper->getFavoriteFilePaths($userId);
229+
$userFolder = $this->rootFolder->getUserFolder($userId);
230+
$favElements = $this->activityHelper->getFavoriteNodes($userId, true);
231+
$favElements = array_map(fn (Folder $node) => [
232+
'fileid' => $node->getId(),
233+
'path' => $userFolder->getRelativePath($node->getPath()),
234+
], $favElements);
230235
} catch (\RuntimeException $e) {
231-
$favElements['folders'] = [];
236+
$favElements = [];
232237
}
233238

234239
// If the file doesn't exists in the folder and
@@ -260,7 +265,7 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
260265
$this->initialState->provideInitialState('storageStats', $storageInfo);
261266
$this->initialState->provideInitialState('config', $this->userConfig->getConfigs());
262267
$this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs());
263-
$this->initialState->provideInitialState('favoriteFolders', $favElements['folders'] ?? []);
268+
$this->initialState->provideInitialState('favoriteFolders', $favElements);
264269

265270
// File sorting user config
266271
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);

apps/files/src/views/Navigation.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<template #list>
2626
<NcAppNavigationItem v-for="view in parentViews"
2727
:key="view.id"
28-
allow-collapse
28+
:allow-collapse="true"
2929
:data-cy-files-navigation-item="view.id"
3030
:exact="useExactRouteMatching(view)"
3131
:icon="view.iconClass"
@@ -188,7 +188,7 @@ export default {
188188
* Like for the 'files' view this does not work because of optional 'fileid' param so /files and /files/1234 are both in the 'files' view
189189
* @param view The view to check
190190
*/
191-
useExactRouteMatching(view: View) {
191+
useExactRouteMatching(view: View): boolean {
192192
return this.childViews[view.id]?.length > 0
193193
},
194194
@@ -230,8 +230,8 @@ export default {
230230
*/
231231
generateToNavigation(view: View) {
232232
if (view.params) {
233-
const { dir, fileid } = view.params
234-
return { name: 'filelist', params: view.params, query: { dir, fileid } }
233+
const { dir } = view.params
234+
return { name: 'filelist', params: view.params, query: { dir } }
235235
}
236236
return { name: 'filelist', params: { view: view.id } }
237237
},

apps/files/src/views/favorites.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,36 @@
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*
2121
*/
22-
import { basename } from 'path'
23-
import { getLanguage, translate as t } from '@nextcloud/l10n'
24-
import { loadState } from '@nextcloud/initial-state'
25-
import { Node, FileType, View, getNavigation } from '@nextcloud/files'
22+
import type { Folder, Node } from '@nextcloud/files'
23+
2624
import { subscribe } from '@nextcloud/event-bus'
25+
import { FileType, View, getNavigation } from '@nextcloud/files'
26+
import { loadState } from '@nextcloud/initial-state'
27+
import { getLanguage, translate as t } from '@nextcloud/l10n'
28+
import { basename } from 'path'
2729
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
2830
import StarSvg from '@mdi/svg/svg/star.svg?raw'
2931

3032
import { getContents } from '../services/Favorites'
3133
import { hashCode } from '../utils/hashUtils'
3234
import logger from '../logger'
3335

34-
export const generateFolderView = function(folder: string, index = 0): View {
36+
// The return type of the initial state
37+
interface IFavoriteFolder {
38+
fileid: number
39+
path: string
40+
}
41+
42+
export const generateFavoriteFolderView = function(folder: IFavoriteFolder, index = 0): View {
3543
return new View({
36-
id: generateIdFromPath(folder),
37-
name: basename(folder),
44+
id: generateIdFromPath(folder.path),
45+
name: basename(folder.path),
3846

3947
icon: FolderSvg,
4048
order: index,
4149
params: {
42-
dir: folder,
50+
dir: folder.path,
51+
fileid: folder.fileid.toString(),
4352
view: 'favorites',
4453
},
4554

@@ -57,8 +66,8 @@ export const generateIdFromPath = function(path: string): string {
5766

5867
export default () => {
5968
// Load state in function for mock testing purposes
60-
const favoriteFolders = loadState<string[]>('files', 'favoriteFolders', [])
61-
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index)) as View[]
69+
const favoriteFolders = loadState<IFavoriteFolder[]>('files', 'favoriteFolders', [])
70+
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFavoriteFolderView(folder, index)) as View[]
6271

6372
const Navigation = getNavigation()
6473
Navigation.register(new View({
@@ -93,7 +102,7 @@ export default () => {
93102
return
94103
}
95104

96-
addPathToFavorites(node.path)
105+
addToFavorites(node as Folder)
97106
})
98107

99108
/**
@@ -118,26 +127,27 @@ export default () => {
118127
* update the order property of the existing views
119128
*/
120129
const updateAndSortViews = function() {
121-
favoriteFolders.sort((a, b) => a.localeCompare(b, getLanguage(), { ignorePunctuation: true }))
130+
favoriteFolders.sort((a, b) => a.path.localeCompare(b.path, getLanguage(), { ignorePunctuation: true }))
122131
favoriteFolders.forEach((folder, index) => {
123-
const view = favoriteFoldersViews.find(view => view.id === generateIdFromPath(folder))
132+
const view = favoriteFoldersViews.find((view) => view.id === generateIdFromPath(folder.path))
124133
if (view) {
125134
view.order = index
126135
}
127136
})
128137
}
129138

130139
// Add a folder to the favorites paths array and update the views
131-
const addPathToFavorites = function(path: string) {
132-
const view = generateFolderView(path)
140+
const addToFavorites = function(node: Folder) {
141+
const newFavoriteFolder = { path: node.path, fileid: node.fileid! } as IFavoriteFolder
142+
const view = generateFavoriteFolderView(newFavoriteFolder)
133143

134144
// Skip if already exists
135-
if (favoriteFolders.find(folder => folder === path)) {
145+
if (favoriteFolders.find((folder) => folder.path === node.path)) {
136146
return
137147
}
138148

139149
// Update arrays
140-
favoriteFolders.push(path)
150+
favoriteFolders.push(newFavoriteFolder)
141151
favoriteFoldersViews.push(view)
142152

143153
// Update and sort views
@@ -148,7 +158,7 @@ export default () => {
148158
// Remove a folder from the favorites paths array and update the views
149159
const removePathFromFavorites = function(path: string) {
150160
const id = generateIdFromPath(path)
151-
const index = favoriteFolders.findIndex(folder => folder === path)
161+
const index = favoriteFolders.findIndex((folder) => folder.path === path)
152162

153163
// Skip if not exists
154164
if (index === -1) {

0 commit comments

Comments
 (0)