Skip to content

Commit

Permalink
Drop Symfony event dispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Aug 18, 2023
1 parent 6347879 commit 5d63edc
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 72 deletions.
13 changes: 8 additions & 5 deletions lib/AppInfo/Application.php
Expand Up @@ -37,6 +37,7 @@
use OCA\Activity\Listener\UserDeleted;
use OCA\Activity\MailQueueHandler;
use OCA\Activity\NotificationGenerator;
use OCA\Activity\ShareEventListener;
use OCA\Files\Event\LoadSidebar;
use OCP\Activity\IManager;
use OCP\AppFramework\App;
Expand All @@ -52,6 +53,8 @@
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\RichObjectStrings\IValidator;
use OCP\Share\Events\BeforeShareDeletedEvent;
use OCP\Share\Events\ShareDeletedFromSelfEvent;
use OCP\User\Events\PostLoginEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\Util;
Expand Down Expand Up @@ -136,11 +139,12 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserDeletedEvent::class, UserDeleted::class);
$context->registerEventListener(PostLoginEvent::class, SetUserDefaults::class);
$context->registerDashboardWidget(ActivityWidget::class);

$this->registerFilesActivity($context);
}

public function boot(IBootContext $context): void {
$this->registerActivityConsumer();
$this->registerFilesActivity();
$this->registerNotifier();
}

