Helper.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files\Activity;
  24. use OCP\Files\Folder;
  25. use OCP\ITagManager;
  26. class Helper {
  27. /** If a user has a lot of favorites the query might get too slow and long */
  28. public const FAVORITE_LIMIT = 50;
  29. /** @var ITagManager */
  30. protected $tagManager;
  31. /**
  32. * @param ITagManager $tagManager
  33. */
  34. public function __construct(ITagManager $tagManager) {
  35. $this->tagManager = $tagManager;
  36. }
  37. /**
  38. * Returns an array with the favorites
  39. *
  40. * @param string $user
  41. * @return array
  42. * @throws \RuntimeException when too many or no favorites where found
  43. */
  44. public function getFavoriteFilePaths($user) {
  45. $tags = $this->tagManager->load('files', [], false, $user);
  46. $favorites = $tags->getFavorites();
  47. if (empty($favorites)) {
  48. throw new \RuntimeException('No favorites', 1);
  49. } elseif (isset($favorites[self::FAVORITE_LIMIT])) {
  50. throw new \RuntimeException('Too many favorites', 2);
  51. }
  52. // Can not DI because the user is not known on instantiation
  53. $rootFolder = \OC::$server->getUserFolder($user);
  54. $folders = $items = [];
  55. foreach ($favorites as $favorite) {
  56. $nodes = $rootFolder->getById($favorite);
  57. if (!empty($nodes)) {
  58. /** @var \OCP\Files\Node $node */
  59. $node = array_shift($nodes);
  60. $path = substr($node->getPath(), strlen($user . '/files/'));
  61. $items[] = $path;
  62. if ($node instanceof Folder) {
  63. $folders[] = $path;
  64. }
  65. }
  66. }
  67. if (empty($items)) {
  68. throw new \RuntimeException('No favorites', 1);
  69. }
  70. return [
  71. 'items' => $items,
  72. 'folders' => $folders,
  73. ];
  74. }
  75. }