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 *
2324namespace OCA \Files \Activity ;
2425
2526use OCP \Files \Folder ;
27+ use OCP \Files \IRootFolder ;
28+ use OCP \Files \Node ;
2629use OCP \ITagManager ;
2730
2831class 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+ $ favoriteNodes = [];
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+ $ favoriteNodes [] = $ node ;
7269 }
7370 }
7471 }
7572
76- if (empty ($ items )) {
73+ if (empty ($ favoriteNodes )) {
7774 throw new \RuntimeException ('No favorites ' , 1 );
7875 }
7976
77+ return $ favoriteNodes ;
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 ,
0 commit comments