From fde3c0b112fcafaa998ac1a2e30c7791b953a20e Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sat, 31 Jul 2021 22:07:05 +0300 Subject: [PATCH] Added response filter --- app/config/services.php | 16 +++++++- src/Appwrite/Utopia/Response.php | 2 + src/Appwrite/Utopia/Response/Model.php | 16 +++++++- .../Utopia/Response/Model/Project.php | 37 ++++++++++++++++--- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/app/config/services.php b/app/config/services.php index e3af7fbe50..9be3a717b6 100644 --- a/app/config/services.php +++ b/app/config/services.php @@ -9,6 +9,7 @@ return [ 'sdk' => false, 'docs' => false, 'tests' => false, + 'optional' => false, ], 'console' => [ 'key' => 'console', @@ -17,6 +18,7 @@ return [ 'sdk' => false, 'docs' => false, 'tests' => false, + 'optional' => false, ], 'account' => [ 'key' => 'account', @@ -27,6 +29,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'avatars' => [ 'key' => 'avatars', @@ -37,6 +40,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'database' => [ 'key' => 'database', @@ -47,6 +51,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'locale' => [ 'key' => 'locale', @@ -57,6 +62,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'health' => [ 'key' => 'health', @@ -67,6 +73,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'projects' => [ 'key' => 'projects', @@ -76,6 +83,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => false, ], 'storage' => [ 'key' => 'storage', @@ -86,6 +94,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'teams' => [ 'key' => 'teams', @@ -96,6 +105,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'users' => [ 'key' => 'users', @@ -106,6 +116,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'functions' => [ 'key' => 'functions', @@ -116,6 +127,7 @@ return [ 'sdk' => true, 'docs' => true, 'tests' => false, + 'optional' => true, ], 'mock' => [ 'key' => 'mock', @@ -126,6 +138,7 @@ return [ 'sdk' => false, 'docs' => false, 'tests' => true, + 'optional' => false, ], 'graphql' => [ 'key' => 'graphql', @@ -135,6 +148,7 @@ return [ 'controller' => 'api/graphql.php', 'sdk' => false, 'docs' => false, - 'tests' => false, + 'tests' => true, + 'optional' => false, ], ]; diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 981d18f4b2..94e82adde3 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -304,6 +304,8 @@ class Response extends SwooleResponse return $this->payload; } + $document = $model->filter($document); + foreach ($model->getRules() as $key => $rule) { if (!$document->isSet($key)) { if (!is_null($rule['default'])) { diff --git a/src/Appwrite/Utopia/Response/Model.php b/src/Appwrite/Utopia/Response/Model.php index af71a57596..7c6aafdbdd 100644 --- a/src/Appwrite/Utopia/Response/Model.php +++ b/src/Appwrite/Utopia/Response/Model.php @@ -2,6 +2,8 @@ namespace Appwrite\Utopia\Response; +use Utopia\Database\Document; + abstract class Model { const TYPE_STRING = 'string'; @@ -30,19 +32,29 @@ abstract class Model */ protected $rules = []; + /** + * Filter Document Structure + * + * @return string + */ + public function filter(Document $document): Document + { + return $document; + } + /** * Get Name * * @return string */ - abstract public function getName():string; + abstract public function getName(): string; /** * Get Collection * * @return string */ - abstract public function getType():string; + abstract public function getType(): string; /** * Get Rules diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index f74edb4e71..20a8d08d74 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -6,6 +6,7 @@ use stdClass; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; use Utopia\Config\Config; +use Utopia\Database\Document; class Project extends Model { @@ -173,14 +174,18 @@ class Project extends Model ; } - foreach ($services as $index => $service) { - $name = $method['name'] ?? ''; - $key = $method['key'] ?? ''; + foreach ($services as $service) { + if(!$service['optional']) { + continue; + } + + $name = $service['name'] ?? ''; + $key = $service['key'] ?? ''; $this - ->addRule($key, [ + ->addRule('serviceStatusFor'.ucfirst($key), [ 'type' => self::TYPE_BOOLEAN, - 'description' => $name.' auth method status', + 'description' => $name.' service status', 'example' => true, 'default' => true, ]) @@ -207,4 +212,26 @@ class Project extends Model { return Response::MODEL_PROJECT; } + + /** + * Get Collection + * + * @return string + */ + public function filter(Document $document): Document + { + $values = $document->getAttribute('services', []); + $services = Config::getParam('services', []); + + foreach($services as $service) { + if(!$service['optional']) { + continue; + } + $key = $service['key'] ?? ''; + $value = $values[$key] ?? true; + $document->setAttribute('serviceStatusFor'.$key, $value); + } + + return $document; + } } \ No newline at end of file