Merge pull request #174 from nextcloud/filter-api

Add an API to retrieve all filters
This commit is contained in:
Joas Schilling 2017-11-13 07:07:47 +01:00 коммит произвёл GitHub
Родитель 8d3f392906 a1ffe2cfa4
Коммит ea803d80a3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 93 добавлений и 19 удалений

Просмотреть файл

@ -24,6 +24,7 @@ return [
['name' => 'RemoteActivity#receiveActivity', 'url' => '/api/v2/remote/{token}', 'verb' => 'POST'],
['name' => 'APIv1#get', 'url' => '/activity', 'verb' => 'GET', 'root' => '/cloud'],
['name' => 'APIv2#getDefault', 'url' => '/api/v2/activity', 'verb' => 'GET'],
['name' => 'APIv2#listFilters', 'url' => '/api/v2/activity/filters', 'verb' => 'GET'],
['name' => 'APIv2#getFilter', 'url' => '/api/v2/activity/{filter}', 'verb' => 'GET'],
],
'routes' => [

Просмотреть файл

@ -42,6 +42,7 @@ class Capabilities implements ICapability {
'activity' => [
'apiv2' => [
'filters',
'filters-api',
'previews',
'rich-strings',
],

Просмотреть файл

@ -29,6 +29,7 @@ use OCA\Activity\Exception\InvalidFilterException;
use OCA\Activity\GroupHelper;
use OCA\Activity\UserSettings;
use OCA\Activity\ViewInfoCache;
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@ -67,7 +68,6 @@ class APIv2 extends OCSController {
/** @var bool */
protected $loadPreviews;
/** @var IManager */
protected $activityManager;
@ -208,6 +208,35 @@ class APIv2 extends OCSController {
return $this->get($filter, $since, $limit, $previews, $object_type, $object_id, $sort);
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function listFilters() {
$filters = $this->activityManager->getFilters();
$filters = array_map(function(IFilter $filter) {
return [
'id' => $filter->getIdentifier(),
'name' => $filter->getName(),
'icon' => $filter->getIcon(),
'priority' => $filter->getPriority(),
];
}, $filters);
// php 5.6 has problems with usort and objects
usort($filters, function(array $a, array $b) {
if ($a['priority'] === $b['priority']) {
return $a['id'] > $b['id'];
}
return $a['priority'] > $b['priority'];
});
return new DataResponse($filters);
}
/**
* @param string $filter
* @param int $since

Просмотреть файл

@ -34,6 +34,7 @@ class CapabilitiesTest extends TestCase {
'activity' => [
'apiv2' => [
'filters',
'filters-api',
'previews',
'rich-strings',
],

Просмотреть файл

@ -25,6 +25,7 @@ namespace OCA\Activity\Tests\Controller;
use OCA\Activity\Controller\APIv2;
use OCA\Activity\Exception\InvalidFilterException;
use OCA\Activity\Tests\TestCase;
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@ -134,25 +135,25 @@ class APIv2Test extends TestCase {
$this->view,
$this->infoCache
);
} else {
return $this->getMockBuilder(APIv2::class)
->setConstructorArgs([
'activity',
$this->request,
$this->activityManager,
$this->data,
$this->helper,
$this->userSettings,
$this->urlGenerator,
$this->userSession,
$this->preview,
$this->mimeTypeDetector,
$this->view,
$this->infoCache,
])
->setMethods($methods)
->getMock();
}
return $this->getMockBuilder(APIv2::class)
->setConstructorArgs([
'activity',
$this->request,
$this->activityManager,
$this->data,
$this->helper,
$this->userSettings,
$this->urlGenerator,
$this->userSession,
$this->preview,
$this->mimeTypeDetector,
$this->view,
$this->infoCache,
])
->setMethods($methods)
->getMock();
}
public function dataValidateParametersFilter() {
@ -435,6 +436,47 @@ class APIv2Test extends TestCase {
$this->assertSame($expected, $result->getStatus());
}
public function testListFilters() {
$filters = [
$this->createFilterMock(10, 'id1', 'Filter 3'),
$this->createFilterMock(10, 'abc', 'Filter 2'),
$this->createFilterMock(5, 'id3', 'Filter 1'),
];
$this->activityManager->expects($this->once())
->method('getFilters')
->willReturn($filters);
$controller = $this->getController();
$response = $controller->listFilters();
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame(Http::STATUS_OK, $response->getStatus());
$this->assertSame([
['id' => 'id3', 'name' => 'Filter 1', 'icon' => 'id35', 'priority' => 5],
['id' => 'abc', 'name' => 'Filter 2', 'icon' => 'abc10', 'priority' => 10],
['id' => 'id1', 'name' => 'Filter 3', 'icon' => 'id110', 'priority' => 10],
], $response->getData());
}
protected function createFilterMock($priority, $id, $name) {
$filter = $this->createMock(IFilter::class);
$filter->expects($this->any())
->method('getPriority')
->willReturn($priority);
$filter->expects($this->any())
->method('getIdentifier')
->willReturn($id);
$filter->expects($this->any())
->method('getName')
->willReturn($name);
$filter->expects($this->any())
->method('getIcon')
->willReturn($id . $priority);
return $filter;
}
public function dataGet() {
return [
[123456789, 'files', 42, [], false, 0, ['object_type' => 'files', 'object_id' => 42, 'datetime' => date(\DateTime::ATOM, 123456789)]],