mirror of
https://github.com/appwrite/appwrite
synced 2026-05-24 09:28:40 +00:00
116 lines
3.3 KiB
PHP
116 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Appwrite\Functions;
|
|
|
|
use Utopia\Database\Database;
|
|
use Utopia\Database\Document;
|
|
use Utopia\Database\Query;
|
|
|
|
class EventProcessor
|
|
{
|
|
/**
|
|
* @param array<mixed> $events
|
|
* @return array<string, bool>
|
|
*/
|
|
private function getEventMap(array $events): array
|
|
{
|
|
return \array_fill_keys(
|
|
\array_map('strval', \array_unique($events)),
|
|
true
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get function events for a project, using Redis cache
|
|
* @param Document|null $project
|
|
* @param Database $dbForProject
|
|
* @return array<string, bool>
|
|
*/
|
|
public function getFunctionsEvents(?Document $project, Database $dbForProject): array
|
|
{
|
|
if ($project === null ||
|
|
$project->isEmpty() ||
|
|
$project->getId() === 'console') {
|
|
return [];
|
|
}
|
|
|
|
$hostname = $dbForProject->getAdapter()->getHostname();
|
|
$cacheKey = \sprintf(
|
|
'%s-cache-%s:%s:%s:project:%s:functions:events',
|
|
$dbForProject->getCacheName(),
|
|
$hostname,
|
|
$dbForProject->getNamespace(),
|
|
$dbForProject->getTenant(),
|
|
$project->getId()
|
|
);
|
|
|
|
$ttl = 3600; // 1 hour cache TTL
|
|
$cachedFunctionEvents = $dbForProject->getCache()->load($cacheKey, $ttl);
|
|
|
|
if ($cachedFunctionEvents !== false) {
|
|
$decoded = \json_decode($cachedFunctionEvents, true);
|
|
|
|
return \is_array($decoded) ? $this->getEventMap(\array_keys($decoded)) : [];
|
|
}
|
|
|
|
$events = [];
|
|
$limit = 100;
|
|
$sum = 100;
|
|
$offset = 0;
|
|
|
|
while ($sum >= $limit) {
|
|
$functions = $dbForProject->getAuthorization()->skip(fn () => $dbForProject->find('functions', [
|
|
Query::select(['$id', 'events']),
|
|
Query::limit($limit),
|
|
Query::offset($offset),
|
|
Query::orderAsc('$sequence'),
|
|
]));
|
|
|
|
$sum = \count($functions);
|
|
$offset = $offset + $limit;
|
|
|
|
foreach ($functions as $function) {
|
|
$functionEvents = $function->getAttribute('events', []);
|
|
if (!empty($functionEvents)) {
|
|
\array_push($events, ...$functionEvents);
|
|
}
|
|
}
|
|
}
|
|
|
|
$uniqueEvents = $this->getEventMap($events);
|
|
$dbForProject->getCache()->save($cacheKey, \json_encode($uniqueEvents));
|
|
|
|
return $uniqueEvents;
|
|
}
|
|
|
|
/**
|
|
* Get webhook events for a project from the project's webhooks attribute
|
|
* @param Document|null $project
|
|
* @return array<string, bool>
|
|
*/
|
|
public function getWebhooksEvents(?Document $project): array
|
|
{
|
|
if ($project === null || $project->isEmpty() || $project->getId() === 'console') {
|
|
return [];
|
|
}
|
|
|
|
$webhooks = $project->getAttribute('webhooks', []);
|
|
if (empty($webhooks)) {
|
|
return [];
|
|
}
|
|
|
|
$events = [];
|
|
foreach ($webhooks as $webhook) {
|
|
if ($webhook->getAttribute('enabled', false) !== true) {
|
|
continue;
|
|
}
|
|
|
|
$webhookEvents = $webhook->getAttribute('events', []);
|
|
if (!empty($webhookEvents)) {
|
|
\array_push($events, ...$webhookEvents);
|
|
}
|
|
}
|
|
|
|
return $this->getEventMap($events);
|
|
}
|
|
}
|