From 69cc4f0bb6d6f5b6f6f99c559612e919fa23d514 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 7 May 2025 17:32:54 +0530 Subject: [PATCH] update: handle the context logic, use defaults. --- app/init/constants.php | 10 +++++++ .../Databases/Http/Collections/Action.php | 30 ++++--------------- .../Databases/Http/Collections/Create.php | 4 --- .../Databases/Http/Collections/Delete.php | 4 --- .../Databases/Http/Collections/Get.php | 4 --- .../Databases/Http/Collections/Logs/XList.php | 4 --- .../Databases/Http/Collections/Update.php | 4 --- .../Databases/Http/Collections/Usage/Get.php | 4 --- .../Databases/Http/Collections/XList.php | 4 --- .../Modules/Databases/Http/Tables/Create.php | 2 +- .../Modules/Databases/Http/Tables/Delete.php | 2 +- .../Modules/Databases/Http/Tables/Get.php | 2 +- .../Databases/Http/Tables/Logs/XList.php | 2 +- .../Modules/Databases/Http/Tables/Update.php | 2 +- .../Databases/Http/Tables/Usage/Get.php | 2 +- .../Modules/Databases/Http/Tables/XList.php | 2 +- 16 files changed, 22 insertions(+), 60 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index 2b15f9fa0b..80d4796aad 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -257,3 +257,13 @@ const RESOURCE_TYPE_PROVIDERS = 'providers'; const RESOURCE_TYPE_TOPICS = 'topics'; const RESOURCE_TYPE_SUBSCRIBERS = 'subscribers'; const RESOURCE_TYPE_MESSAGES = 'messages'; + +// Context constants for database + +const DATABASE_ROWS_CONTEXT = 'row'; +const DATABASE_TABLES_CONTEXT = 'table'; +const DATABASE_COLUMNS_CONTEXT = 'column'; + +const DATABASE_DOCUMENTS_CONTEXT = 'document'; +const DATABASE_ATTRIBUTES_CONTEXT = 'attribute'; +const DATABASE_COLLECTIONS_CONTEXT = 'collection'; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Action.php index d2129f31ea..d00bc1f9f9 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Action.php @@ -13,26 +13,21 @@ use Utopia\Platform\Action as UtopiaAction; */ abstract class Action extends UtopiaAction { - /** - * Valid context identifiers. - */ - public const TABLE = 'table'; - public const COLLECTION = 'collection'; /** * The current API context (either 'table' or 'collection'). */ - private ?string $context = null; + private ?string $context = DATABASE_COLLECTIONS_CONTEXT; /** * Set the current API context. * - * @param string $context Must be either `self::TABLE` or `self::COLLECTION`. + * @param string $context Must be either `DATABASE_TABLES_CONTEXT` or `DATABASE_COLLECTIONS_CONTEXT`. */ final protected function setContext(string $context): void { - if (!\in_array($context, [self::TABLE, self::COLLECTION], true)) { - throw new \InvalidArgumentException("Invalid context '$context'. Must be either `Action::TABLE` or `Action::COLLECTION`."); + if (!\in_array($context, [DATABASE_TABLES_CONTEXT, DATABASE_COLLECTIONS_CONTEXT], true)) { + throw new \InvalidArgumentException("Invalid context '$context'. Must be either `DATABASE_TABLES_CONTEXT` or `DATABASE_COLLECTIONS_CONTEXT`."); } $this->context = $context; @@ -45,10 +40,6 @@ abstract class Action extends UtopiaAction */ final protected function getContext(): string { - if ($this->context === null) { - throw new \Exception('Missing context: you must call setContext() with either `Action::TABLE` or `Action::COLLECTION` before using this method.'); - } - return $this->context; } @@ -70,7 +61,7 @@ abstract class Action extends UtopiaAction */ final protected function isCollectionsAPI(): bool { - return $this->getContext() === self::COLLECTION; + return $this->getContext() === DATABASE_COLLECTIONS_CONTEXT; } /** @@ -110,15 +101,4 @@ abstract class Action extends UtopiaAction ? Exception::COLLECTION_LIMIT_EXCEEDED : Exception::TABLE_LIMIT_EXCEEDED; } - - /** - * Ensures that a valid context has been set. - * - * @throws \Exception if context is missing - */ - final protected function validateContext(): void - { - $this->getContext(); // Triggers exception if not set - } - } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Create.php index d0320c6dd0..063ce75720 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Create.php @@ -40,8 +40,6 @@ class Create extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_POST) ->setHttpPath('/v1/databases/:databaseId/collections') @@ -80,8 +78,6 @@ class Create extends Action public function action(string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Delete.php index d30c37efed..7cec4addea 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Delete.php @@ -32,8 +32,6 @@ class Delete extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_DELETE) ->setHttpPath('/v1/databases/:databaseId/collections/:collectionId') @@ -69,8 +67,6 @@ class Delete extends Action public function action(string $databaseId, string $collectionId, UtopiaResponse $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Get.php index 962ad3d21d..e7f02b8930 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Get.php @@ -30,8 +30,6 @@ class Get extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) ->setHttpPath('/v1/databases/:databaseId/collections/:collectionId') @@ -62,8 +60,6 @@ class Get extends Action public function action(string $databaseId, string $collectionId, UtopiaResponse $response, Database $dbForProject): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Logs/XList.php index 429c39dc44..bd31ad2fe1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Logs/XList.php @@ -42,8 +42,6 @@ class XList extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) ->setHttpPath('/v1/databases/:databaseId/collections/:collectionId/logs') @@ -77,8 +75,6 @@ class XList extends Action public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, Locale $locale, Reader $geodb): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Update.php index 582f0e84a3..8c8dfb58b5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Update.php @@ -35,8 +35,6 @@ class Update extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_PUT) ->setHttpPath('/v1/databases/:databaseId/collections/:collectionId') @@ -75,8 +73,6 @@ class Update extends Action public function action(string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Usage/Get.php index a9a924114f..60c7af4420 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/Usage/Get.php @@ -35,8 +35,6 @@ class Get extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) ->setHttpPath('/v1/databases/:databaseId/collections/:collectionId/usage') @@ -68,8 +66,6 @@ class Get extends Action public function action(string $databaseId, string $range, string $collectionId, UtopiaResponse $response, Database $dbForProject): void { - $this->validateContext(); - $database = $dbForProject->getDocument('databases', $databaseId); $collectionDocument = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId); $collection = $dbForProject->getCollection('database_' . $database->getInternalId() . '_collection_' . $collectionDocument->getInternalId()); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Collections/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Collections/XList.php index e540e86c06..98246defaa 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Collections/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Collections/XList.php @@ -36,8 +36,6 @@ class XList extends Action public function __construct() { - $this->setContext(Action::COLLECTION); - $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) ->setHttpPath('/v1/databases/:databaseId/collections') @@ -69,8 +67,6 @@ class XList extends Action public function action(string $databaseId, array $queries, string $search, UtopiaResponse $response, Database $dbForProject): void { - $this->validateContext(); - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Create.php index 6f30c897b4..8a6c009e87 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Create.php @@ -35,7 +35,7 @@ class Create extends CollectionCreate public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_POST) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Delete.php index 810edf7f21..e06c19b308 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Delete.php @@ -32,7 +32,7 @@ class Delete extends CollectionDelete public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_DELETE) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Get.php index 80798a4516..e5f2954de5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Get.php @@ -30,7 +30,7 @@ class Get extends CollectionGet public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Logs/XList.php index b3ce8cd272..67ff31efb7 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Logs/XList.php @@ -35,7 +35,7 @@ class XList extends CollectionLogXList public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Update.php index fde9114e59..f27232353e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Update.php @@ -34,7 +34,7 @@ class Update extends CollectionUpdate public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_PUT) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Usage/Get.php index 9ba99d32e1..647a98d952 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/Usage/Get.php @@ -31,7 +31,7 @@ class Get extends CollectionUsageGet public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Tables/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Tables/XList.php index f90a1f66f2..77eb556301 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Tables/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Tables/XList.php @@ -32,7 +32,7 @@ class XList extends CollectionXList public function __construct() { - $this->setContext(Action::TABLE); + $this->setContext(DATABASE_TABLES_CONTEXT); $this ->setHttpMethod(self::HTTP_REQUEST_METHOD_GET)