Refacto all project #1

Merged
Xefir merged 18 commits from test into master 2023-06-17 23:48:37 +00:00
4 changed files with 44 additions and 24 deletions
Showing only changes of commit bdfb251876 - Show all commits

View File

@ -52,4 +52,7 @@ See [README] for more exhaustive information on features and potential misfeatur
<personal>OCA\Epubreader\Settings\Personal</personal>
<personal-section>OCA\Epubreader\Settings\PersonalSection</personal-section>
</settings>
<types>
<filesystem/>
</types>
</info>

View File

@ -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');
}
}

View File

@ -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();
}

View File

@ -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);
}