-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(files): Make sure to add the
fileid
on favorite folders navigat…
…ion entries Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
4 changed files
with
63 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
* | ||
* @author Christoph Wurst <[email protected]> | ||
* @author Joas Schilling <[email protected]> | ||
* @author Ferdinand Thiessen <[email protected]> | ||
* | ||
* @license AGPL-3.0 | ||
* | ||
|
@@ -23,30 +24,29 @@ | |
namespace OCA\Files\Activity; | ||
|
||
use OCP\Files\Folder; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\Node; | ||
use OCP\ITagManager; | ||
|
||
class Helper { | ||
/** If a user has a lot of favorites the query might get too slow and long */ | ||
public const FAVORITE_LIMIT = 50; | ||
|
||
/** @var ITagManager */ | ||
protected $tagManager; | ||
|
||
/** | ||
* @param ITagManager $tagManager | ||
*/ | ||
public function __construct(ITagManager $tagManager) { | ||
$this->tagManager = $tagManager; | ||
public function __construct( | ||
protected ITagManager $tagManager, | ||
protected IRootFolder $rootFolder, | ||
) { | ||
} | ||
|
||
/** | ||
* Returns an array with the favorites | ||
* Return an array with nodes marked as favorites | ||
* | ||
* @param string $user | ||
* @return array | ||
* @param string $user User ID | ||
* @param bool $foldersOnly Only return folders (default false) | ||
* @return Node[] | ||
* @throws \RuntimeException when too many or no favorites where found | ||
*/ | ||
public function getFavoriteFilePaths($user) { | ||
public function getFavoriteNodes(string $user, bool $foldersOnly = false): array { | ||
$tags = $this->tagManager->load('files', [], false, $user); | ||
$favorites = $tags->getFavorites(); | ||
|
||
|
@@ -57,26 +57,45 @@ public function getFavoriteFilePaths($user) { | |
} | ||
|
||
// Can not DI because the user is not known on instantiation | ||
$rootFolder = \OC::$server->getUserFolder($user); | ||
$folders = $items = []; | ||
$userFolder = $this->rootFolder->getUserFolder($user); | ||
$nodes = []; | ||
foreach ($favorites as $favorite) { | ||
$nodes = $rootFolder->getById($favorite); | ||
$nodes = $userFolder->getById($favorite); | ||
if (!empty($nodes)) { | ||
/** @var \OCP\Files\Node $node */ | ||
$node = array_shift($nodes); | ||
$path = substr($node->getPath(), strlen($user . '/files/')); | ||
|
||
$items[] = $path; | ||
if ($node instanceof Folder) { | ||
$folders[] = $path; | ||
if (!$foldersOnly || $node instanceof Folder) { | ||
$nodes[] = $node; | ||
} | ||
} | ||
} | ||
|
||
if (empty($items)) { | ||
if (empty($nodes)) { | ||
throw new \RuntimeException('No favorites', 1); | ||
} | ||
|
||
return $nodes; | ||
} | ||
|
||
/** | ||
* Returns an array with the favorites | ||
* | ||
* @param string $user | ||
* @return array | ||
* @throws \RuntimeException when too many or no favorites where found | ||
*/ | ||
public function getFavoriteFilePaths($user) { | ||
$userFolder = $this->rootFolder->getUserFolder($user); | ||
$nodes = $this->getFavoriteNodes($user); | ||
$folders = $items = []; | ||
foreach ($nodes as $node) { | ||
$path = $userFolder->getRelativePath($node->getPath()); | ||
|
||
$items[] = $path; | ||
if ($node instanceof Folder) { | ||
$folders[] = $path; | ||
} | ||
} | ||
|
||
return [ | ||
'items' => $items, | ||
'folders' => $folders, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters