activityhelper.php 2.2 KB

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