Expand All @@ -165,7 +169,7 @@ public function registerNotifier() {
/**
* Register the hooks for filesystem operations
*/
private function registerFilesActivity() {
private function registerFilesActivity(IRegistrationContext $context) {
// All other events from other apps have to be send via the Consumer
Util::connectHook('OC_Filesystem', 'post_create', FilesHooksStatic::class, 'fileCreate');
Util::connectHook('OC_Filesystem', 'post_update', FilesHooksStatic::class, 'fileUpdate');
Expand All @@ -175,8 +179,7 @@ private function registerFilesActivity() {
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', FilesHooksStatic::class, 'fileRestore');
Util::connectHook('OCP\Share', 'post_shared', FilesHooksStatic::class, 'share');

$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$eventDispatcher->addListener('OCP\Share::preUnshare', [FilesHooksStatic::class, 'unShare']);
$eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', [FilesHooksStatic::class, 'unShareSelf']);
$context->registerEventListener(BeforeShareDeletedEvent::class, ShareEventListener::class);
$context->registerEventListener(ShareDeletedFromSelfEvent::class, ShareEventListener::class);
}
}
52 changes: 13 additions & 39 deletions lib/Controller/ActivitiesController.php
Expand Up @@ -23,53 +23,26 @@
namespace OCA\Activity\Controller;

use OCA\Activity\Data;
use OCA\Activity\Event\LoadAdditionalScriptsEvent;
use OCA\Activity\Navigation;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\IEventDispatcher;

class ActivitiesController extends Controller {
/** @var IConfig */
protected $config;

/** @var Data */
protected $data;

/** @var IL10N */
private $l10n;

/** @var Navigation */
protected $navigation;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

/**
* @param string $appName
* @param IRequest $request
* @param IConfig $config
* @param Data $data
* @param Navigation $navigation
* @param EventDispatcherInterface $eventDispatcher
* @param IL10N $l10n
*/
public function __construct($appName,
public function __construct(
string $appName,
IRequest $request,
IConfig $config,
Data $data,
Navigation $navigation,
EventDispatcherInterface $eventDispatcher,
IL10N $l10n) {
protected IConfig $config,
protected Data $data,
protected Navigation $navigation,
protected IEventDispatcher $eventDispatcher,
private IL10N $l10n
) {
parent::__construct($appName, $request);
$this->data = $data;
$this->config = $config;
$this->navigation = $navigation;
$this->eventDispatcher = $eventDispatcher;
$this->l10n = $l10n;
}

/**
Expand All @@ -82,8 +55,9 @@ public function __construct($appName,
public function showList($filter = 'all') {
$filter = $this->data->validateFilter($filter);

$event = new GenericEvent($filter);
$this->eventDispatcher->dispatch('OCA\Activity::loadAdditionalScripts', $event);
$event = new LoadAdditionalScriptsEvent($filter);
$this->eventDispatcher->dispatchTyped($event);
$this->eventDispatcher->dispatch(LoadAdditionalScriptsEvent::EVENT_ENTITY, $event);

return new TemplateResponse('activity', 'stream.body', [
'appNavigation' => $this->navigation->getTemplate($filter),
Expand Down
38 changes: 38 additions & 0 deletions lib/Event/LoadAdditionalScriptsEvent.php
@@ -0,0 +1,38 @@
<?php
/**
* @copyright Copyright (c) 2023, Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Activity\Event;

use OCP\EventDispatcher\Event;

/**
* @since 28.0.0 Dispatched as a typed event
*/
class LoadAdditionalScriptsEvent extends Event {
/**
* @deprecated 28.0.0 - Listen to the typed event instead.
*/
public const EVENT_ENTITY = 'OCA\Activity::loadAdditionalScripts';

public function __construct(public readonly string $filter) {
parent::__construct();
}
}
25 changes: 0 additions & 25 deletions lib/FilesHooksStatic.php
Expand Up @@ -24,9 +24,6 @@

namespace OCA\Activity;

use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* The class to handle the filesystem hooks
*/
Expand Down Expand Up @@ -93,26 +90,4 @@ public static function fileRestore($params) {
public static function share($params) {
self::getHooks()->share($params);
}

/**
* Unsharing event
* @param GenericEvent $event
*/
public static function unShare(GenericEvent $event) {
$share = $event->getSubject();
if ($share instanceof IShare) {
self::getHooks()->unShare($share);
}
}

/**
* "Unsharing a share from self only" event
* @param GenericEvent $event
*/
public static function unShareSelf(GenericEvent $event) {
$share = $event->getSubject();
if ($share instanceof IShare) {
self::getHooks()->unShareSelf($share);
}
}
}
64 changes: 64 additions & 0 deletions lib/ShareEventListener.php
@@ -0,0 +1,64 @@
<?php
/**
* @copyright Copyright (c) 2023, Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Activity;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Share\Events\BeforeShareDeletedEvent;
use OCP\Share\Events\ShareDeletedFromSelfEvent;

/**
* The class to handle the share events
*/
class ShareEventListener implements IEventListener {

public function __construct (
private FilesHooks $fileHooks,
) {}

public function handle(Event $event): void {
if ($event instanceof BeforeShareDeletedEvent) {
$this->unShare($event);
}

if ($event instanceof ShareDeletedFromSelfEvent) {
$this->unShareSelf($event);
}
}

/**
* Unsharing event
*/
public function unShare(BeforeShareDeletedEvent $event): void {
$share = $event->getShare();
$this->fileHooks->unShare($share);
}

/**
* "Unsharing a share from self only" event
*/
public function unShareSelf(ShareDeletedFromSelfEvent $event): void {
$share = $event->getShare();
$this->fileHooks->unShareSelf($share);
}
}
6 changes: 3 additions & 3 deletions tests/Controller/ActivitiesControllerTest.php
Expand Up @@ -33,7 +33,7 @@
use OCP\IRequest;
use OCP\Template;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\EventDispatcher\IEventDispatcher;

/**
* Class ActivitiesTest
Expand All @@ -48,7 +48,7 @@ class ActivitiesControllerTest extends TestCase {
protected $config;
/** @var Data|MockObject */
protected $data;
/** @var EventDispatcherInterface|MockObject */
/** @var IEventDispatcher|MockObject */
protected $eventDispatcher;
/** @var Navigation|MockObject */
protected $navigation;
Expand All @@ -64,7 +64,7 @@ protected function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->data = $this->createMock(Data::class);
$this->navigation = $this->createMock(Navigation::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->request = $this->createMock(IRequest::class);
$this->l10n = $this->createMock(IL10N::class);

Expand Down

0 comments on commit 5d63edc

Please sign in to comment.