Helper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\Activity;
  25. use OCP\Files\Folder;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\Node;
  28. use OCP\ITagManager;
  29. class Helper {
  30. /** If a user has a lot of favorites the query might get too slow and long */
  31. public const FAVORITE_LIMIT = 50;
  32. public function __construct(
  33. protected ITagManager $tagManager,
  34. protected IRootFolder $rootFolder,
  35. ) {
  36. }
  37. /**
  38. * Return an array with nodes marked as favorites
  39. *
  40. * @param string $user User ID
  41. * @param bool $foldersOnly Only return folders (default false)
  42. * @return Node[]
  43. * @psalm-return ($foldersOnly is true ? Folder[] : Node[])
  44. * @throws \RuntimeException when too many or no favorites where found
  45. */
  46. public function getFavoriteNodes(string $user, bool $foldersOnly = false): array {
  47. $tags = $this->tagManager->load('files', [], false, $user);
  48. $favorites = $tags->getFavorites();
  49. if (empty($favorites)) {
  50. throw new \RuntimeException('No favorites', 1);
  51. } elseif (isset($favorites[self::FAVORITE_LIMIT])) {
  52. throw new \RuntimeException('Too many favorites', 2);
  53. }
  54. // Can not DI because the user is not known on instantiation
  55. $userFolder = $this->rootFolder->getUserFolder($user);
  56. $favoriteNodes = [];
  57. foreach ($favorites as $favorite) {
  58. $node = $userFolder->getFirstNodeById($favorite);
  59. if ($node) {
  60. if (!$foldersOnly || $node instanceof Folder) {
  61. $favoriteNodes[] = $node;
  62. }
  63. }
  64. }
  65. if (empty($favoriteNodes)) {
  66. throw new \RuntimeException('No favorites', 1);
  67. }
  68. return $favoriteNodes;
  69. }
  70. /**
  71. * Returns an array with the favorites
  72. *
  73. * @param string $user
  74. * @return array
  75. * @throws \RuntimeException when too many or no favorites where found
  76. */
  77. public function getFavoriteFilePaths(string $user): array {
  78. $userFolder = $this->rootFolder->getUserFolder($user);
  79. $nodes = $this->getFavoriteNodes($user);
  80. $folders = $items = [];
  81. foreach ($nodes as $node) {
  82. $path = $userFolder->getRelativePath($node->getPath());
  83. $items[] = $path;
  84. if ($node instanceof Folder) {
  85. $folders[] = $path;
  86. }
  87. }
  88. return [
  89. 'items' => $items,
  90. 'folders' => $folders,
  91. ];
  92. }
  93. }