From 5919f97e39e7f584dfdc7645568ceba3b5b1dece Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 2 Feb 2022 15:19:18 +0100 Subject: [PATCH 1/3] Allow splitting activities to another database Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 422aaab71..e61b71523 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,19 +23,34 @@ namespace OCA\Activity\AppInfo; +use OC\DB\ConnectionAdapter; use OC\Files\View; +use OC\SystemConfig; use OCA\Activity\Capabilities; use OCA\Activity\Consumer; +use OCA\Activity\Data; use OCA\Activity\FilesHooksStatic; use OCA\Activity\Hooks; use OCA\Activity\Listener\LoadSidebarScripts; +use OCA\Activity\MailQueueHandler; use OCA\Activity\NotificationGenerator; use OCA\Files\Event\LoadSidebar; +use OCP\Activity\IManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\IConfig; +use OCP\IDateTimeFormatter; +use OCP\IDBConnection; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\L10N\IFactory; +use OCP\Mail\IMailer; +use OCP\RichObjectStrings\IValidator; use OCP\Util; +use Psr\Container\ContainerInterface; class Application extends App implements IBootstrap { public const APP_ID = 'activity'; @@ -45,6 +60,47 @@ public function __construct() { } public function register(IRegistrationContext $context): void { + $context->registerService('ActivityDBConnection', function (ContainerInterface $c) { + $systemConfig = $c->get(SystemConfig::class); + $factory = new \OC\DB\ConnectionFactory($systemConfig); + $type = $systemConfig->getValue('dbtype', 'sqlite'); + if (!$factory->isValidType($type)) { + throw new \OC\DatabaseException('Invalid database type'); + } + $connectionParams = $factory->createConnectionParams('activity_'); + $connection = $factory->getConnection($type, $connectionParams); + $connection->getConfiguration()->setSQLLogger($c->get(\OCP\Diagnostics\IQueryLogger::class)); + return $connection; + }); + + $context->registerService('ActivityConnectionAdapter', function (ContainerInterface $c) { + return new ConnectionAdapter( + $c->get('ActivityDBConnection') + ); + }); + + $context->registerService(Data::class, function (ContainerInterface $c) { + return new Data( + $c->get(IManager::class), + $c->get('ActivityConnectionAdapter') + ); + }); + + $context->registerService(MailQueueHandler::class, function (ContainerInterface $c) { + return new MailQueueHandler( + $c->get(IDateTimeFormatter::class), + $c->get('ActivityConnectionAdapter'), + $c->get(IMailer::class), + $c->get(IURLGenerator::class), + $c->get(IUserManager::class), + $c->get(IFactory::class), + $c->get(IManager::class), + $c->get(IValidator::class), + $c->get(IConfig::class), + $c->get(ILogger::class) + ); + }); + // Allow automatic DI for the View, until we migrated to Nodes API $context->registerService(View::class, function () { return new View(''); From 34ce95cc719acf8366a67f3fce1bca327043b5b2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 3 Feb 2022 12:43:25 +0100 Subject: [PATCH 2/3] Fall back to the normal DB when nothing is special Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e61b71523..937f297e5 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -74,6 +74,18 @@ public function register(IRegistrationContext $context): void { }); $context->registerService('ActivityConnectionAdapter', function (ContainerInterface $c) { + $systemConfig = $c->get(SystemConfig::class); + $configPrefix = 'activity_'; + + if ($systemConfig->getValue($configPrefix . 'dbuser', null) === null && + $systemConfig->getValue($configPrefix . 'dbpassword', null) === null && + $systemConfig->getValue($configPrefix . 'dbname', null) === null && + $systemConfig->getValue($configPrefix . 'dbhost', null) === null && + $systemConfig->getValue($configPrefix . 'dbport', null) === null && + $systemConfig->getValue($configPrefix . 'dbdriveroptions', null) === null) { + return $c->get(IDBConnection::class); + } + return new ConnectionAdapter( $c->get('ActivityDBConnection') ); From 96cab258ecf9159f3530fa0f587cd3dfbf0183df Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Feb 2022 10:51:24 +0100 Subject: [PATCH 3/3] Ignore psalm in this cases Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 937f297e5..61d38df5a 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -59,20 +59,28 @@ public function __construct() { parent::__construct(self::APP_ID); } + /** + * @psalm-suppress UndefinedClass + */ public function register(IRegistrationContext $context): void { $context->registerService('ActivityDBConnection', function (ContainerInterface $c) { $systemConfig = $c->get(SystemConfig::class); $factory = new \OC\DB\ConnectionFactory($systemConfig); $type = $systemConfig->getValue('dbtype', 'sqlite'); if (!$factory->isValidType($type)) { + /** @psalm-suppress InvalidThrow */ throw new \OC\DatabaseException('Invalid database type'); } $connectionParams = $factory->createConnectionParams('activity_'); $connection = $factory->getConnection($type, $connectionParams); + /** @psalm-suppress MissingDependency */ $connection->getConfiguration()->setSQLLogger($c->get(\OCP\Diagnostics\IQueryLogger::class)); return $connection; }); + /** + * @psalm-suppress UndefinedClass + */ $context->registerService('ActivityConnectionAdapter', function (ContainerInterface $c) { $systemConfig = $c->get(SystemConfig::class); $configPrefix = 'activity_';