From 79bf99372e11b87360a41503865004b1b8db6925 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Mon, 17 May 2021 12:49:17 +0300 Subject: [PATCH 1/4] Removed DB connection --- app/controllers/shared/api.php | 1 + app/workers/webhooks.php | 27 ++------------------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 8b3971079d..0fca084db9 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -188,6 +188,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits $webhooks ->setQueue('v1-webhooks') ->setClass('WebhooksV1') + ->setParam('webhooks', $project->getAttribute('webhooks', [])) ->trigger(); $functions diff --git a/app/workers/webhooks.php b/app/workers/webhooks.php index 75cc54bf57..6acc38f646 100644 --- a/app/workers/webhooks.php +++ b/app/workers/webhooks.php @@ -2,11 +2,6 @@ use Utopia\App; use Utopia\CLI\Console; -use Utopia\Config\Config; -use Appwrite\Database\Database; -use Appwrite\Database\Adapter\MySQL as MySQLAdapter; -use Appwrite\Database\Adapter\Redis as RedisAdapter; -use Appwrite\Database\Validator\Authorization; require_once __DIR__.'/../init.php'; @@ -24,34 +19,16 @@ class WebhooksV1 public function perform() { - global $register; - - $consoleDB = new Database(); - $consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); - $consoleDB->setNamespace('app_console'); // Main DB - $consoleDB->setMocks(Config::getParam('collections', [])); - $errors = []; // Event $projectId = $this->args['projectId'] ?? ''; + $webhooks = $this->args['webhooks'] ?? []; $userId = $this->args['userId'] ?? ''; $event = $this->args['event'] ?? ''; $eventData = \json_encode($this->args['eventData']); - // Webhook - - Authorization::disable(); - - $project = $consoleDB->getDocument($projectId); - - Authorization::reset(); - - if (\is_null($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS !== $project->getCollection()) { - throw new Exception('Project Not Found'); - } - - foreach ($project->getAttribute('webhooks', []) as $webhook) { + foreach ($webhooks as $webhook) { if (!(isset($webhook['events']) && \is_array($webhook['events']) && \in_array($event, $webhook['events']))) { continue; } From af470a3f1a527093665ec511b2d6e7bc14d55fa8 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Mon, 17 May 2021 12:54:04 +0300 Subject: [PATCH 2/4] Cleanups --- app/tasks/sdks.php | 14 -------------- app/workers/mails.php | 1 - 2 files changed, 15 deletions(-) diff --git a/app/tasks/sdks.php b/app/tasks/sdks.php index 1d7a87307b..501de50211 100644 --- a/app/tasks/sdks.php +++ b/app/tasks/sdks.php @@ -21,20 +21,6 @@ use Appwrite\SDK\Language\Swift; $cli ->task('sdks') ->action(function () { - function getSSLPage($url) - { - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_HEADER, false); - \curl_setopt($ch, CURLOPT_URL, $url); - \curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - \curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $result = \curl_exec($ch); - \curl_close($ch); - - return $result; - } - $platforms = Config::getParam('platforms'); $selected = \strtolower(Console::confirm('Choose SDK ("*" for all):')); $version = Console::confirm('Choose an Appwrite version'); diff --git a/app/workers/mails.php b/app/workers/mails.php index 6f4422b57f..11f41e4446 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -29,7 +29,6 @@ class MailsV1 return; } - $event = $this->args['event']; $from = $this->args['from']; $recipient = $this->args['recipient']; $name = $this->args['name']; From a8cc398c8528cb20eecef333c10e334429a87f20 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Mon, 17 May 2021 14:32:37 +0300 Subject: [PATCH 3/4] Updated functions trigger --- app/controllers/api/functions.php | 2 ++ app/controllers/shared/api.php | 2 +- app/workers/functions.php | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index ff8b7d5bc9..e74fc0ee1a 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -321,6 +321,7 @@ App::put('/v1/functions/:functionId') if ($next && $schedule !== $original) { ResqueScheduler::enqueueAt($next, 'v1-functions', 'FunctionsV1', [ 'projectId' => $project->getId(), + 'webhooks' => $project->getAttribute('webhooks', []), 'functionId' => $function->getId(), 'executionId' => null, 'trigger' => 'schedule', @@ -375,6 +376,7 @@ App::patch('/v1/functions/:functionId/tag') if ($next) { // Init first schedule ResqueScheduler::enqueueAt($next, 'v1-functions', 'FunctionsV1', [ 'projectId' => $project->getId(), + 'webhooks' => $project->getAttribute('webhooks', []), 'functionId' => $function->getId(), 'executionId' => null, 'trigger' => 'schedule', diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0fca084db9..a6c2b4173d 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -76,6 +76,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $register, $e */ $events ->setParam('projectId', $project->getId()) + ->setParam('webhooks', $project->getAttribute('webhooks', [])) ->setParam('userId', $user->getId()) ->setParam('event', $route->getLabel('event', '')) ->setParam('eventData', []) @@ -188,7 +189,6 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits $webhooks ->setQueue('v1-webhooks') ->setClass('WebhooksV1') - ->setParam('webhooks', $project->getAttribute('webhooks', [])) ->trigger(); $functions diff --git a/app/workers/functions.php b/app/workers/functions.php index 7ea356464e..33b300d54b 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -141,6 +141,7 @@ class FunctionsV1 $projectId = $this->args['projectId'] ?? ''; $functionId = $this->args['functionId'] ?? ''; + $webhooks = $this->args['webhooks'] ?? []; $executionId = $this->args['executionId'] ?? ''; $trigger = $this->args['trigger'] ?? ''; $event = $this->args['event'] ?? ''; @@ -196,7 +197,7 @@ class FunctionsV1 Console::success('Triggered function: '.$event); - $this->execute('event', $projectId, '', $database, $function, $event, $eventData, $data, $userId, $jwt); + $this->execute('event', $projectId, '', $database, $function, $event, $eventData, $data, $webhooks, $userId, $jwt); } } break; @@ -252,7 +253,7 @@ class FunctionsV1 'scheduleOriginal' => $function->getAttribute('schedule', ''), ]); // Async task rescheduale - $this->execute($trigger, $projectId, $executionId, $database, $function, /*$event*/'', /*$eventData*/'', $data, $userId, $jwt); + $this->execute($trigger, $projectId, $executionId, $database, $function, /*$event*/'', /*$eventData*/'', $data, $webhooks, $userId, $jwt); break; case 'http': @@ -264,7 +265,7 @@ class FunctionsV1 throw new Exception('Function not found ('.$functionId.')'); } - $this->execute($trigger, $projectId, $executionId, $database, $function, /*$event*/'', /*$eventData*/'', $data, $userId, $jwt); + $this->execute($trigger, $projectId, $executionId, $database, $function, /*$event*/'', /*$eventData*/'', $data, $webhooks, $userId, $jwt); break; default: @@ -287,7 +288,7 @@ class FunctionsV1 * * @return void */ - public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $eventData = '', string $data = '', string $userId = '', string $jwt = ''): void + public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $eventData = '', string $data = '', array $webhooks = [], string $userId = '', string $jwt = ''): void { global $list; @@ -479,6 +480,7 @@ class FunctionsV1 $executionUpdate ->setParam('projectId', $projectId) ->setParam('userId', $userId) + ->setParam('webhooks', $webhooks) ->setParam('event', 'functions.executions.update') ->setParam('eventData', [ '$id' => $execution['$id'], From 63a92a768adf3ccb5cbd9e2cbdd9f8c09dacd3a4 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Mon, 17 May 2021 18:52:22 +0300 Subject: [PATCH 4/4] Fixed functions webhooks trigger --- app/controllers/api/functions.php | 1 + app/workers/functions.php | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index e74fc0ee1a..0f009aa404 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -768,6 +768,7 @@ App::post('/v1/functions/:functionId/executions') Resque::enqueue('v1-functions', 'FunctionsV1', [ 'projectId' => $project->getId(), + 'webhooks' => $project->getAttribute('webhooks', []), 'functionId' => $function->getId(), 'executionId' => $execution->getId(), 'trigger' => 'http', diff --git a/app/workers/functions.php b/app/workers/functions.php index 33b300d54b..8bf719e607 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -247,6 +247,7 @@ class FunctionsV1 ResqueScheduler::enqueueAt($next, 'v1-functions', 'FunctionsV1', [ 'projectId' => $projectId, + 'webhooks' => $webhooks, 'functionId' => $function->getId(), 'executionId' => null, 'trigger' => 'schedule', @@ -285,6 +286,9 @@ class FunctionsV1 * @param string $event * @param string $eventData * @param string $data + * @param array $webhooks + * @param string $userId + * @param string $jwt * * @return void */