Skip to content

Commit

Permalink
chore: Add event to register mount providers more lazy
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jun 18, 2024
1 parent 250bb12 commit f2d0c53
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 49 deletions.
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'OCA\\Files_Sharing\\Listener\\BeforeZipCreatedListener' => $baseDir . '/../lib/Listener/BeforeZipCreatedListener.php',
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Sharing\\Listener\\RegisterMountProviderListener' => $baseDir . '/../lib/Listener/RegisterMountProviderListener.php',
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => $baseDir . '/../lib/Listener/ShareInteractionListener.php',
'OCA\\Files_Sharing\\Listener\\UserAddedToGroupListener' => $baseDir . '/../lib/Listener/UserAddedToGroupListener.php',
'OCA\\Files_Sharing\\Listener\\UserShareAcceptanceListener' => $baseDir . '/../lib/Listener/UserShareAcceptanceListener.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ComposerStaticInitFiles_Sharing
'OCA\\Files_Sharing\\Listener\\BeforeZipCreatedListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeZipCreatedListener.php',
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Sharing\\Listener\\RegisterMountProviderListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterMountProviderListener.php',
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/ShareInteractionListener.php',
'OCA\\Files_Sharing\\Listener\\UserAddedToGroupListener' => __DIR__ . '/..' . '/../lib/Listener/UserAddedToGroupListener.php',
'OCA\\Files_Sharing\\Listener\\UserShareAcceptanceListener' => __DIR__ . '/..' . '/../lib/Listener/UserShareAcceptanceListener.php',
Expand Down
13 changes: 4 additions & 9 deletions apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use OCA\Files_Sharing\Listener\BeforeZipCreatedListener;
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
use OCA\Files_Sharing\Listener\LoadSidebarListener;
use OCA\Files_Sharing\Listener\RegisterMountProviderListener;
use OCA\Files_Sharing\Listener\ShareInteractionListener;
use OCA\Files_Sharing\Listener\UserAddedToGroupListener;
use OCA\Files_Sharing\Listener\UserShareAcceptanceListener;
use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
use OCA\Files_Sharing\MountProvider;
use OCA\Files_Sharing\Notification\Listener;
use OCA\Files_Sharing\Notification\Notifier;
use OCA\Files_Sharing\ShareBackend\File;
Expand All @@ -37,9 +37,9 @@
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudIdManager;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
use OCP\Files\Events\BeforeZipCreatedEvent;
use OCP\Files\Events\RegisterMountProviderEvent;
use OCP\Group\Events\GroupChangedEvent;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
Expand Down Expand Up @@ -95,10 +95,11 @@ function () use ($c) {
// Handle download events for view only checks
$context->registerEventListener(BeforeZipCreatedEvent::class, BeforeZipCreatedListener::class);
$context->registerEventListener(BeforeDirectFileDownloadEvent::class, BeforeDirectFileDownloadListener::class);

$context->registerEventListener(RegisterMountProviderEvent::class, RegisterMountProviderListener::class);
}

public function boot(IBootContext $context): void {
$context->injectFn([$this, 'registerMountProviders']);
$context->injectFn([$this, 'registerEventsScripts']);

Helper::registerHooks();
Expand All @@ -107,12 +108,6 @@ public function boot(IBootContext $context): void {
Share::registerBackend('folder', Folder::class, 'file');
}


public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
$mountProviderCollection->registerProvider($mountProvider);
$mountProviderCollection->registerProvider($externalMountProvider);
}

