From bdfb2518766d2975bf644662c606fbf35d58590d Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 17 Jun 2023 13:09:00 +0200 Subject: [PATCH] Fix emmiter --- appinfo/info.xml | 3 ++ lib/AppInfo/Application.php | 27 ++++++-------- lib/Hooks.php | 36 +++++++++++++++---- .../OC/Hooks/Emitter.php | 2 -- 4 files changed, 44 insertions(+), 24 deletions(-) rename "stubs/oc/Hooks\\Emitter.php" => stubs/OC/Hooks/Emitter.php (90%) diff --git a/appinfo/info.xml b/appinfo/info.xml index 40beaca..fb5f44f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -52,4 +52,7 @@ See [README] for more exhaustive information on features and potential misfeatur OCA\Epubreader\Settings\Personal OCA\Epubreader\Settings\PersonalSection + + + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 1c0d0c2..d257f31 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -14,32 +14,27 @@ namespace OCA\Epubreader\AppInfo; use OCA\Epubreader\Hooks; use OCP\AppFramework\App; -use OCP\AppFramework\Bootstrap\IBootContext; -use OCP\AppFramework\Bootstrap\IBootstrap; -use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Files\IRootFolder; +use OCP\IDBConnection; use OCP\Util; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -class Application extends App implements IBootstrap { +class Application extends App { public const APP_ID = 'epubreader'; public function __construct() { parent::__construct(self::APP_ID); - Util::addscript(self::APP_ID, 'plugin'); - } + /** @psalm-suppress DeprecatedInterface */ + $container = $this->getContainer(); + $hooks = new Hooks( + $container->get(IRootFolder::class), + $container->get(IDBConnection::class), + ); + $hooks->register(); - public function boot(IBootContext $context): void { /** @psalm-suppress DeprecatedMethod */ Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings'); - - $context->injectFn(function (EventDispatcherInterface $dispatcher) { - $dispatcher->addListener('OC\Files::preDelete', [Hooks::class, 'deleteFile']); - $dispatcher->addListener('OC\User::preDelete', [Hooks::class, 'deleteUser']); - }); - } - - public function register(IRegistrationContext $context): void { + Util::addscript(self::APP_ID, 'plugin'); } } diff --git a/lib/Hooks.php b/lib/Hooks.php index 3864fb3..b124ae6 100644 --- a/lib/Hooks.php +++ b/lib/Hooks.php @@ -11,13 +11,37 @@ namespace OCA\Epubreader; use OCA\Epubreader\AppInfo\Application; +use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IUser; use OCP\IUserSession; use OCP\Server; class Hooks { + private IRootFolder $rootFolder; + private IDBConnection $dbConnection; + + public function __construct( + IRootFolder $rootFolder, + IDBConnection $dbConnection, + ) { + $this->rootFolder = $rootFolder; + $this->dbConnection = $dbConnection; + } + + public function register(): void { + $this->rootFolder->listen('\OC\Files', 'preDelete', function (Node $node) { + $this->deleteFile($node->getId()); + }); + + $this->rootFolder->listen('\OC\User', 'preDelete', function (IUser $user) { + $this->deleteUser($user->getUID()); + }); + } + public static function announce_settings(array $settings): void { // Nextcloud encodes this as JSON, Owncloud does not (yet) (#75) // TODO: remove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties @@ -39,22 +63,22 @@ class Hooks { } } - protected static function deleteFile(IDBConnection $connection, int $fileId): void { - $queryBuilder = $connection->getQueryBuilder(); + protected function deleteFile(int $fileId): void { + $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('file_id = file_id')->setParameter('file_id', $fileId); $queryBuilder->executeStatement(); - $queryBuilder = $connection->getQueryBuilder(); + $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_prefs')->where('file_id = file_id')->setParameter('file_id', $fileId); $queryBuilder->executeStatement(); } - protected static function deleteUser(IDBConnection $connection, string $userId): void { - $queryBuilder = $connection->getQueryBuilder(); + protected function deleteUser(string $userId): void { + $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('user_id = user_id')->setParameter('user_id', $userId); $queryBuilder->executeStatement(); - $queryBuilder = $connection->getQueryBuilder(); + $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_prefs')->where('user_id = user_id')->setParameter('user_id', $userId); $queryBuilder->executeStatement(); } diff --git "a/stubs/oc/Hooks\\Emitter.php" b/stubs/OC/Hooks/Emitter.php similarity index 90% rename from "stubs/oc/Hooks\\Emitter.php" rename to stubs/OC/Hooks/Emitter.php index bc5af2b..dd9cf31 100644 --- "a/stubs/oc/Hooks\\Emitter.php" +++ b/stubs/OC/Hooks/Emitter.php @@ -38,7 +38,6 @@ interface Emitter { * @param string $method * @param callable $callback * @return void - * @deprecated 18.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener */ public function listen($scope, $method, callable $callback); @@ -47,7 +46,6 @@ interface Emitter { * @param string $method optional * @param callable $callback optional * @return void - * @deprecated 18.0.0 use \OCP\EventDispatcher\IEventDispatcher::removeListener */ public function removeListener($scope = null, $method = null, callable $callback = null); }