]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move the favorites filter to IFilter
authorJoas Schilling <coding@schilljs.com>
Fri, 21 Oct 2016 10:46:04 +0000 (12:46 +0200)
committerJoas Schilling <coding@schilljs.com>
Wed, 16 Nov 2016 08:25:44 +0000 (09:25 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/files/appinfo/app.php
apps/files/appinfo/info.xml
apps/files/lib/Activity.php
apps/files/lib/Activity/Filter/Favorites.php [new file with mode: 0644]
apps/files/lib/Activity/Helper.php [new file with mode: 0644]
apps/files/lib/ActivityHelper.php [deleted file]
apps/files/tests/ActivityTest.php

index afb327e24ba31c61cfacf380f2933081d7baa811..a541dad3cc79868d2b7a3ea116f08ed0109f0599 100644 (file)
@@ -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()
        );
index 85d0bb167bd74ac04d35c9c6496f6f558dbb632d..d19c57dffe09bab6cf427a0765c5467683ed7ea2 100644 (file)
@@ -20,6 +20,7 @@
        <activity>
                <filters>
                        <filter>OCA\Files\Activity\Filter\FileChanges</filter>
+                       <filter>OCA\Files\Activity\Filter\Favorites</filter>
                </filters>
        </activity>
 
index 6c465943e310340a4a4233dc824add34939ecc9f..f60f4f16f0a444b1c8a4b54d2809bf0209986bea 100644 (file)
@@ -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 (file)
index 0000000..955e56f
--- /dev/null
@@ -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/Activity/Helper.php b/apps/files/lib/Activity/Helper.php
new file mode 100644 (file)
index 0000000..d03d6e8
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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\Activity;
+
+use OCP\Files\Folder;
+use OCP\ITagManager;
+
+class Helper {
+       /** If a user has a lot of favorites the query might get too slow and long */
+       const FAVORITE_LIMIT = 50;
+
+       /** @var 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 (!empty($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,
+               ];
+       }
+}
diff --git a/apps/files/lib/ActivityHelper.php b/apps/files/lib/ActivityHelper.php
deleted file mode 100644 (file)
index f5660de..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Joas Schilling <coding@schilljs.com>
- *
- * @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 (!empty($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,
-               ];
-       }
-}
index 65e914c1a547850202daa75ccbbe0ce0f0f4c809..37bb82ab1df4c2ed9cb0651216ea188e157136e2 100644 (file)
@@ -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
                );