Skip to content

Commit 77ea357

Browse files
committed
fix(files): Make sure to add the fileid on favorite folders navigation entries
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent a395071 commit 77ea357

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

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 <[email protected]>
66
* @author Joas Schilling <[email protected]>
7+
* @author Ferdinand Thiessen <[email protected]>
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ import { getContents } from '../services/Favorites'
3131
import { hashCode } from '../utils/hashUtils'
3232
import logger from '../logger'
3333

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

3945
icon: FolderSvg,
4046
order: index,
4147
params: {
42-
dir: folder,
48+
dir: folder.path,
49+
fileid: folder.fileid.toString(),
4350
view: 'favorites',
4451
},
4552

@@ -57,7 +64,7 @@ export const generateIdFromPath = function(path: string): string {
5764

5865
export default () => {
5966
// Load state in function for mock testing purposes
60-
const favoriteFolders = loadState<string[]>('files', 'favoriteFolders', [])
67+
const favoriteFolders = loadState<IFavoriteFolder[]>('files', 'favoriteFolders', [])
6168
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index)) as View[]
6269

6370
const Navigation = getNavigation()

0 commit comments

Comments
 (0)