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 6f3bc93
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 38 deletions.
44 changes: 44 additions & 0 deletions OCA/Collectives/Listeners/Event/RegisterMountProviderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 OCA\Collectives\Listeners\Event;

use OCA\Collectives\Mount\MountProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\RegisterMountProviderEvent;

class RegisterMountProviderListener implements IEventListener {

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

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

$event->getProviderCollection()->registerProvider($this->mountProvider);
}
}
10 changes: 4 additions & 6 deletions apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -40,6 +41,7 @@
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,6 +97,8 @@ 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 {
Expand All @@ -107,12 +111,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);
}
}
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));

Check failure

Code scanning / Psalm

TooManyArguments Error

Too many arguments for OCP\Files\Events\RegisterMountProviderEvent::__construct - expecting 0 but saw 1

Check failure on line 227 in lib/private/Files/Config/MountProviderCollection.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TooManyArguments

lib/private/Files/Config/MountProviderCollection.php:227:42: TooManyArguments: Too many arguments for OCP\Files\Events\RegisterMountProviderEvent::__construct - expecting 0 but saw 1 (see https://psalm.dev/026)
}
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
34 changes: 34 additions & 0 deletions lib/public/Files/Events/RegisterMountProviderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;

class RegisterMountProviderEvent extends Event {

Check failure on line 28 in lib/public/Files/Events/RegisterMountProviderEvent.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Files/Events/RegisterMountProviderEvent.php:28:1: InvalidDocblock: PHPDoc is required for classes/interfaces in OCP. (see https://psalm.dev/008)
private IMountProviderCollection $providerCollection;

public function getProviderCollection(): IMountProviderCollection {

Check failure on line 31 in lib/public/Files/Events/RegisterMountProviderEvent.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Files/Events/RegisterMountProviderEvent.php:31:2: InvalidDocblock: PHPDoc is required for methods in OCP. (see https://psalm.dev/008)
return $this->providerCollection;
}
}

0 comments on commit 6f3bc93

Please sign in to comment.