diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-03-30 17:21:06 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-03-30 17:21:06 +0200 |
commit | 9233d32834d07f8bb55d8efb3436d70e66014cb5 (patch) | |
tree | 0da663b2658504a383cc5cfaaef141098421f284 /apps/files/lib | |
parent | e365ea7ec55ca50cf133cc6bf64adb475a6303bf (diff) | |
download | nextcloud-server-9233d32834d07f8bb55d8efb3436d70e66014cb5.tar.gz nextcloud-server-9233d32834d07f8bb55d8efb3436d70e66014cb5.zip |
Move tag related code into a helper so we can test the query without a view
Diffstat (limited to 'apps/files/lib')
-rw-r--r-- | apps/files/lib/activity.php | 67 | ||||
-rw-r--r-- | apps/files/lib/activityhelper.php | 84 |
2 files changed, 116 insertions, 35 deletions
diff --git a/apps/files/lib/activity.php b/apps/files/lib/activity.php index 559342d9796..7302bfb33b5 100644 --- a/apps/files/lib/activity.php +++ b/apps/files/lib/activity.php @@ -27,7 +27,6 @@ use OCP\Activity\IExtension; use OCP\Activity\IManager; use OCP\IConfig; use OCP\IL10N; -use OCP\ITagManager; use OCP\IURLGenerator; class Activity implements IExtension { @@ -55,22 +54,22 @@ class Activity implements IExtension { /** @var \OCP\IConfig */ protected $config; - /** @var \OCP\ITagManager */ - protected $tagManager; + /** @var \OCA\Files\ActivityHelper */ + protected $helper; /** * @param Factory $languageFactory * @param IURLGenerator $URLGenerator * @param IManager $activityManager - * @param ITagManager $tagManager + * @param ActivityHelper $helper * @param IConfig $config */ - public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ITagManager $tagManager, IConfig $config) { + public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ActivityHelper $helper, IConfig $config) { $this->languageFactory = $languageFactory; $this->URLGenerator = $URLGenerator; $this->l = $this->getL10N(); $this->activityManager = $activityManager; - $this->tagManager = $tagManager; + $this->helper = $helper; $this->config = $config; } @@ -94,7 +93,7 @@ class Activity implements IExtension { return [ self::TYPE_SHARE_CREATED => (string) $l->t('A new file or folder has been <strong>created</strong>'), self::TYPE_SHARE_CHANGED => (string) $l->t('A file or folder has been <strong>changed</strong>'), - self::TYPE_FAVORITES => (string) $l->t('Limit notifications about creation and changes to your <strong>favorite files</strong>'), + self::TYPE_FAVORITES => (string) $l->t('Limit notifications about creation and changes to your <strong>favorite files</strong> <em>(Stream only)</em>'), self::TYPE_SHARE_DELETED => (string) $l->t('A file or folder has been <strong>deleted</strong>'), self::TYPE_SHARE_RESTORED => (string) $l->t('A file or folder has been <strong>restored</strong>'), ]; @@ -250,19 +249,20 @@ class Activity implements IExtension { */ public function getNavigation() { return [ - 'apps' => [ + 'top' => [ self::FILTER_FAVORITES => [ 'id' => self::FILTER_FAVORITES, 'name' => (string) $this->l->t('Favorites'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FAVORITES]), ], + ], + 'apps' => [ self::FILTER_FILES => [ 'id' => self::FILTER_FILES, 'name' => (string) $this->l->t('Files'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FILES]), ], ], - 'top' => [], ]; } @@ -319,41 +319,38 @@ class Activity implements IExtension { // Display actions from favorites only if ($filter === self::FILTER_FAVORITES || $filter === 'all' && $this->userSettingFavoritesOnly($user)) { - $tags = $this->tagManager->load('files', [], false, $user); - $favorites = $tags->getFavorites(); - - if (isset($favorites[50])) { + try { + $favorites = $this->helper->getFavoriteFilePaths($user); + } catch (\RuntimeException $e) { // Too many favorites, can not put them into one query anymore... return ['`app` = ?', ['files']]; } - // Can not DI because the user is not known on instantiation - $rootFolder = \OC::$server->getUserFolder($user); + /* + * Display activities only, when they are not `type` create/change + * or `file` is a favorite or in a favorite folder + */ $parameters = $fileQueryList = []; - foreach ($favorites as $favorite) { - $nodes = $rootFolder->getById($favorite); - if ($nodes) { - /** @var \OCP\Files\Node $node */ - $node = array_shift($nodes); - - $fileQueryList[] = '`file` = ?'; - $parameters[] = substr($node->getPath(), strlen($user . '/files/')); - - if ($node instanceof \OCP\Files\Folder) { - // Also look for subfolders and files - $fileQueryList[] = '`file` LIKE ?'; - $parameters[] = substr($node->getPath(), strlen($user . '/files/')) . '/%'; - } - } - } + $parameters[] = 'files'; - if (empty($fileQueryList)) { - // No favorites... - return ['`app` = ?', ['files']]; + $fileQueryList[] = '`type` <> ?'; + $parameters[] = self::TYPE_SHARE_CREATED; + $fileQueryList[] = '`type` <> ?'; + $parameters[] = self::TYPE_SHARE_CHANGED; + + foreach ($favorites['items'] as $favorite) { + $fileQueryList[] = '`file` = ?'; + $parameters[] = $favorite; } + foreach ($favorites['folders'] as $favorite) { + $fileQueryList[] = '`file` LIKE ?'; + $parameters[] = $favorite . '/%'; + } + + $parameters[] = 'files'; return [ - ' CASE WHEN `app` = \'files\' THEN (' . implode(' OR ', $fileQueryList) . ') ELSE `app` <> \'files\' END ', + ' CASE WHEN `app` = ? THEN (' . implode(' OR ', $fileQueryList) . ') ELSE `app` <> ? END ', $parameters, ]; } diff --git a/apps/files/lib/activityhelper.php b/apps/files/lib/activityhelper.php new file mode 100644 index 00000000000..e05ae6c9831 --- /dev/null +++ b/apps/files/lib/activityhelper.php @@ -0,0 +1,84 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Files; + +use OCP\Files\Folder; +use OCP\ITagManager; + +class ActivityHelper { + /** If a user has a lot of favorites the query might get too slow and long */ + const FAVORITE_LIMIT = 50; + + /** @var \OCP\ITagManager */ + protected $tagManager; + + /** + * @param ITagManager $tagManager + */ + public function __construct(ITagManager $tagManager) { + $this->tagManager = $tagManager; + } + + /** + * Returns an array with the favorites + * + * @param string $user + * @return array + * @throws \RuntimeException when too many or no favorites where found + */ + public function getFavoriteFilePaths($user) { + $tags = $this->tagManager->load('files', [], false, $user); + $favorites = $tags->getFavorites(); + + if (empty($favorites)) { + throw new \RuntimeException('No favorites', 1); + } else if (isset($favorites[self::FAVORITE_LIMIT])) { + throw new \RuntimeException('Too many favorites', 2); + } + + // Can not DI because the user is not known on instantiation + $rootFolder = \OC::$server->getUserFolder($user); + $folders = $items = []; + foreach ($favorites as $favorite) { + $nodes = $rootFolder->getById($favorite); + if ($nodes) { + /** @var \OCP\Files\Node $node */ + $node = array_shift($nodes); + $path = substr($node->getPath(), strlen($user . '/files/')); + + $items[] = $path; + if ($node instanceof Folder) { + $folders[] = $path; + } + } + } + + if (empty($items)) { + throw new \RuntimeException('No favorites', 1); + } + + return [ + 'items' => $items, + 'folders' => $folders, + ]; + } +} |