Hello!
I am trying to write an app that listens to Events and sends a notification to a backend.
I am using the official Docker image and i copy my code into the custom_apps directory via docker cp.
Here is what i think are the important parts of my code
<?php
namespace OCA\IntrafindConnector\AppInfo;
use OCA\IntrafindConnector\Event\NodeCreatedListener;
use OCA\IntrafindConnector\Event\UserCreatedListener;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\User\Events\UserCreatedEvent;
class Application extends App {
public const APP_ID = 'intrafindconnector';
public function __construct() {
parent::__construct(self::APP_ID);
/* @var IEventDispatcher $eventDispatcher */
$dispatcher = $this->getContainer()->get(IEventDispatcher::class);
$dispatcher->addServiceListener(NodeCreatedEvent::class, NodeCreatedListener::class);
$dispatcher->addServiceListener(UserCreatedEvent::class, UserCreatedListener::class);
}
}
<?php
namespace OCA\IntrafindConnector\Event;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
class UserCreatedListener implements IEventListener {
private $logger;
private $appName;
public function __construct(LoggerInterface $logger, string $appName) {
$this->logger = $logger;
$this->appName = $appName;
}
/**
* @inheritDoc
*/
public function handle(Event $event): void {
$user = $event->getUser();
$this->logger->warning($user->getDisplayName());
}
}
The idea is that i create a new user and the username gets written into the log file (of course only for testing purposes). Unfortunately i get the following exception
[no app in context] Error: OCP\AppFramework\QueryException: Could not resolve OCA\IntrafindConnector\Event\UserCreatedListener! Class OCA\IntrafindConnector\Event\UserCreatedListener does not exist at <<closure>>
0. /var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php line 126
OC\AppFramework\Utility\SimpleContainer->resolve("OCA\\IntrafindC ... r")
1. /var/www/html/lib/private/ServerContainer.php line 162
OC\AppFramework\Utility\SimpleContainer->query("OCA\\IntrafindC ... r", true)
2. /var/www/html/lib/private/EventDispatcher/ServiceEventListener.php line 67
OC\ServerContainer->query("OCA\\IntrafindC ... r")
3. /var/www/html/3rdparty/symfony/event-dispatcher/EventDispatcher.php line 251
OC\EventDispatcher\ServiceEventListener->__invoke(OCP\User\Events\UserCreatedEvent {}, "OCP\\User\\Events\\UserCreatedEvent", Symfony\Componen ... {})
4. /var/www/html/3rdparty/symfony/event-dispatcher/EventDispatcher.php line 73
Symfony\Component\EventDispatcher\EventDispatcher->callListeners([Closure {}], "OCP\\User\\Events\\UserCreatedEvent", OCP\User\Events\UserCreatedEvent {})
5. /var/www/html/lib/private/EventDispatcher/EventDispatcher.php line 86
Symfony\Component\EventDispatcher\EventDispatcher->dispatch(OCP\User\Events\UserCreatedEvent {}, "OCP\\User\\Events\\UserCreatedEvent")
6. /var/www/html/lib/private/EventDispatcher/EventDispatcher.php line 98
OC\EventDispatcher\EventDispatcher->dispatch("OCP\\User\\Events\\UserCreatedEvent", OCP\User\Events\UserCreatedEvent {})
7. /var/www/html/lib/private/User/Manager.php line 460
OC\EventDispatcher\EventDispatcher->dispatchTyped(OCP\User\Events\UserCreatedEvent {})
8. /var/www/html/lib/private/User/Manager.php line 396
OC\User\Manager->createUserFromBackend("*** sensitive parameter replaced ***", "*** sensitive parameter replaced ***", OC\User\Database {})
9. /var/www/html/apps/provisioning_api/lib/Controller/UsersController.php line 412
OC\User\Manager->createUser("*** sensitive parameter replaced ***", "*** sensitive parameter replaced ***")
10. /var/www/html/lib/private/AppFramework/Http/Dispatcher.php line 218
OCA\Provisioning_API\Controller\UsersController->addUser("*** sensitive parameters replaced ***")
11. /var/www/html/lib/private/AppFramework/Http/Dispatcher.php line 127
OC\AppFramework\Http\Dispatcher->executeController(OCA\Provisioning ... {}, "addUser")
12. /var/www/html/lib/private/AppFramework/App.php line 157
OC\AppFramework\Http\Dispatcher->dispatch(OCA\Provisioning ... {}, "addUser")
13. /var/www/html/lib/private/Route/Router.php line 302
OC\AppFramework\App::main("OCA\\Provisioni ... r", "addUser", OC\AppFramework\ ... {}, {_route: "ocs.pr ... "})
14. /var/www/html/ocs/v1.php line 63
OC\Route\Router->match("/ocsapp/cloud/users")
15. /var/www/html/ocs/v2.php line 24
require_once("/var/www/html/ocs/v1.php")
So from what i can tell the event gets caught but then my Listener class doesn’t get resolved.
What do i need to do that my Listener is resolved? Did i miss something?
I want to do basically the same with files. When a new file is uploaded, i want to write the filename to the logfile. But in this case, the event isn’t even caught.
In Owncloud, if you want to use filesystem hooks, you have to add
<types>
<filesystem/>
</types>
to the info.xml.
Do i have to do the same or a similiar thing here as well to use the filesystem events?
Thank you and regards
Fabian