From f270e47b48ba7450bdf0c6d6f2e8ca40a89cea85 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 14 Dec 2025 07:50:21 +0000 Subject: [PATCH] more fixes to audit queries --- app/controllers/api/account.php | 10 +++++---- app/controllers/api/messaging.php | 22 ++++++++++++++----- app/controllers/api/teams.php | 7 ++++-- .../Collections/Documents/Logs/XList.php | 7 ++++-- .../Http/Databases/Collections/Logs/XList.php | 17 ++++++++------ .../Databases/Http/Databases/Logs/XList.php | 17 ++++++++------ .../Databases/Http/TablesDB/Logs/XList.php | 17 ++++++++------ 7 files changed, 62 insertions(+), 35 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index ada4a98de9..df1c6a35aa 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2972,7 +2972,8 @@ App::get('/v1/account/logs') ->inject('locale') ->inject('geodb') ->inject('dbForProject') - ->action(function (array $queries, bool $includeTotal, Response $response, Document $user, Locale $locale, Reader $geodb, Database $dbForProject) { + ->inject('audit') + ->action(function (array $queries, bool $includeTotal, Response $response, Document $user, Locale $locale, Reader $geodb, Database $dbForProject, Audit $audit) { try { $queries = Query::parseQueries($queries); @@ -2980,9 +2981,10 @@ App::get('/v1/account/logs') throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } - $audit = new EventAudit($dbForProject); - - $logs = $audit->getLogsByUser($user->getSequence(), $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByUser($user->getSequence(), offset: $offset, limit: $limit); $output = []; diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index eed12c3376..72abce087b 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -1211,7 +1211,7 @@ App::get('/v1/messaging/providers/:providerId/logs') } $response->dynamic(new Document([ - 'total' => $includeTotal ? $audit->countLogsByResource($resource, $queries) : 0, + 'total' => $includeTotal ? $audit->countLogsByResource($resource, $filterQueries) : 0, 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -2568,7 +2568,10 @@ App::get('/v1/messaging/topics/:topicId/logs') } $resource = 'topic/' . $topicId; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -2616,7 +2619,7 @@ App::get('/v1/messaging/topics/:topicId/logs') } $response->dynamic(new Document([ - 'total' => $includeTotal ? $audit->countLogsByResource($resource, $queries) : 0, + 'total' => $includeTotal ? $audit->countLogsByResource($resource) : 0, 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -2985,7 +2988,10 @@ App::get('/v1/messaging/subscribers/:subscriberId/logs') } $resource = 'subscriber/' . $subscriberId; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -3033,7 +3039,7 @@ App::get('/v1/messaging/subscribers/:subscriberId/logs') } $response->dynamic(new Document([ - 'total' => $includeTotal ? $audit->countLogsByResource($resource, $queries) : 0, + 'total' => $includeTotal ? $audit->countLogsByResource($resource) : 0, 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -3780,7 +3786,11 @@ App::get('/v1/messaging/messages/:messageId/logs') } $resource = 'message/' . $messageId; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $filterQueries = $grouped['filters'] ?? []; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 04a43577ad..cc35f0d264 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -1480,7 +1480,10 @@ App::get('/v1/teams/:teamId/logs') } $resource = 'team/' . $team->getId(); - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -1527,7 +1530,7 @@ App::get('/v1/teams/:teamId/logs') } } $response->dynamic(new Document([ - 'total' => $includeTotal ? $audit->countLogsByResource($resource, $queries) : 0, + 'total' => $includeTotal ? $audit->countLogsByResource($resource) : 0, 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php index 292bed4c36..e0b595ebd1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php @@ -103,7 +103,10 @@ class XList extends Action $context = $this->getContext(); $resource = "database/$databaseId/$type/$collectionId/$context/{$document->getId()}"; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -152,7 +155,7 @@ class XList extends Action $response->dynamic(new Document([ 'logs' => $output, - 'total' => $audit->countLogsByResource($resource, $queries), + 'total' => $audit->countLogsByResource($resource), ]), $this->getResponseModel()); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php index 2244bfd2d7..73c615697a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php @@ -67,11 +67,11 @@ class XList extends Action ->param('databaseId', '', new UID(), 'Database ID.') ->param('collectionId', '', new UID(), 'Collection ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('response') - ->inject('dbForProject') - ->inject('locale') - ->inject('geodb') - ->inject('audit') + ->inject('response') + ->inject('dbForProject') + ->inject('locale') + ->inject('geodb') + ->inject('audit') ->callback($this->action(...)); } @@ -98,7 +98,10 @@ class XList extends Action $context = $this->getContext(); $resource = "database/$databaseId/$context/$collectionId"; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -147,7 +150,7 @@ class XList extends Action $response->dynamic(new Document([ 'logs' => $output, - 'total' => $audit->countLogsByResource($resource, $queries), + 'total' => $audit->countLogsByResource($resource), ]), $this->getResponseModel()); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php index be30a0fad8..1d828977c4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php @@ -63,11 +63,11 @@ class XList extends Action ]) ->param('databaseId', '', new UID(), 'Database ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('response') - ->inject('dbForProject') - ->inject('locale') - ->inject('geodb') - ->inject('audit') + ->inject('response') + ->inject('dbForProject') + ->inject('locale') + ->inject('geodb') + ->inject('audit') ->callback($this->action(...)); } @@ -86,7 +86,10 @@ class XList extends Action } $resource = 'database/' . $databaseId; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -133,7 +136,7 @@ class XList extends Action } $response->dynamic(new Document([ - 'total' => $audit->countLogsByResource($resource, $queries), + 'total' => $audit->countLogsByResource($resource), 'logs' => $output, ]), UtopiaResponse::MODEL_LOG_LIST); } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php index ed2aaa848c..b37d38dfe1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php @@ -58,11 +58,11 @@ class XList extends Action ]) ->param('databaseId', '', new UID(), 'Database ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('response') - ->inject('dbForProject') - ->inject('locale') - ->inject('geodb') - ->inject('audit') + ->inject('response') + ->inject('dbForProject') + ->inject('locale') + ->inject('geodb') + ->inject('audit') ->callback($this->action(...)); } @@ -81,7 +81,10 @@ class XList extends Action } $resource = 'database/' . $databaseId; - $logs = $audit->getLogsByResource($resource, $queries); + $grouped = Query::groupByType($queries); + $limit = $grouped['limit'] ?? 25; + $offset = $grouped['offset'] ?? 0; + $logs = $audit->getLogsByResource($resource, offset: $offset, limit: $limit); $output = []; @@ -128,7 +131,7 @@ class XList extends Action } $response->dynamic(new Document([ - 'total' => $audit->countLogsByResource($resource, $queries), + 'total' => $audit->countLogsByResource($resource), 'logs' => $output, ]), UtopiaResponse::MODEL_LOG_LIST); }