From 058b4dd97571092514f6c3a9984f47aef57961b8 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 16 Nov 2021 12:20:14 +0100 Subject: [PATCH] fix(api): add after pagination to logs endpoints --- app/controllers/api/account.php | 13 +++++++++++-- app/controllers/api/database.php | 11 ++++++++++- app/controllers/api/users.php | 13 +++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index be0532a460..db408d0548 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1185,13 +1185,14 @@ App::get('/v1/account/logs') ->label('sdk.response.model', Response::MODEL_LOG_LIST) ->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true) ->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true) + ->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true) ->inject('response') ->inject('user') ->inject('locale') ->inject('geodb') ->inject('dbForInternal') ->inject('usage') - ->action(function ($limit, $offset, $response, $user, $locale, $geodb, $dbForInternal, $usage) { + ->action(function ($limit, $offset, $after, $response, $user, $locale, $geodb, $dbForInternal, $usage) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Document $project */ /** @var Utopia\Database\Document $user */ @@ -1200,6 +1201,14 @@ App::get('/v1/account/logs') /** @var Utopia\Database\Database $dbForInternal */ /** @var Appwrite\Stats\Stats $usage */ + if (!empty($after)) { + $afterLog = $dbForInternal->getDocument('audit', $after); + + if ($afterLog->isEmpty()) { + throw new Exception("Log '{$after}' for the 'after' value not found.", 400); + } + } + $audit = new Audit($dbForInternal); $logs = $audit->getLogsByUserAndEvents($user->getId(), [ @@ -1218,7 +1227,7 @@ App::get('/v1/account/logs') 'teams.membership.create', 'teams.membership.update', 'teams.membership.delete', - ], $limit, $offset); + ], $limit, $offset, $afterLog ?? null); $output = []; diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 200b2cff04..ce83c1abe3 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -460,6 +460,7 @@ App::get('/v1/database/collections/:collectionId/logs') ->param('collectionId', '', new UID(), 'Collection unique ID.') ->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true) ->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true) + ->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true) ->inject('response') ->inject('dbForInternal') ->inject('dbForExternal') @@ -479,9 +480,17 @@ App::get('/v1/database/collections/:collectionId/logs') throw new Exception('Collection not found', 404); } + if (!empty($after)) { + $afterLog = $dbForInternal->getDocument('audit', $after); + + if ($afterLog->isEmpty()) { + throw new Exception("Log '{$after}' for the 'after' value not found.", 400); + } + } + $audit = new Audit($dbForInternal); - $logs = $audit->getLogsByResource('collection/'.$collection->getId(), $limit, $offset); + $logs = $audit->getLogsByResource('collection/'.$collection->getId(), $limit, $offset, $afterLog ?? null); $output = []; diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 489b807705..5cca14404e 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -261,12 +261,13 @@ App::get('/v1/users/:userId/logs') ->param('userId', '', new UID(), 'User unique ID.') ->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true) ->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true) + ->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true) ->inject('response') ->inject('dbForInternal') ->inject('locale') ->inject('geodb') ->inject('usage') - ->action(function ($userId, $limit, $offset, $response, $dbForInternal, $locale, $geodb, $usage) { + ->action(function ($userId, $limit, $offset, $after, $response, $dbForInternal, $locale, $geodb, $usage) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Document $project */ /** @var Utopia\Database\Database $dbForInternal */ @@ -280,6 +281,14 @@ App::get('/v1/users/:userId/logs') throw new Exception('User not found', 404); } + if (!empty($after)) { + $afterLog = $dbForInternal->getDocument('audit', $after); + + if ($afterLog->isEmpty()) { + throw new Exception("Log '{$after}' for the 'after' value not found.", 400); + } + } + $audit = new Audit($dbForInternal); $logs = $audit->getLogsByUserAndEvents($user->getId(), [ @@ -298,7 +307,7 @@ App::get('/v1/users/:userId/logs') 'teams.membership.create', 'teams.membership.update', 'teams.membership.delete', - ], $limit, $offset); + ], $limit, $offset, $afterLog ?? null); $output = [];