aboutsummaryrefslogtreecommitdiffstats
path: root/apps/webhook_listeners/lib/AppInfo/Application.php
blob: 30f2e0969f6ed057387d834366ce0b8b533d6652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\WebhookListeners\AppInfo;

use OCA\WebhookListeners\Db\WebhookListenerMapper;
use OCA\WebhookListeners\Listener\WebhooksEventListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUserSession;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class Application extends App implements IBootstrap {
	public const APP_ID = 'webhook_listeners';

	public function __construct() {
		parent::__construct(self::APP_ID);
	}

	public function register(IRegistrationContext $context): void {
	}

	public function boot(IBootContext $context): void {
		$context->injectFn($this->registerRuleListeners(...));
	}

	private function registerRuleListeners(
		IEventDispatcher $dispatcher,
		ContainerInterface $container,
		LoggerInterface $logger,
	): void {
		/** @var WebhookListenerMapper */
		$mapper = $container->get(WebhookListenerMapper::class);
		$userSession = $container->get(IUserSession::class);

		/* Listen to all events with at least one webhook configured */
		$configuredEvents = $mapper->getAllConfiguredEvents($userSession->getUser()?->getUID());
		foreach ($configuredEvents as $eventName) {
			$logger->debug("Listening to {$eventName}");
			$dispatcher->addServiceListener(
				$eventName,
				WebhooksEventListener::class,
				-1,
			);
		}
	}
}