Skip to content

Commit e4378fd

Browse files
authored
Merge pull request #32349 from nextcloud/enh/projects-event
Add event to load additional scripts for projects
2 parents ac79137 + ae0e9ff commit e4378fd

File tree

7 files changed

+85
-17
lines changed

7 files changed

+85
-17
lines changed

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use OCP\AppFramework\Bootstrap\IBootContext;
5757
use OCP\AppFramework\Bootstrap\IBootstrap;
5858
use OCP\AppFramework\Bootstrap\IRegistrationContext;
59+
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
5960
use OCP\EventDispatcher\IEventDispatcher;
6061
use OCP\Federation\ICloudIdManager;
6162
use OCP\Files\Config\IMountProviderCollection;
@@ -131,11 +132,11 @@ public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatc
131132
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class);
132133
$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
133134
$dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
134-
$dispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () {
135-
\OCP\Util::addScript('files_sharing', 'collaboration');
136-
});
137135
$dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
138136
$dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
137+
$dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
138+
\OCP\Util::addScript('files_sharing', 'collaboration');
139+
});
139140

140141
// notifications api to accept incoming user shares
141142
$oldDispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) {

apps/files_sharing/list.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
use OCA\Files\Event\LoadAdditionalScriptsEvent;
2727
use OCA\Files\Event\LoadSidebar;
2828
use OCA\Viewer\Event\LoadViewer;
29-
use OCP\EventDispatcher\GenericEvent;
29+
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
30+
use OCP\EventDispatcher\IEventDispatcher;
31+
use OCP\IConfig;
32+
use OCP\IUserSession;
33+
use OCP\Server;
3034

31-
$config = \OC::$server->getConfig();
32-
$userSession = \OC::$server->getUserSession();
33-
$legacyEventDispatcher = \OC::$server->getEventDispatcher();
34-
/** @var \OCP\EventDispatcher\IEventDispatcher $eventDispatcher */
35-
$eventDispatcher = \OC::$server->get(OCP\EventDispatcher\IEventDispatcher::class);
35+
$config = Server::get(IConfig::class);
36+
$userSession = Server::get(IUserSession::class);
37+
$eventDispatcher = Server::get(IEventDispatcher::class);
3638

3739
$showgridview = $config->getUserValue($userSession->getUser()->getUID(), 'files', 'show_grid', false);
3840

@@ -42,7 +44,7 @@
4244
$tmpl->assign('showgridview', $showgridview);
4345

4446
// fire script events
45-
$legacyEventDispatcher->dispatch('\OCP\Collaboration\Resources::loadAdditionalScripts', new GenericEvent());
47+
$eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
4648
$eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
4749
$eventDispatcher->dispatchTyped(new LoadSidebar());
4850

lib/base.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@
6262
*
6363
*/
6464

65+
use OC\EventDispatcher\SymfonyAdapter;
6566
use OCP\EventDispatcher\IEventDispatcher;
6667
use OCP\Group\Events\UserRemovedEvent;
6768
use OCP\ILogger;
69+
use OCP\Server;
6870
use OCP\Share;
6971
use OC\Encryption\HookManager;
7072
use OC\Files\Filesystem;
@@ -888,7 +890,7 @@ private static function registerAppRestrictionsHooks() {
888890
}
889891

890892
private static function registerResourceCollectionHooks() {
891-
\OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher());
893+
\OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class));
892894
}
893895

894896
/**

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
'OCP\\Collaboration\\Resources\\IProvider' => $baseDir . '/lib/public/Collaboration/Resources/IProvider.php',
149149
'OCP\\Collaboration\\Resources\\IProviderManager' => $baseDir . '/lib/public/Collaboration/Resources/IProviderManager.php',
150150
'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php',
151+
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
151152
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
152153
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
153154
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
177177
'OCP\\Collaboration\\Resources\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProvider.php',
178178
'OCP\\Collaboration\\Resources\\IProviderManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProviderManager.php',
179179
'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php',
180+
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
180181
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
181182
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
182183
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',

lib/private/Collaboration/Resources/Listener.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
*/
2727
namespace OC\Collaboration\Resources;
2828

29+
use OC\EventDispatcher\SymfonyAdapter;
2930
use OCP\Collaboration\Resources\IManager;
31+
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
32+
use OCP\EventDispatcher\IEventDispatcher;
3033
use OCP\IGroup;
3134
use OCP\IUser;
32-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3335
use Symfony\Component\EventDispatcher\GenericEvent;
3436

3537
class Listener {
36-
public static function register(EventDispatcherInterface $dispatcher): void {
38+
public static function register(SymfonyAdapter $symfonyDispatcher, IEventDispatcher $eventDispatcher): void {
3739
$listener = function (GenericEvent $event) {
3840
/** @var IUser $user */
3941
$user = $event->getArgument('user');
@@ -42,10 +44,10 @@ public static function register(EventDispatcherInterface $dispatcher): void {
4244

4345
$resourceManager->invalidateAccessCacheForUser($user);
4446
};
45-
$dispatcher->addListener(IGroup::class . '::postAddUser', $listener);
46-
$dispatcher->addListener(IGroup::class . '::postRemoveUser', $listener);
47+
$symfonyDispatcher->addListener(IGroup::class . '::postAddUser', $listener);
48+
$symfonyDispatcher->addListener(IGroup::class . '::postRemoveUser', $listener);
4749

48-
$dispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) {
50+
$symfonyDispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) {
4951
/** @var IUser $user */
5052
$user = $event->getSubject();
5153
/** @var IManager $resourceManager */
@@ -54,7 +56,7 @@ public static function register(EventDispatcherInterface $dispatcher): void {
5456
$resourceManager->invalidateAccessCacheForUser($user);
5557
});
5658

57-
$dispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) {
59+
$symfonyDispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) {
5860
/** @var IGroup $group */
5961
$group = $event->getSubject();
6062
/** @var IManager $resourceManager */
@@ -64,5 +66,24 @@ public static function register(EventDispatcherInterface $dispatcher): void {
6466
$resourceManager->invalidateAccessCacheForUser($user);
6567
}
6668
});
69+
70+
// Stay backward compatible with the legacy event for now
71+
$fallbackEventRunning = false;
72+
$symfonyDispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () use ($eventDispatcher, &$fallbackEventRunning) {
73+
if ($fallbackEventRunning) {
74+
return;
75+
}
76+
$fallbackEventRunning = true;
77+
$eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
78+
$fallbackEventRunning = false;
79+
});
80+
$eventDispatcher->addListener(LoadAdditionalScriptsEvent::class, static function () use ($symfonyDispatcher, &$fallbackEventRunning) {
81+
if ($fallbackEventRunning) {
82+
return;
83+
}
84+
$fallbackEventRunning = true;
85+
$symfonyDispatcher->dispatch('\OCP\Collaboration\Resources::loadAdditionalScripts');
86+
$fallbackEventRunning = false;
87+
});
6788
}
6889
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
7+
*
8+
* @author Julius Härtl <jus@bitgrid.net>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\Collaboration\Resources;
28+
29+
use OCP\EventDispatcher\Event;
30+
31+
/**
32+
* This event is used by apps to register their own frontend scripts for integrating
33+
* projects in their app. Apps also need to dispatch the event in order to load
34+
* scripts during page load
35+
*
36+
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
37+
* @since 25.0.0
38+
*/
39+
class LoadAdditionalScriptsEvent extends Event {
40+
}

0 commit comments

Comments
 (0)