diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-09-04 18:05:19 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-09-05 14:45:50 +0300 |
commit | d18ef16578dc34ffd67e48bc5c61654e55461ce9 (patch) | |
tree | 0fb85d03cba26c56be550b2cba5072dd6aca4e91 /lib/AppInfo | |
parent | eec81c6c38259c0edc843018581a1f1bd1ab280a (diff) |
Move message sending and draft saving code
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/AppInfo')
-rw-r--r-- | lib/AppInfo/Application.php | 50 | ||||
-rw-r--r-- | lib/AppInfo/BootstrapSingleton.php | 125 |
2 files changed, 135 insertions, 40 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 873eef879..87ce3bffc 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -30,8 +30,16 @@ use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Contracts\IMailTransmission; use OCA\Mail\Contracts\IUserPreferences; use OCA\Mail\Events\BeforeMessageDeletedEvent; +use OCA\Mail\Events\DraftSavedEvent; +use OCA\Mail\Events\MessageSentEvent; +use OCA\Mail\Events\SaveDraftEvent; use OCA\Mail\Http\Middleware\ErrorMiddleware; -use OCA\Mail\Listener\MessageDeleteTrashCreatorListener; +use OCA\Mail\Listener\AddressCollectionListener; +use OCA\Mail\Listener\DeleteDraftListener; +use OCA\Mail\Listener\DraftMailboxCreatorListener; +use OCA\Mail\Listener\FlagRepliedMessageListener; +use OCA\Mail\Listener\TrashMailboxCreatorListener; +use OCA\Mail\Listener\SaveSentMessageListener; use OCA\Mail\Service\Attachment\AttachmentService; use OCA\Mail\Service\AvatarService; use OCA\Mail\Service\Group\IGroupService; @@ -48,45 +56,7 @@ class Application extends App { public function __construct(array $urlParams = []) { parent::__construct('mail', $urlParams); - $this->initializeAppContainer(); - $this->registerEvents(); - } - - private function initializeAppContainer() { - $container = $this->getContainer(); - - $transport = $container->getServer()->getConfig()->getSystemValue('app.mail.transport', 'smtp'); - $testSmtp = $transport === 'smtp'; - - $container->registerAlias(IAvatarService::class, AvatarService::class); - $container->registerAlias(IAttachmentService::class, AttachmentService::class); - $container->registerAlias(IMailManager::class, MailManager::class); - $container->registerAlias(IMailTransmission::class, MailTransmission::class); - $container->registerAlias(IUserPreferences::class, UserPreferenceSevice::class); - $container->registerService('OCP\ISession', function ($c) { - return $c->getServer()->getSession(); - }); - - $container->registerParameter("appName", "mail"); - $container->registerService("userFolder", function () use ($container) { - $user = $container->query("UserId"); - return $container->getServer()->getUserFolder($user); - }); - - $container->registerParameter("testSmtp", $testSmtp); - $container->registerParameter("hostname", Util::getServerHostName()); - - $container->registerAlias('ErrorMiddleware', ErrorMiddleware::class); - $container->registerMiddleWare('ErrorMiddleware'); - - $container->registerAlias(IGroupService::class, NextcloudGroupService::class); - } - - private function registerEvents(): void { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->getContainer()->query(IEventDispatcher::class); - - $dispatcher->addServiceListener(BeforeMessageDeletedEvent::class, MessageDeleteTrashCreatorListener::class); + BootstrapSingleton::getInstance($this->getContainer())->boot(); } } diff --git a/lib/AppInfo/BootstrapSingleton.php b/lib/AppInfo/BootstrapSingleton.php new file mode 100644 index 000000000..be8f93ca8 --- /dev/null +++ b/lib/AppInfo/BootstrapSingleton.php @@ -0,0 +1,125 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Mail\AppInfo; + +use OCA\Mail\Contracts\IAttachmentService; +use OCA\Mail\Contracts\IAvatarService; +use OCA\Mail\Contracts\IMailManager; +use OCA\Mail\Contracts\IMailTransmission; +use OCA\Mail\Contracts\IUserPreferences; +use OCA\Mail\Events\BeforeMessageDeletedEvent; +use OCA\Mail\Events\DraftSavedEvent; +use OCA\Mail\Events\MessageSentEvent; +use OCA\Mail\Events\SaveDraftEvent; +use OCA\Mail\Http\Middleware\ErrorMiddleware; +use OCA\Mail\Listener\AddressCollectionListener; +use OCA\Mail\Listener\DeleteDraftListener; +use OCA\Mail\Listener\DraftMailboxCreatorListener; +use OCA\Mail\Listener\FlagRepliedMessageListener; +use OCA\Mail\Listener\SaveSentMessageListener; +use OCA\Mail\Listener\TrashMailboxCreatorListener; +use OCA\Mail\Service\Attachment\AttachmentService; +use OCA\Mail\Service\AvatarService; +use OCA\Mail\Service\Group\IGroupService; +use OCA\Mail\Service\Group\NextcloudGroupService; +use OCA\Mail\Service\MailManager; +use OCA\Mail\Service\MailTransmission; +use OCA\Mail\Service\UserPreferenceSevice; +use OCP\AppFramework\IAppContainer; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IContainer; +use OCP\Util; + +class BootstrapSingleton { + + /** @var BootstrapSingleton */ + private static $instance = null; + + /** @var bool */ + private $booted = false; + + /** @var IAppContainer */ + private $container; + + private function __construct(IAppContainer $container) { + $this->container = $container; + } + + public static function getInstance(IAppContainer $container): BootstrapSingleton { + if (self::$instance === null) { + self::$instance = new static($container); + } + + return self::$instance; + } + + public function boot(): void { + if ($this->booted) { + return; + } + + $this->initializeAppContainer($this->container); + $this->registerEvents($this->container); + + $this->booted = true; + } + + private function initializeAppContainer(IAppContainer $container) { + $transport = $container->getServer()->getConfig()->getSystemValue('app.mail.transport', 'smtp'); + $testSmtp = $transport === 'smtp'; + + $container->registerAlias(IAvatarService::class, AvatarService::class); + $container->registerAlias(IAttachmentService::class, AttachmentService::class); + $container->registerAlias(IMailManager::class, MailManager::class); + $container->registerAlias(IMailTransmission::class, MailTransmission::class); + $container->registerAlias(IUserPreferences::class, UserPreferenceSevice::class); + + $container->registerService("userFolder", function () use ($container) { + $user = $container->query("UserId"); + return $container->getServer()->getUserFolder($user); + }); + + $container->registerParameter('testSmtp', $testSmtp); + $container->registerParameter('hostname', Util::getServerHostName()); + + $container->registerAlias('ErrorMiddleware', ErrorMiddleware::class); + $container->registerMiddleWare('ErrorMiddleware'); + + $container->registerAlias(IGroupService::class, NextcloudGroupService::class); + } + + private function registerEvents(IAppContainer $container): void { + /** @var IEventDispatcher $dispatcher */ + $dispatcher = $container->query(IEventDispatcher::class); + + $dispatcher->addServiceListener(BeforeMessageDeletedEvent::class, TrashMailboxCreatorListener::class); + $dispatcher->addServiceListener(DraftSavedEvent::class, DeleteDraftListener::class); + $dispatcher->addServiceListener(MessageSentEvent::class, AddressCollectionListener::class); + $dispatcher->addServiceListener(MessageSentEvent::class, DeleteDraftListener::class); + $dispatcher->addServiceListener(MessageSentEvent::class, FlagRepliedMessageListener::class); + $dispatcher->addServiceListener(MessageSentEvent::class, SaveSentMessageListener::class); + $dispatcher->addServiceListener(SaveDraftEvent::class, DraftMailboxCreatorListener::class); + } + +} |