public function registerEventsScripts(IEventDispatcher $dispatcher): void {
$dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
\OCP\Util::addScript('files_sharing', 'collaboration');
Expand Down
36 changes: 36 additions & 0 deletions apps/files_sharing/lib/Listener/RegisterMountProviderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

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

namespace OCA\Files_Sharing\Listener;

use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
use OCA\Files_Sharing\MountProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\RegisterMountProviderEvent;

/** @template-implements IEventListener<RegisterMountProviderEvent> */
class RegisterMountProviderListener implements IEventListener {

public function __construct(
private MountProvider $mountProvider,
private ExternalMountProvider $externalMountProvider,
) {
}

public function handle(Event $event): void {
if (!($event instanceof RegisterMountProviderEvent)) {
return;
}

$mountProviderCollection = $event->getProviderCollection();
$mountProviderCollection->registerProvider($this->mountProvider);
$mountProviderCollection->registerProvider($this->externalMountProvider);
}
}
8 changes: 0 additions & 8 deletions apps/files_sharing/tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use OC\Files\Filesystem;
use OC\User\DisplayNameCache;
use OCA\Files_Sharing\AppInfo\Application;
use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
use OCA\Files_Sharing\MountProvider;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Share\IShare;
use Test\Traits\MountProviderTrait;

Expand Down Expand Up @@ -54,11 +51,6 @@ public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();

$app = new Application();
$app->registerMountProviders(
\OC::$server->get(IMountProviderCollection::class),
\OC::$server->get(MountProvider::class),
\OC::$server->get(ExternalMountProvider::class),
);

// reset backend
\OC_User::clearBackends();
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 @@ -370,6 +370,7 @@
'OCP\\Files\\Events\\Node\\NodeRenamedEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeRenamedEvent.php',
'OCP\\Files\\Events\\Node\\NodeTouchedEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeTouchedEvent.php',
'OCP\\Files\\Events\\Node\\NodeWrittenEvent' => $baseDir . '/lib/public/Files/Events/Node/NodeWrittenEvent.php',
'OCP\\Files\\Events\\RegisterMountProviderEvent' => $baseDir . '/lib/public/Files/Events/RegisterMountProviderEvent.php',
'OCP\\Files\\File' => $baseDir . '/lib/public/Files/File.php',
'OCP\\Files\\FileInfo' => $baseDir . '/lib/public/Files/FileInfo.php',
'OCP\\Files\\FileNameTooLongException' => $baseDir . '/lib/public/Files/FileNameTooLongException.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 @@ -403,6 +403,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Files\\Events\\Node\\NodeRenamedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeRenamedEvent.php',
'OCP\\Files\\Events\\Node\\NodeTouchedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeTouchedEvent.php',
'OCP\\Files\\Events\\Node\\NodeWrittenEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/NodeWrittenEvent.php',
'OCP\\Files\\Events\\RegisterMountProviderEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/RegisterMountProviderEvent.php',
'OCP\\Files\\File' => __DIR__ . '/../../..' . '/lib/public/Files/File.php',
'OCP\\Files\\FileInfo' => __DIR__ . '/../../..' . '/lib/public/Files/FileInfo.php',
'OCP\\Files\\FileNameTooLongException' => __DIR__ . '/../../..' . '/lib/public/Files/FileNameTooLongException.php',
Expand Down
52 changes: 21 additions & 31 deletions lib/private/Files/Config/MountProviderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use OC\Hooks\Emitter;
use OC\Hooks\EmitterTrait;
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IHomeMountProvider;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Config\IRootMountProvider;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Events\RegisterMountProviderEvent;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorageFactory;
Expand All @@ -23,46 +25,30 @@
class MountProviderCollection implements IMountProviderCollection, Emitter {
use EmitterTrait;

/**
* @var \OCP\Files\Config\IHomeMountProvider[]
*/
private $homeProviders = [];
/** @var \OCP\Files\Config\IHomeMountProvider[] */
private array $homeProviders = [];

/**
* @var \OCP\Files\Config\IMountProvider[]
*/
private $providers = [];
/** @var \OCP\Files\Config\IMountProvider[] */
private array $providers = [];

/** @var \OCP\Files\Config\IRootMountProvider[] */
private $rootProviders = [];

/**
* @var \OCP\Files\Storage\IStorageFactory
*/
private $loader;

/**
* @var \OCP\Files\Config\IUserMountCache
*/
private $mountCache;
private array $rootProviders = [];

/** @var callable[] */
private $mountFilters = [];
private array $mountFilters = [];

private IEventLogger $eventLogger;
private bool $registerEventEmitted = false;

/**
* @param \OCP\Files\Storage\IStorageFactory $loader
* @param IUserMountCache $mountCache
*/
public function __construct(
IStorageFactory $loader,
IUserMountCache $mountCache,
IEventLogger $eventLogger
private IStorageFactory $loader,
private IUserMountCache $mountCache,
private IEventLogger $eventLogger,
private IEventDispatcher $eventDispatcher,
) {
$this->loader = $loader;
$this->mountCache = $mountCache;
$this->eventLogger = $eventLogger;
}

private function getMountsFromProvider(IMountProvider $provider, IUser $user, IStorageFactory $loader): array {
Expand Down Expand Up @@ -91,12 +77,12 @@ private function getUserMountsForProviders(IUser $user, array $providers): array
}

public function getMountsForUser(IUser $user): array {
return $this->getUserMountsForProviders($user, $this->providers);
return $this->getUserMountsForProviders($user, $this->getProviders());
}

public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array {
$providers = array_filter(
$this->providers,
$this->getProviders(),
fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses))
);
return $this->getUserMountsForProviders($user, $providers);
Expand All @@ -107,9 +93,9 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla
// to check for name collisions
$firstMounts = [];
if ($providerFilter) {
$providers = array_filter($this->providers, $providerFilter);
$providers = array_filter($this->getProviders(), $providerFilter);
} else {
$providers = $this->providers;
$providers = $this->getProviders();
}
$firstProviders = array_filter($providers, function (IMountProvider $provider) {
return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
Expand Down Expand Up @@ -236,6 +222,10 @@ public function clearProviders() {
}

public function getProviders(): array {
if (!$this->registerEventEmitted) {
$this->registerEventEmitted = true;
$this->eventDispatcher->dispatchTyped(new RegisterMountProviderEvent($this));
}
return $this->providers;
}
}
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,8 @@ public function __construct($webRoot, \OC\Config $config) {
$loader = $c->get(IStorageFactory::class);
$mountCache = $c->get(IUserMountCache::class);
$eventLogger = $c->get(IEventLogger::class);
$manager = new MountProviderCollection($loader, $mountCache, $eventLogger);
$eventDispatcher = $c->get(IEventDispatcher::class);
$manager = new MountProviderCollection($loader, $mountCache, $eventLogger, $eventDispatcher);

// builtin providers

Expand Down
45 changes: 45 additions & 0 deletions lib/public/Files/Events/RegisterMountProviderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @copyright Copyright (c) 2024 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\Files\Events;

use OCP\EventDispatcher\Event;
use OCP\Files\Config\IMountProviderCollection;

/** @since 30.0.0 */
class RegisterMountProviderEvent extends Event {
/**
* @since 30.0.0
*/
public function __construct(
private IMountProviderCollection $mountProviderCollection,
) {
}

/**
* Get the mount provider collection to register new providers
* @since 30.0.0
*/
public function getProviderCollection(): IMountProviderCollection {
return $this->mountProviderCollection;
}
}

0 comments on commit f2d0c53

Please sign in to comment.