Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 18 additions & 67 deletions lib/Controller/APIv2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,14 @@
use OCP\Notification\IManager as INotificationManager;

class APIv2Controller extends OCSController {
/** @var string */
protected $filter;

/** @var int */
protected $since;

/** @var int */
protected $limit;

/** @var string */
protected $sort;

/** @var string */
protected $objectType;

/** @var int */
protected $objectId;

/** @var string */
protected $user;

/** @var bool */
protected $loadPreviews;
protected string $filter = 'all';
protected int $since = 0;
protected int $limit = 50;
protected string $sort = 'desc';
protected string $objectType = '';
protected int $objectId = 0;
protected string $user = '';
protected bool $loadPreviews = false;

public function __construct(
$appName,
Expand All @@ -71,26 +56,19 @@ public function __construct(
}

/**
* @param string $filter
* @param int $since
* @param int $limit
* @param bool $previews
* @param string $objectType
* @param int $objectId
* @param string $sort
* @throws InvalidFilterException when the filter is invalid
* @throws \OutOfBoundsException when no user is given
*/
protected function validateParameters($filter, $since, $limit, $previews, $objectType, $objectId, $sort) {
$this->filter = \is_string($filter) ? $filter : 'all';
protected function validateParameters(string $filter, int $since, int $limit, bool $previews, string $objectType, int $objectId, string $sort): void {
$this->filter = $filter;
if ($this->filter !== $this->data->validateFilter($this->filter)) {
throw new InvalidFilterException('Invalid filter');
}
$this->since = (int)$since;
$this->limit = (int)$limit;
$this->loadPreviews = (bool)$previews;
$this->objectType = (string)$objectType;
$this->objectId = (int)$objectId;
$this->since = $since;
$this->limit = $limit;
$this->loadPreviews = $previews;
$this->objectType = $objectType;
$this->objectId = $objectId;
$this->sort = \in_array($sort, ['asc', 'desc'], true) ? $sort : 'desc';

if (($this->objectType !== '' && $this->objectId === 0) || ($this->objectType === '' && $this->objectId !== 0)) {
Expand All @@ -110,32 +88,15 @@ protected function validateParameters($filter, $since, $limit, $previews, $objec

/**
* @NoAdminRequired
*
* @param int $since
* @param int $limit
* @param bool $previews
* @param string $object_type
* @param int $object_id
* @param string $sort
* @return DataResponse
*/
public function getDefault($since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc'): DataResponse {
public function getDefault(int $since = 0, int $limit = 50, bool $previews = false, string $object_type = '', int $object_id = 0, string $sort = 'desc'): DataResponse {
return $this->get('all', $since, $limit, $previews, $object_type, $object_id, $sort);
}

/**
* @NoAdminRequired
*
* @param string $filter
* @param int $since
* @param int $limit
* @param bool $previews
* @param string $object_type
* @param int $object_id
* @param string $sort
* @return DataResponse
*/
public function getFilter($filter, $since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc'): DataResponse {
public function getFilter(string $filter, int $since = 0, int $limit = 50, bool $previews = false, string $object_type = '', int $object_id = 0, string $sort = 'desc'): DataResponse {
return $this->get($filter, $since, $limit, $previews, $object_type, $object_id, $sort);
}

Expand Down Expand Up @@ -191,17 +152,7 @@ public function listFilters(): DataResponse {
return new DataResponse($filters);
}

/**
* @param string $filter
* @param int $since
* @param int $limit
* @param bool $previews
* @param string $filterObjectType
* @param int $filterObjectId
* @param string $sort
* @return DataResponse
*/
protected function get($filter, $since, $limit, $previews, $filterObjectType, $filterObjectId, $sort): DataResponse {
protected function get(string $filter, int $since, int $limit, bool $previews, string $filterObjectType, int $filterObjectId, string $sort): DataResponse {
try {
$this->validateParameters($filter, $since, $limit, $previews, $filterObjectType, $filterObjectId, $sort);
} catch (InvalidFilterException $e) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function storeMail(IEvent $event, int $latestSendTime): bool {
* @return array
*
*/
public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0, bool $returnEvents = false) {
public function get(GroupHelper $groupHelper, UserSettings $userSettings, string $user, int $since, int $limit, string $sort, string $filter, string $objectType = '', int $objectId = 0, bool $returnEvents = false): array {
// get current user
if ($user === '') {
throw new \OutOfBoundsException('Invalid user', 1);
Expand Down Expand Up @@ -323,15 +323,15 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user,
*
* @throws \OutOfBoundsException If $since is not owned by $user
*/
protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) {
protected function setOffsetFromSince(IQueryBuilder $query, string $user, int $since, string $sort): array {
if (!$since) {
return $this->getFirstKnownActivityHeader($user, $sort);
}

$queryBuilder = $this->connection->getQueryBuilder();
$queryBuilder->select(['affecteduser', 'timestamp'])
->from('activity')
->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int)$since)));
->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter($since)));
$result = $queryBuilder->executeQuery();
$activity = $result->fetch();
$result->closeCursor();
Expand Down
18 changes: 12 additions & 6 deletions tests/Controller/APIv2ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ public function testValidateParametersFilter(string $param, string $filter): voi
->method('validateFilter')
->with($param)
->willReturn($filter);
$userMock = $this->createMock(IUser::class);
$userMock->method('getUID')->willReturn('testuser');
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($this->createMock(IUser::class));
->willReturn($userMock);

self::invokePrivate($this->controller, 'validateParameters', [$param, 0, 0, false, '', 0, 'desc']);
$this->assertSame($filter, self::invokePrivate($this->controller, 'filter'));
Expand Down Expand Up @@ -173,19 +175,21 @@ public static function dataValidateParametersObject(): array {
return [
['type', 42, 'type', 42],
['type', '42', 'type', 42],
[null, '42', '', 0],
['type', null, '', 0],
['', 42, '', 0],
['type', 0, '', 0],
];
}

#[DataProvider('dataValidateParametersObject')]
public function testValidateParametersObject(?string $type, mixed $id, string $expectedType, int $expectedId): void {
public function testValidateParametersObject(string $type, mixed $id, string $expectedType, int $expectedId): void {
$this->data->expects($this->once())
->method('validateFilter')
->willReturnArgument(0);
$userMock = $this->createMock(IUser::class);
$userMock->method('getUID')->willReturn('testuser');
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($this->createMock(IUser::class));
->willReturn($userMock);

self::invokePrivate($this->controller, 'validateParameters', ['all', 0, 0, false, $type, $id, 'desc']);
$this->assertSame($expectedType, self::invokePrivate($this->controller, 'objectType'));
Expand Down Expand Up @@ -229,9 +233,11 @@ public function testValidateParameters(string $param, mixed $value, string $memb
$this->data->expects($this->once())
->method('validateFilter')
->willReturnArgument(0);
$userMock = $this->createMock(IUser::class);
$userMock->method('getUID')->willReturn('testuser');
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($this->createMock(IUser::class));
->willReturn($userMock);

self::invokePrivate($this->controller, 'validateParameters', $params);
$this->assertSame($expectedValue, self::invokePrivate($this->controller, $memberName));
Expand Down
Loading