From 6c1f9675099ac039c4797991b3f5b9d6b93a1dad Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 28 Dec 2025 18:10:44 +0200 Subject: [PATCH] add functionsEvents and webhooksEvents --- app/config/collections/platform.php | 22 +++++++++++ app/init/database/filters.php | 38 +++++++++++++++++++ .../Collections/Documents/Action.php | 18 +++++++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index d44d9b725c..9f46d5e8c7 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -276,6 +276,28 @@ return [ 'array' => false, 'filters' => ['subQueryWebhooks'], ], + [ + '$id' => ID::custom('webhookEvents'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => [], + 'array' => true, + 'filters' => ['subQueryWebhookEvents'], + ], + [ + '$id' => ID::custom('functionEvents'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => [], + 'array' => true, + 'filters' => ['subQueryFunctionEvents'], + ], [ '$id' => ID::custom('keys'), 'type' => Database::VAR_STRING, diff --git a/app/init/database/filters.php b/app/init/database/filters.php index c4cfd1ac81..ef40e55379 100644 --- a/app/init/database/filters.php +++ b/app/init/database/filters.php @@ -170,6 +170,44 @@ Database::addFilter( } ); +Database::addFilter( + 'subQueryWebhookEvents', + function (mixed $value) { + return; + }, + function (mixed $value, Document $document, Database $database) { + $webhooks = $database + ->find('webhooks', [ + Query::equal('projectInternalId', [$document->getSequence()]), + Query::limit(APP_LIMIT_SUBQUERY), + ]); + + $events = []; + foreach ($webhooks as $webhook) { + $webhookEvents = $webhook->getAttribute('events', []); + if (!empty($webhookEvents)) { + $events = array_merge($events, $webhookEvents); + } + } + + return array_unique($events); + } +); + +Database::addFilter( + 'subQueryFunctionEvents', + function (mixed $value) { + return; + }, + function (mixed $value, Document $document, Database $database) { + // Functions are stored in the project database, not platform database + // This filter will return empty array when called from platform DB + // Function events will need to be computed separately when dbForProject is available + // For now, return empty to avoid errors + return []; + } +); + Database::addFilter( 'subQuerySessions', function (mixed $value) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index f16d00998d..a0cb5c20f5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -378,11 +378,21 @@ abstract class Action extends DatabasesAction ->from($queueForEvents) ->trigger(); - $queueForFunctions - ->from($queueForEvents) - ->trigger(); + $project = $queueForEvents->getProject(); + $generatedEvents = Event::generateEvents( + $queueForEvents->getEvent(), + $queueForEvents->getParams() + ); - if (!empty($queueForEvents->getProject()?->getAttribute('webhooks', []))) { + $functionEvents = $project?->getAttribute('functionEvents', []); + if (!empty($functionEvents) && !empty(array_intersect($functionEvents, $generatedEvents))) { + $queueForFunctions + ->from($queueForEvents) + ->trigger(); + } + + $webhookEvents = $project?->getAttribute('webhookEvents', []); + if (!empty($webhookEvents) && !empty(array_intersect($webhookEvents, $generatedEvents))) { $queueForWebhooks ->from($queueForEvents) ->trigger();