diff options
Diffstat (limited to 'apps/webhooks/lib/AppInfo/Application.php')
-rw-r--r-- | apps/webhooks/lib/AppInfo/Application.php | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/apps/webhooks/lib/AppInfo/Application.php b/apps/webhooks/lib/AppInfo/Application.php index a8836a25f54..cbbacd3ed48 100644 --- a/apps/webhooks/lib/AppInfo/Application.php +++ b/apps/webhooks/lib/AppInfo/Application.php @@ -16,14 +16,25 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\EventDispatcher\IEventDispatcher; +use OCP\ICache; +use OCP\ICacheFactory; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; class Application extends App implements IBootstrap { public const APP_ID = 'webhooks'; - public function __construct() { + private ?ICache $cache = null; + + private const CACHE_KEY = 'eventsUsedInWebhooks'; + + public function __construct( + ICacheFactory $cacheFactory, + ) { parent::__construct(self::APP_ID); + if ($cacheFactory->isAvailable()) { + $this->cache = $cacheFactory->createDistributed(); + } } public function register(IRegistrationContext $context): void { @@ -38,13 +49,10 @@ class Application extends App implements IBootstrap { ContainerInterface $container, LoggerInterface $logger, ): void { - /** @var WebhookListenerMapper */ - $mapper = $container->get(WebhookListenerMapper::class); - /* Listen to all events with at least one webhook configured */ - $configuredEvents = $mapper->getAllConfiguredEvents(); + $configuredEvents = $this->getAllConfiguredEvents($container); foreach ($configuredEvents as $eventName) { - // $logger->error($eventName.' '.\OCP\Files\Events\Node\NodeWrittenEvent::class, ['exception' => new \Exception('coucou')]); + $logger->debug("Listening to {$eventName}"); $dispatcher->addServiceListener( $eventName, WebhooksEventListener::class, @@ -52,4 +60,19 @@ class Application extends App implements IBootstrap { ); } } + + /** + * List all events with at least one webhook configured, with cache + */ + private function getAllConfiguredEvents(ContainerInterface $container) { + $events = $this->cache?->get(self::CACHE_KEY); + if ($events !== null) { + return json_decode($events); + } + /** @var WebhookListenerMapper */ + $mapper = $container->get(WebhookListenerMapper::class); + $events = $mapper->getAllConfiguredEvents(); + // cache for 5 minutes + $this->cache?->set(self::CACHE_KEY, json_encode($events), 300); + } } |