From f80bb987eb7f347f076b1a15cb1e523a40912578 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Tue, 6 Dec 2022 19:20:02 +0100 Subject: [PATCH] listen to RenderReferenceEvent to inject provider list with initial state Signed-off-by: Julien Veyssier --- lib/base.php | 5 ++ .../RenderReferenceEventListener.php | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/private/Collaboration/Reference/RenderReferenceEventListener.php diff --git a/lib/base.php b/lib/base.php index a847373ea2bb2..09eccb1c647ae 100644 --- a/lib/base.php +++ b/lib/base.php @@ -752,6 +752,7 @@ public static function init(): void { self::registerAccountHooks(); self::registerResourceCollectionHooks(); self::registerFileReferenceEventListener(); + self::registerRenderReferenceEventListener(); self::registerAppRestrictionsHooks(); // Make sure that the application class is not loaded before the database is setup @@ -915,6 +916,10 @@ private static function registerFileReferenceEventListener(): void { \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class)); } + private static function registerRenderReferenceEventListener() { + \OC\Collaboration\Reference\RenderReferenceEventListener::register(Server::get(IEventDispatcher::class)); + } + /** * register hooks for sharing */ diff --git a/lib/private/Collaboration/Reference/RenderReferenceEventListener.php b/lib/private/Collaboration/Reference/RenderReferenceEventListener.php new file mode 100644 index 0000000000000..b91b7ca917882 --- /dev/null +++ b/lib/private/Collaboration/Reference/RenderReferenceEventListener.php @@ -0,0 +1,69 @@ + + * + * @author Julien Veyssier + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OC\Collaboration\Reference; + +use OCP\Collaboration\Reference\IDiscoverableReferenceProvider; +use OCP\Collaboration\Reference\IReferenceManager; +use OCP\Collaboration\Reference\RenderReferenceEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\EventDispatcher\IEventListener; +use OCP\IInitialStateService; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; + +class RenderReferenceEventListener implements IEventListener { + private IReferenceManager $manager; + private IInitialStateService $initialStateService; + + public function __construct(IReferenceManager $manager, IInitialStateService $initialStateService) { + $this->manager = $manager; + $this->initialStateService = $initialStateService; + } + + public static function register(IEventDispatcher $eventDispatcher): void { + $eventDispatcher->addServiceListener(RenderReferenceEvent::class, RenderReferenceEventListener::class); + } + + /** + * @inheritDoc + */ + public function handle(Event $event): void { + if (!($event instanceof RenderReferenceEvent)) { + return; + } + + try { + $providers = $this->manager->getDiscoverableProviders(); + } catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) { + return; + } + + $jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) { + return $provider->jsonSerialize(); + }, $providers); + $this->initialStateService->provideInitialState('core', 'reference-provider-list', $jsonProviders); + } +}