Skip to content

Commit

Permalink
Merge pull request #32349 from nextcloud/enh/projects-event
Browse files Browse the repository at this point in the history
Add event to load additional scripts for projects
  • Loading branch information
CarlSchwan authored May 27, 2022
2 parents ac79137 + ae0e9ff commit e4378fd
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 17 deletions.
7 changes: 4 additions & 3 deletions apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudIdManager;
use OCP\Files\Config\IMountProviderCollection;
Expand Down Expand Up @@ -131,11 +132,11 @@ public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatc
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class);
$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
$dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
$dispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () {
\OCP\Util::addScript('files_sharing', 'collaboration');
});
$dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
$dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
$dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
\OCP\Util::addScript('files_sharing', 'collaboration');
});

// notifications api to accept incoming user shares
$oldDispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) {
Expand Down
16 changes: 9 additions & 7 deletions apps/files_sharing/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files\Event\LoadSidebar;
use OCA\Viewer\Event\LoadViewer;
use OCP\EventDispatcher\GenericEvent;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Server;

$config = \OC::$server->getConfig();
$userSession = \OC::$server->getUserSession();
$legacyEventDispatcher = \OC::$server->getEventDispatcher();
/** @var \OCP\EventDispatcher\IEventDispatcher $eventDispatcher */
$eventDispatcher = \OC::$server->get(OCP\EventDispatcher\IEventDispatcher::class);
$config = Server::get(IConfig::class);
$userSession = Server::get(IUserSession::class);
$eventDispatcher = Server::get(IEventDispatcher::class);

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

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

// fire script events
$legacyEventDispatcher->dispatch('\OCP\Collaboration\Resources::loadAdditionalScripts', new GenericEvent());
$eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
$eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
$eventDispatcher->dispatchTyped(new LoadSidebar());

Expand Down
4 changes: 3 additions & 1 deletion lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
*
*/

use OC\EventDispatcher\SymfonyAdapter;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
use OCP\Server;
use OCP\Share;
use OC\Encryption\HookManager;
use OC\Files\Filesystem;
Expand Down Expand Up @@ -888,7 +890,7 @@ private static function registerAppRestrictionsHooks() {
}

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

/**
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
'OCP\\Collaboration\\Resources\\IProvider' => $baseDir . '/lib/public/Collaboration/Resources/IProvider.php',
'OCP\\Collaboration\\Resources\\IProviderManager' => $baseDir . '/lib/public/Collaboration/Resources/IProviderManager.php',
'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Collaboration\\Resources\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProvider.php',
'OCP\\Collaboration\\Resources\\IProviderManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProviderManager.php',
'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
Expand Down
33 changes: 27 additions & 6 deletions lib/private/Collaboration/Resources/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
*/
namespace OC\Collaboration\Resources;

use OC\EventDispatcher\SymfonyAdapter;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroup;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

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

$resourceManager->invalidateAccessCacheForUser($user);
};
$dispatcher->addListener(IGroup::class . '::postAddUser', $listener);
$dispatcher->addListener(IGroup::class . '::postRemoveUser', $listener);
$symfonyDispatcher->addListener(IGroup::class . '::postAddUser', $listener);
$symfonyDispatcher->addListener(IGroup::class . '::postRemoveUser', $listener);

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

$dispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) {
$symfonyDispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) {
/** @var IGroup $group */
$group = $event->getSubject();
/** @var IManager $resourceManager */
Expand All @@ -64,5 +66,24 @@ public static function register(EventDispatcherInterface $dispatcher): void {
$resourceManager->invalidateAccessCacheForUser($user);
}
});

// Stay backward compatible with the legacy event for now
$fallbackEventRunning = false;
$symfonyDispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () use ($eventDispatcher, &$fallbackEventRunning) {
if ($fallbackEventRunning) {
return;
}
$fallbackEventRunning = true;
$eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
$fallbackEventRunning = false;
});
$eventDispatcher->addListener(LoadAdditionalScriptsEvent::class, static function () use ($symfonyDispatcher, &$fallbackEventRunning) {
if ($fallbackEventRunning) {
return;
}
$fallbackEventRunning = true;
$symfonyDispatcher->dispatch('\OCP\Collaboration\Resources::loadAdditionalScripts');
$fallbackEventRunning = false;
});
}
}
40 changes: 40 additions & 0 deletions lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\Collaboration\Resources;

use OCP\EventDispatcher\Event;

/**
* This event is used by apps to register their own frontend scripts for integrating
* projects in their app. Apps also need to dispatch the event in order to load
* scripts during page load
*
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
* @since 25.0.0
*/
class LoadAdditionalScriptsEvent extends Event {
}

0 comments on commit e4378fd

Please sign in to comment.