Move the favorites filter to IFilter

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2016-10-21 12:46:04 +02:00
parent 5d71896c8e
commit 0d940e581a
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
6 changed files with 167 additions and 94 deletions

View 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()
);

View File

@ -20,6 +20,7 @@
<activity>
<filters>
<filter>OCA\Files\Activity\Filter\FileChanges</filter>
<filter>OCA\Files\Activity\Filter\Favorites</filter>
</filters>
</activity>

View 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);
}
}

View File

@ -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);
}
}

View File

@ -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;
/**

View 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
);