diff options
author | Joas Schilling <coding@schilljs.com> | 2016-10-21 12:46:04 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2016-11-16 09:25:44 +0100 |
commit | 0d940e581a38735497784b76c1321001493e5290 (patch) | |
tree | 7cf008764d0c6bb71205bd0f58f33c4dce04cafa /apps | |
parent | 5d71896c8e188d31e5ef0860c16ad469948514b1 (diff) | |
download | nextcloud-server-0d940e581a38735497784b76c1321001493e5290.tar.gz nextcloud-server-0d940e581a38735497784b76c1321001493e5290.zip |
Move the favorites filter to IFilter
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/appinfo/app.php | 3 | ||||
-rw-r--r-- | apps/files/appinfo/info.xml | 1 | ||||
-rw-r--r-- | apps/files/lib/Activity.php | 84 | ||||
-rw-r--r-- | apps/files/lib/Activity/Filter/Favorites.php | 160 | ||||
-rw-r--r-- | apps/files/lib/Activity/Helper.php (renamed from apps/files/lib/ActivityHelper.php) | 6 | ||||
-rw-r--r-- | apps/files/tests/ActivityTest.php | 7 |
6 files changed, 167 insertions, 94 deletions
diff --git a/apps/files/appinfo/app.php b/apps/files/appinfo/app.php index afb327e24ba..a541dad3cc7 100644 --- a/apps/files/appinfo/app.php +++ b/apps/files/appinfo/app.php @@ -73,9 +73,6 @@ $templateManager->registerTemplate('application/vnd.oasis.opendocument.spreadshe \OC::$server->query('L10NFactory'), \OC::$server->getURLGenerator(), \OC::$server->getActivityManager(), - new \OCA\Files\ActivityHelper( - \OC::$server->getTagManager() - ), \OC::$server->getDatabaseConnection(), \OC::$server->getConfig() ); diff --git a/apps/files/appinfo/info.xml b/apps/files/appinfo/info.xml index 85d0bb167bd..d19c57dffe0 100644 --- a/apps/files/appinfo/info.xml +++ b/apps/files/appinfo/info.xml @@ -20,6 +20,7 @@ <activity> <filters> <filter>OCA\Files\Activity\Filter\FileChanges</filter> + <filter>OCA\Files\Activity\Filter\Favorites</filter> </filters> </activity> diff --git a/apps/files/lib/Activity.php b/apps/files/lib/Activity.php index 6c465943e31..f60f4f16f0a 100644 --- a/apps/files/lib/Activity.php +++ b/apps/files/lib/Activity.php @@ -60,23 +60,18 @@ class Activity implements IExtension { /** @var \OCP\IConfig */ protected $config; - /** @var \OCA\Files\ActivityHelper */ - protected $helper; - /** * @param IFactory $languageFactory * @param IURLGenerator $URLGenerator * @param IManager $activityManager - * @param ActivityHelper $helper * @param IDBConnection $connection * @param IConfig $config */ - public function __construct(IFactory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ActivityHelper $helper, IDBConnection $connection, IConfig $config) { + public function __construct(IFactory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, IDBConnection $connection, IConfig $config) { $this->languageFactory = $languageFactory; $this->URLGenerator = $URLGenerator; $this->l = $this->getL10N(); $this->activityManager = $activityManager; - $this->helper = $helper; $this->connection = $connection; $this->config = $config; } @@ -327,17 +322,7 @@ class Activity implements IExtension { * @return array|false */ public function getNavigation() { - return [ - 'top' => [ - self::FILTER_FAVORITES => [ - 'id' => self::FILTER_FAVORITES, - 'icon' => 'icon-favorite', - 'name' => (string) $this->l->t('Favorites'), - 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FAVORITES]), - ], - ], - 'apps' => [], - ]; + return false; } /** @@ -347,7 +332,7 @@ class Activity implements IExtension { * @return boolean */ public function isFilterValid($filterValue) { - return $filterValue === self::FILTER_FAVORITES; + return false; } /** @@ -359,14 +344,6 @@ class Activity implements IExtension { * @return array|false */ public function filterNotificationTypes($types, $filter) { - if ($filter === self::FILTER_FAVORITES) { - return array_intersect([ - self::TYPE_SHARE_CREATED, - self::TYPE_SHARE_CHANGED, - self::TYPE_SHARE_DELETED, - self::TYPE_SHARE_RESTORED, - ], $types); - } return false; } @@ -380,61 +357,6 @@ class Activity implements IExtension { * @return array|false */ public function getQueryForFilter($filter) { - $user = $this->activityManager->getCurrentUserId(); - if (!$user) { - // Remaining filters only work with a user/token - return false; - } - - // Display actions from favorites only - if ($filter === self::FILTER_FAVORITES || in_array($filter, ['all', 'by', 'self']) && $this->userSettingFavoritesOnly($user)) { - try { - $favorites = $this->helper->getFavoriteFilePaths($user); - } catch (\RuntimeException $e) { - // Too many favorites, can not put them into one query anymore... - return ['`app` = ?', [self::APP_FILES]]; - } - - /* - * Display activities only, when they are not `type` create/change - * or `file` is a favorite or in a favorite folder - */ - $parameters = $fileQueryList = []; - $parameters[] = self::APP_FILES; - $parameters[] = self::APP_FILES; - - $fileQueryList[] = '(`type` <> ? AND `type` <> ?)'; - $parameters[] = self::TYPE_SHARE_CREATED; - $parameters[] = self::TYPE_SHARE_CHANGED; - - foreach ($favorites['items'] as $favorite) { - $fileQueryList[] = '`file` = ?'; - $parameters[] = $favorite; - } - foreach ($favorites['folders'] as $favorite) { - $fileQueryList[] = '`file` LIKE ?'; - $parameters[] = $this->connection->escapeLikeParameter($favorite) . '/%'; - } - - return [ - ' CASE ' - . 'WHEN `app` <> ? THEN 1 ' - . 'WHEN `app` = ? AND (' . implode(' OR ', $fileQueryList) . ') THEN 1 ' - . 'ELSE 0 ' - . 'END = 1 ', - $parameters, - ]; - } return false; } - - /** - * Is the file actions favorite limitation enabled? - * - * @param string $user - * @return bool - */ - protected function userSettingFavoritesOnly($user) { - return (bool) $this->config->getUserValue($user, 'activity', 'notify_' . self::METHOD_STREAM . '_' . self::TYPE_FAVORITES, false); - } } diff --git a/apps/files/lib/Activity/Filter/Favorites.php b/apps/files/lib/Activity/Filter/Favorites.php new file mode 100644 index 00000000000..955e56f8330 --- /dev/null +++ b/apps/files/lib/Activity/Filter/Favorites.php @@ -0,0 +1,160 @@ +<?php +/** + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Files\Activity\Filter; + + +use OCA\Files\Activity\Helper; +use OCP\Activity\IFilter; +use OCP\Activity\IManager; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\IL10N; +use OCP\IURLGenerator; + +class Favorites implements IFilter { + + /** @var IL10N */ + protected $l; + + /** @var IURLGenerator */ + protected $url; + + /** @var IManager */ + protected $activityManager; + + /** @var Helper */ + protected $helper; + + /** @var IDBConnection */ + protected $db; + + /** + * @param IL10N $l + * @param IURLGenerator $url + * @param IManager $activityManager + * @param Helper $helper + * @param IDBConnection $db + */ + public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, Helper $helper, IDBConnection $db) { + $this->l = $l; + $this->url = $url; + $this->activityManager = $activityManager; + $this->helper = $helper; + $this->db = $db; + } + + /** + * @return string Lowercase a-z only identifier + * @since 9.2.0 + */ + public function getIdentifier() { + return 'files_favorites'; + } + + /** + * @return string A translated string + * @since 9.2.0 + */ + public function getName() { + return $this->l->t('Favorites'); + } + + /** + * @return int + * @since 9.2.0 + */ + public function getPriority() { + return 10; + } + + /** + * @return string Full URL to an icon, empty string when none is given + * @since 9.2.0 + */ + public function getIcon() { + return $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star-dark.svg')); + } + + /** + * @param string[] $types + * @return string[] An array of allowed apps from which activities should be displayed + * @since 9.2.0 + */ + public function filterTypes(array $types) { + return array_intersect([ + 'file_created', + 'file_changed', + 'file_deleted', + 'file_restored', + ], $types); + } + + /** + * @return string[] An array of allowed apps from which activities should be displayed + * @since 9.2.0 + */ + public function allowedApps() { + return ['files']; + } + + /** + * @param IQueryBuilder $query + */ + public function filterFavorites(IQueryBuilder $query) { + try { + $user = $this->activityManager->getCurrentUserId(); + } catch (\UnexpectedValueException $e) { + return; + } + + try { + $favorites = $this->helper->getFavoriteFilePaths($user); + } catch (\RuntimeException $e) { + return; + } + + $limitations = []; + if (!empty($favorites['items'])) { + $limitations[] = $query->expr()->in('file', $query->createNamedParameter($favorites['items'], IQueryBuilder::PARAM_STR_ARRAY)); + } + foreach ($favorites['folders'] as $favorite) { + $limitations[] = $query->expr()->like('file', $query->createNamedParameter( + $this->db->escapeLikeParameter($favorite . '/') . '%' + )); + } + + if (empty($limitations)) { + return; + } + + $function = $query->createFunction(' + CASE + WHEN ' . $query->getColumnName('app') . ' <> ' . $query->createNamedParameter('files') . ' THEN 1 + WHEN ' . $query->getColumnName('app') . ' = ' . $query->createNamedParameter('files') . ' + AND (' . implode(' OR ', $limitations) . ') + THEN 1 + END = 1' + ); + + $query->andWhere($function); + } +} diff --git a/apps/files/lib/ActivityHelper.php b/apps/files/lib/Activity/Helper.php index f5660de4b37..d03d6e8e974 100644 --- a/apps/files/lib/ActivityHelper.php +++ b/apps/files/lib/Activity/Helper.php @@ -20,16 +20,16 @@ * */ -namespace OCA\Files; +namespace OCA\Files\Activity; use OCP\Files\Folder; use OCP\ITagManager; -class ActivityHelper { +class Helper { /** If a user has a lot of favorites the query might get too slow and long */ const FAVORITE_LIMIT = 50; - /** @var \OCP\ITagManager */ + /** @var ITagManager */ protected $tagManager; /** diff --git a/apps/files/tests/ActivityTest.php b/apps/files/tests/ActivityTest.php index 65e914c1a54..37bb82ab1df 100644 --- a/apps/files/tests/ActivityTest.php +++ b/apps/files/tests/ActivityTest.php @@ -48,9 +48,6 @@ class ActivityTest extends TestCase { /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ protected $config; - /** @var \OCA\Files\ActivityHelper|\PHPUnit_Framework_MockObject_MockObject */ - protected $activityHelper; - /** @var \OCP\L10N\IFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $l10nFactory; @@ -69,9 +66,6 @@ class ActivityTest extends TestCase { $this->config = $this->getMockBuilder('OCP\IConfig') ->disableOriginalConstructor() ->getMock(); - $this->activityHelper = $this->getMockBuilder('OCA\Files\ActivityHelper') - ->disableOriginalConstructor() - ->getMock(); $this->activityManager = new \OC\Activity\Manager( $this->request, @@ -99,7 +93,6 @@ class ActivityTest extends TestCase { $this->l10nFactory, $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->activityManager, - $this->activityHelper, \OC::$server->getDatabaseConnection(), $this->config ); |