1
0

Helper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $nodes = $userFolder->getById($favorite);
  59. if (!empty($nodes)) {
  60. $node = array_shift($nodes);
  61. if (!$foldersOnly || $node instanceof Folder) {
  62. $favoriteNodes[] = $node;
  63. }
  64. }
  65. }
  66. if (empty($favoriteNodes)) {
  67. throw new \RuntimeException('No favorites', 1);
  68. }
  69. return $favoriteNodes;
  70. }
  71. /**
  72. * Returns an array with the favorites
  73. *
  74. * @param string $user
  75. * @return array
  76. * @throws \RuntimeException when too many or no favorites where found
  77. */
  78. public function getFavoriteFilePaths(string $user): array {
  79. $userFolder = $this->rootFolder->getUserFolder($user);
  80. $nodes = $this->getFavoriteNodes($user);
  81. $folders = $items = [];
  82. foreach ($nodes as $node) {
  83. $path = $userFolder->getRelativePath($node->getPath());
  84. $items[] = $path;
  85. if ($node instanceof Folder) {
  86. $folders[] = $path;
  87. }
  88. }
  89. return [
  90. 'items' => $items,
  91. 'folders' => $folders,
  92. ];
  93. }
  94. }