Merge branch 'upgraded-webhooks' of github.com:appwrite/appwrite into swoole-and-functions
This commit is contained in:
commit
e3086ba789
25
app/app.php
25
app/app.php
|
@ -21,7 +21,7 @@ Config::setParam('domainVerification', false);
|
|||
Config::setParam('cookieDomain', 'localhost');
|
||||
Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE);
|
||||
|
||||
App::init(function ($utopia, $request, $response, $console, $project, $user, $locale, $webhooks, $audits, $usage, $clients) {
|
||||
App::init(function ($utopia, $request, $response, $console, $project, $user, $locale, $webhooks, $audits, $usage, $functions, $clients) {
|
||||
/** @var Appwrite\Swoole\Request $request */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $console */
|
||||
|
@ -31,6 +31,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo
|
|||
/** @var Appwrite\Event\Event $webhooks */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
/** @var Appwrite\Event\Event $functions */
|
||||
/** @var bool $mode */
|
||||
/** @var array $clients */
|
||||
|
||||
|
@ -217,9 +218,18 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo
|
|||
/*
|
||||
* Background Jobs
|
||||
*/
|
||||
$functions
|
||||
->setParam('projectId', $project->getId())
|
||||
->setParam('event', $route->getLabel('events', ''))
|
||||
->setParam('payload', [])
|
||||
->setParam('functionId', null)
|
||||
->setParam('executionId', null)
|
||||
->setParam('trigger', 'event')
|
||||
;
|
||||
|
||||
$webhooks
|
||||
->setParam('projectId', $project->getId())
|
||||
->setParam('event', $route->getLabel('webhook', ''))
|
||||
->setParam('event', $route->getLabel('events', ''))
|
||||
->setParam('payload', [])
|
||||
;
|
||||
|
||||
|
@ -242,9 +252,9 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo
|
|||
->setParam('networkResponseSize', 0)
|
||||
->setParam('storage', 0)
|
||||
;
|
||||
}, ['utopia', 'request', 'response', 'console', 'project', 'user', 'locale', 'webhooks', 'audits', 'usage', 'clients']);
|
||||
}, ['utopia', 'request', 'response', 'console', 'project', 'user', 'locale', 'webhooks', 'audits', 'usage', 'functions', 'clients']);
|
||||
|
||||
App::shutdown(function ($utopia, $request, $response, $project, $webhooks, $audits, $usage, $deletes, $mode) {
|
||||
App::shutdown(function ($utopia, $request, $response, $project, $webhooks, $audits, $usage, $deletes, $functions, $mode) {
|
||||
/** @var Utopia\App $utopia */
|
||||
/** @var Utopia\Request $request */
|
||||
/** @var Utopia\Response $response */
|
||||
|
@ -253,8 +263,13 @@ App::shutdown(function ($utopia, $request, $response, $project, $webhooks, $audi
|
|||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
/** @var Appwrite\Event\Event $deletes */
|
||||
/** @var Appwrite\Event\Event $functions */
|
||||
/** @var bool $mode */
|
||||
|
||||
if (!empty($functions->getParam('event'))) {
|
||||
$functions->trigger();
|
||||
}
|
||||
|
||||
if (!empty($webhooks->getParam('event'))) {
|
||||
$webhooks->trigger();
|
||||
}
|
||||
|
@ -279,7 +294,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $webhooks, $audi
|
|||
->trigger()
|
||||
;
|
||||
}
|
||||
}, ['utopia', 'request', 'response', 'project', 'webhooks', 'audits', 'usage', 'deletes', 'mode']);
|
||||
}, ['utopia', 'request', 'response', 'project', 'webhooks', 'audits', 'usage', 'deletes', 'functions', 'mode']);
|
||||
|
||||
App::options(function ($request, $response) {
|
||||
/** @var Appwrite\Swoole\Request $request */
|
||||
|
|
|
@ -1386,6 +1386,15 @@ $collections = [
|
|||
'required' => false,
|
||||
'array' => false,
|
||||
],
|
||||
[
|
||||
'$collection' => Database::SYSTEM_COLLECTION_RULES,
|
||||
'label' => 'Trigger',
|
||||
'key' => 'trigger',
|
||||
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'array' => false,
|
||||
],
|
||||
[
|
||||
'$collection' => Database::SYSTEM_COLLECTION_RULES,
|
||||
'label' => 'Status',
|
||||
|
|
|
@ -44,7 +44,7 @@ App::init(function() use (&$oauth2Keys) {
|
|||
App::post('/v1/account')
|
||||
->desc('Create Account')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.create')
|
||||
->label('event', 'account.create')
|
||||
->label('scope', 'public')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -149,7 +149,7 @@ App::post('/v1/account')
|
|||
App::post('/v1/account/sessions')
|
||||
->desc('Create Account Session')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.sessions.create')
|
||||
->label('event', 'account.sessions.create')
|
||||
->label('scope', 'public')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -348,7 +348,7 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
|
|||
->desc('OAuth2 Redirect')
|
||||
->groups(['api', 'account'])
|
||||
->label('error', __DIR__.'/../../views/general/error.phtml')
|
||||
->label('webhook', 'account.sessions.create')
|
||||
->label('event', 'account.sessions.create')
|
||||
->label('scope', 'public')
|
||||
->label('abuse-limit', 50)
|
||||
->label('abuse-key', 'ip:{ip}')
|
||||
|
@ -732,7 +732,7 @@ App::get('/v1/account/logs')
|
|||
App::patch('/v1/account/name')
|
||||
->desc('Update Account Name')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.update.name')
|
||||
->label('event', 'account.update.name')
|
||||
->label('scope', 'account')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -773,7 +773,7 @@ App::patch('/v1/account/name')
|
|||
App::patch('/v1/account/password')
|
||||
->desc('Update Account Password')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.update.password')
|
||||
->label('event', 'account.update.password')
|
||||
->label('scope', 'account')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -819,7 +819,7 @@ App::patch('/v1/account/password')
|
|||
App::patch('/v1/account/email')
|
||||
->desc('Update Account Email')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.update.email')
|
||||
->label('event', 'account.update.email')
|
||||
->label('scope', 'account')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -880,7 +880,7 @@ App::patch('/v1/account/email')
|
|||
App::patch('/v1/account/prefs')
|
||||
->desc('Update Account Preferences')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.update.prefs')
|
||||
->label('event', 'account.update.prefs')
|
||||
->label('scope', 'account')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -924,7 +924,7 @@ App::patch('/v1/account/prefs')
|
|||
App::delete('/v1/account')
|
||||
->desc('Delete Account')
|
||||
->groups(['api', 'account'])
|
||||
->label('webhook', 'account.delete')
|
||||
->label('event', 'account.delete')
|
||||
->label('scope', 'account')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
|
@ -986,7 +986,7 @@ App::delete('/v1/account/sessions/:sessionId')
|
|||
->desc('Delete Account Session')
|
||||
->groups(['api', 'account'])
|
||||
->label('scope', 'account')
|
||||
->label('webhook', 'account.sessions.delete')
|
||||
->label('event', 'account.sessions.delete')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
->label('sdk.method', 'deleteSession')
|
||||
|
@ -1051,7 +1051,7 @@ App::delete('/v1/account/sessions')
|
|||
->desc('Delete All Account Sessions')
|
||||
->groups(['api', 'account'])
|
||||
->label('scope', 'account')
|
||||
->label('webhook', 'account.sessions.delete')
|
||||
->label('event', 'account.sessions.delete')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT])
|
||||
->label('sdk.namespace', 'account')
|
||||
->label('sdk.method', 'deleteSessions')
|
||||
|
|
|
@ -24,7 +24,7 @@ use Appwrite\Database\Exception\Structure as StructureException;
|
|||
App::post('/v1/database/collections')
|
||||
->desc('Create Collection')
|
||||
->groups(['api', 'database'])
|
||||
->label('webhook', 'database.collections.create')
|
||||
->label('event', 'database.collections.create')
|
||||
->label('scope', 'collections.write')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_SERVER])
|
||||
|
@ -220,7 +220,7 @@ App::put('/v1/database/collections/:collectionId')
|
|||
->desc('Update Collection')
|
||||
->groups(['api', 'database'])
|
||||
->label('scope', 'collections.write')
|
||||
->label('webhook', 'database.collections.update')
|
||||
->label('event', 'database.collections.update')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_SERVER])
|
||||
->label('sdk.method', 'updateCollection')
|
||||
|
@ -296,7 +296,7 @@ App::delete('/v1/database/collections/:collectionId')
|
|||
->desc('Delete Collection')
|
||||
->groups(['api', 'database'])
|
||||
->label('scope', 'collections.write')
|
||||
->label('webhook', 'database.collections.delete')
|
||||
->label('event', 'database.collections.delete')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_SERVER])
|
||||
->label('sdk.method', 'deleteCollection')
|
||||
|
@ -336,7 +336,7 @@ App::delete('/v1/database/collections/:collectionId')
|
|||
App::post('/v1/database/collections/:collectionId/documents')
|
||||
->desc('Create Document')
|
||||
->groups(['api', 'database'])
|
||||
->label('webhook', 'database.documents.create')
|
||||
->label('event', 'database.documents.create')
|
||||
->label('scope', 'documents.write')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
|
@ -568,7 +568,7 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId')
|
|||
App::patch('/v1/database/collections/:collectionId/documents/:documentId')
|
||||
->desc('Update Document')
|
||||
->groups(['api', 'database'])
|
||||
->label('webhook', 'database.documents.update')
|
||||
->label('event', 'database.documents.update')
|
||||
->label('scope', 'documents.write')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
|
@ -652,7 +652,7 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
|
|||
->desc('Delete Document')
|
||||
->groups(['api', 'database'])
|
||||
->label('scope', 'documents.write')
|
||||
->label('webhook', 'database.documents.delete')
|
||||
->label('event', 'database.documents.delete')
|
||||
->label('sdk.namespace', 'database')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
->label('sdk.method', 'deleteDocument')
|
||||
|
|
|
@ -577,6 +577,7 @@ App::post('/v1/functions/:functionId/executions')
|
|||
],
|
||||
'dateCreated' => time(),
|
||||
'functionId' => $function->getId(),
|
||||
'trigger' => 'http',
|
||||
'status' => 'waiting', // waiting / processing / completed / failed
|
||||
'trigger' => 'http', // http / schedule / event
|
||||
'exitCode' => 0,
|
||||
|
@ -588,14 +589,13 @@ App::post('/v1/functions/:functionId/executions')
|
|||
if (false === $execution) {
|
||||
throw new Exception('Failed saving execution to DB', 500);
|
||||
}
|
||||
|
||||
|
||||
// Issue a TLS certificate when domain is verified
|
||||
Resque::enqueue('v1-functions', 'FunctionsV1', [
|
||||
'projectId' => $project->getId(),
|
||||
'functionId' => $function->getId(),
|
||||
'executionId' => $execution->getId(),
|
||||
'functionTag' => $tag->getId(),
|
||||
'functionTrigger' => 'http',
|
||||
'trigger' => 'http',
|
||||
]);
|
||||
|
||||
$response
|
||||
|
|
|
@ -26,7 +26,7 @@ App::post('/v1/storage/files')
|
|||
->desc('Create File')
|
||||
->groups(['api', 'storage'])
|
||||
->label('scope', 'files.write')
|
||||
->label('webhook', 'storage.files.create')
|
||||
->label('event', 'storage.files.create')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
->label('sdk.namespace', 'storage')
|
||||
->label('sdk.method', 'createFile')
|
||||
|
@ -474,7 +474,7 @@ App::put('/v1/storage/files/:fileId')
|
|||
->desc('Update File')
|
||||
->groups(['api', 'storage'])
|
||||
->label('scope', 'files.write')
|
||||
->label('webhook', 'storage.files.update')
|
||||
->label('event', 'storage.files.update')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
->label('sdk.namespace', 'storage')
|
||||
->label('sdk.method', 'updateFile')
|
||||
|
@ -522,7 +522,7 @@ App::delete('/v1/storage/files/:fileId')
|
|||
->desc('Delete File')
|
||||
->groups(['api', 'storage'])
|
||||
->label('scope', 'files.write')
|
||||
->label('webhook', 'storage.files.delete')
|
||||
->label('event', 'storage.files.delete')
|
||||
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
||||
->label('sdk.namespace', 'storage')
|
||||
->label('sdk.method', 'deleteFile')
|
||||
|
|
|
@ -4,6 +4,7 @@ ini_set('display_startup_errors', 1);
|
|||
error_reporting(E_ALL);
|
||||
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
|
||||
use Appwrite\Database\Adapter\Redis as RedisAdapter;
|
||||
use Appwrite\Database\Validator\Authorization;
|
||||
|
@ -106,29 +107,51 @@ class FunctionsV1
|
|||
|
||||
public function perform()
|
||||
{
|
||||
global $environments, $register;
|
||||
global $register;
|
||||
|
||||
$projectId = $this->args['projectId'];
|
||||
$functionId = $this->args['functionId'];
|
||||
$functionTag = $this->args['functionTag'];
|
||||
$executionId = $this->args['executionId'];
|
||||
$functionTrigger = $this->args['functionTrigger'];
|
||||
$trigger = $this->args['trigger'];
|
||||
|
||||
$projectDB = new Database();
|
||||
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
|
||||
$projectDB->setNamespace('app_'.$projectId);
|
||||
$projectDB->setMocks(Config::getParam('collections', []));
|
||||
$database = new Database();
|
||||
$database->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
|
||||
$database->setNamespace('app_'.$projectId);
|
||||
$database->setMocks(Config::getParam('collections', []));
|
||||
|
||||
Authorization::disable();
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
Authorization::reset();
|
||||
switch ($trigger) {
|
||||
case 'event':
|
||||
# code...
|
||||
break;
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
throw new Exception('Function not found', 404);
|
||||
case 'schedule':
|
||||
# code...
|
||||
break;
|
||||
|
||||
case 'http':
|
||||
Authorization::disable();
|
||||
$function = $database->getDocument($functionId);
|
||||
Authorization::reset();
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
throw new Exception('Function not found');
|
||||
}
|
||||
|
||||
$this->execute($trigger, $projectId, $executionId, $database, $function);
|
||||
break;
|
||||
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function)
|
||||
{
|
||||
$environments = Config::getParam('environments');
|
||||
|
||||
Authorization::disable();
|
||||
$tag = $projectDB->getDocument($functionTag);
|
||||
$tag = $database->getDocument($function->getAttribute('tag', ''));
|
||||
Authorization::reset();
|
||||
|
||||
if($tag->getAttribute('functionId') !== $function->getId()) {
|
||||
|
@ -137,10 +160,10 @@ class FunctionsV1
|
|||
|
||||
Authorization::disable();
|
||||
|
||||
$execution = $projectDB->getDocument($executionId);
|
||||
$execution = $database->getDocument($executionId);
|
||||
|
||||
if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_EXECUTIONS != $execution->getCollection()) {
|
||||
$execution = $projectDB->createDocument([
|
||||
$execution = $database->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
|
||||
'$permissions' => [
|
||||
'read' => [],
|
||||
|
@ -171,10 +194,10 @@ class FunctionsV1
|
|||
}
|
||||
|
||||
$vars = \array_merge($function->getAttribute('vars', []), [
|
||||
'APPWRITE_FUNCTION_ID' => $functionId,
|
||||
'APPWRITE_FUNCTION_ID' => $function->getId(),
|
||||
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),
|
||||
'APPWRITE_FUNCTION_TAG' => $functionTag,
|
||||
'APPWRITE_FUNCTION_TRIGGER' => $functionTrigger,
|
||||
'APPWRITE_FUNCTION_TAG' => $tag->getId(),
|
||||
'APPWRITE_FUNCTION_TRIGGER' => $trigger,
|
||||
'APPWRITE_FUNCTION_ENV_NAME' => $environment['name'],
|
||||
'APPWRITE_FUNCTION_ENV_VERSION' => $environment['version'],
|
||||
]);
|
||||
|
@ -312,7 +335,7 @@ class FunctionsV1
|
|||
|
||||
Authorization::disable();
|
||||
|
||||
$execution = $projectDB->updateDocument(array_merge($execution->getArrayCopy(), [
|
||||
$execution = $database->updateDocument(array_merge($execution->getArrayCopy(), [
|
||||
'tagId' => $tag->getId(),
|
||||
'status' => $functionStatus,
|
||||
'exitCode' => $exitCode,
|
||||
|
|
Loading…
Reference in a new issue