Skip to content

Commit

Permalink
Merge pull request #10078 from nextcloud/techdebt/noid/prepare-28-eve…
Browse files Browse the repository at this point in the history
…nt-dispatcher

fix: Stop using Symfony event dispatcher directly
  • Loading branch information
nickvergessen authored Aug 4, 2023
2 parents e9d641c + 982df1c commit c888ea9
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 42 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function registerCollaborationResourceProvider(IServerContainer $serve
/** @var IProviderManager $resourceManager */
$resourceManager = $server->get(IProviderManager::class);
$resourceManager->registerResourceProvider(ConversationProvider::class);
$server->getEventDispatcher()->addListener(LoadAdditionalScriptsEvent::class, static function () {
$server->get(IEventDispatcher::class)->addListener(LoadAdditionalScriptsEvent::class, static function () {
Util::addScript(self::APP_ID, 'talk-collections');
});
}
Expand Down
25 changes: 8 additions & 17 deletions lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\Server;
use OCP\Share\Events\BeforeShareCreatedEvent;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* @template-implements IEventListener<Event>
Expand Down Expand Up @@ -86,8 +87,8 @@ public static function register(IEventDispatcher $dispatcher): void {
$dispatcher->addListener(Room::EVENT_AFTER_USERS_ADD, self::class . '::addSystemMessageUserAdded');
$dispatcher->addListener(Room::EVENT_AFTER_USER_REMOVE, self::class . '::sendSystemMessageUserRemoved');
$dispatcher->addListener(Room::EVENT_AFTER_PARTICIPANT_TYPE_SET, self::class . '::sendSystemMessageAboutPromoteOrDemoteModerator');
$dispatcher->addListener('OCP\Share::preShare', self::class . '::setShareExpiration');
$dispatcher->addListener('OCP\Share::postShare', self::class . '::fixMimeTypeOfVoiceMessage');
$dispatcher->addListener(BeforeShareCreatedEvent::class, self::class . '::setShareExpiration');
$dispatcher->addListener(ShareCreatedEvent::class, self::class . '::fixMimeTypeOfVoiceMessage');
$dispatcher->addListener(RoomShareProvider::EVENT_SHARE_FILE_AGAIN, self::class . '::fixMimeTypeOfVoiceMessage');
$dispatcher->addListener(Room::EVENT_AFTER_SET_MESSAGE_EXPIRATION, self::class . '::afterSetMessageExpiration');
$dispatcher->addListener(Room::EVENT_AFTER_SET_CALL_RECORDING, self::class . '::setCallRecording');
Expand Down Expand Up @@ -330,13 +331,8 @@ public static function sendSystemMessageAboutPromoteOrDemoteModerator(ModifyPart
}
}

/**
* @param GenericEvent|Event $event
* @return void
*/
public static function setShareExpiration($event): void {
/** @var IShare $share */
$share = $event->getSubject();
public static function setShareExpiration(BeforeShareCreatedEvent $event): void {
$share = $event->getShare();

if ($share->getShareType() !== IShare::TYPE_ROOM) {
return;
Expand All @@ -357,13 +353,8 @@ public static function setShareExpiration($event): void {
$share->setExpirationDate($dateTime);
}

/**
* @param GenericEvent|Event $event
* @return void
*/
public static function fixMimeTypeOfVoiceMessage($event): void {
/** @var IShare $share */
$share = $event->getSubject();
public static function fixMimeTypeOfVoiceMessage(ShareCreatedEvent $event): void {
$share = $event->getShare();

if ($share->getShareType() !== IShare::TYPE_ROOM) {
return;
Expand Down
11 changes: 5 additions & 6 deletions lib/Share/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
use OCA\Talk\Config;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Server;
use OCP\Share\Events\BeforeShareCreatedEvent;
use OCP\Share\Events\VerifyMountPointEvent;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;

class Listener {
public static function register(IEventDispatcher $dispatcher): void {
/**
* @psalm-suppress UndefinedClass
*/
$dispatcher->addListener('OCP\Share::preShare', [self::class, 'listenPreShare'], 1000);
$dispatcher->addListener(BeforeShareCreatedEvent::class, [self::class, 'listenPreShare'], 1000);
$dispatcher->addListener(VerifyMountPointEvent::class, [self::class, 'listenVerifyMountPointEvent'], 1000);
}

public static function listenPreShare(GenericEvent $event): void {
public static function listenPreShare(BeforeShareCreatedEvent $event): void {
$listener = Server::get(self::class);
$listener->overwriteShareTarget($event);
}
Expand All @@ -55,9 +55,8 @@ public function __construct(
) {
}

public function overwriteShareTarget(GenericEvent $event): void {
/** @var IShare $share */
$share = $event->getSubject();
public function overwriteShareTarget(BeforeShareCreatedEvent $event): void {
$share = $event->getShare();

if ($share->getShareType() !== IShare::TYPE_ROOM
&& $share->getShareType() !== RoomShareProvider::SHARE_TYPE_USERROOM) {
Expand Down
2 changes: 0 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<referencedClass name="OCA\Circles\Model\Member" />
<referencedClass name="OCA\DAV\CardDAV\PhotoCache" />
<referencedClass name="OCA\FederatedFileSharing\AddressHandler" />
<referencedClass name="Symfony\Component\EventDispatcher\EventDispatcherInterface" />
</errorLevel>
</UndefinedDocblockClass>
<UndefinedInterfaceMethod>
Expand All @@ -87,6 +86,5 @@
<file name="tests/stubs/GuzzleHttp_Exception_ClientException.php" />
<file name="tests/stubs/GuzzleHttp_Exception_ConnectException.php" />
<file name="tests/stubs/GuzzleHttp_Exception_ServerException.php" />
<file name="tests/stubs/Symfony_Component_EventDispatcher_GenericEvent.php" />
</stubs>
</psalm>
5 changes: 1 addition & 4 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
<files psalm-version="5.14.1@b9d355e0829c397b9b3b47d0c0ed042a8a70284d">
<file src="lib/AppInfo/Application.php">
<UndefinedClass>
<code>BeforeTemplateRenderedEvent</code>
Expand Down Expand Up @@ -236,9 +236,6 @@
</UndefinedClass>
</file>
<file src="lib/Share/Listener.php">
<InvalidArgument>
<code><![CDATA[[self::class, 'listenPreShare']]]></code>
</InvalidArgument>
<UndefinedClass>
<code><![CDATA[$event->getView()]]></code>
<code><![CDATA[$event->getView()]]></code>
Expand Down

This file was deleted.

0 comments on commit c888ea9

Please sign in to comment.