\OC::$server->query('L10NFactory'),
\OC::$server->getURLGenerator(),
\OC::$server->getActivityManager(),
- new \OCA\Files\ActivityHelper(
- \OC::$server->getTagManager()
- ),
\OC::$server->getDatabaseConnection(),
\OC::$server->getConfig()
);
<activity>
<filters>
<filter>OCA\Files\Activity\Filter\FileChanges</filter>
+ <filter>OCA\Files\Activity\Filter\Favorites</filter>
</filters>
</activity>
/** @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;
}
* @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;
}
/**
* @return boolean
*/
public function isFilterValid($filterValue) {
- return $filterValue === self::FILTER_FAVORITES;
+ return false;
}
/**
* @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;
}
* @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);
- }
}
--- /dev/null
+<?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);
+ }
+}
--- /dev/null
+<?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,
+ ];
+ }
+}
+++ /dev/null
-<?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,
- ];
- }
-}
/** @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;
$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,
$this->l10nFactory,
$this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
$this->activityManager,
- $this->activityHelper,
\OC::$server->getDatabaseConnection(),
$this->config
);