Skip to content

Commit

Permalink
feat: mail provider backend
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Jul 13, 2024
1 parent 4b296c7 commit b1f6e45
Show file tree
Hide file tree
Showing 25 changed files with 2,288 additions and 27 deletions.
82 changes: 57 additions & 25 deletions apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Mail\Provider\IManager as IMailManager;
use OCP\Mail\Provider\IMessageSend;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin;
Expand Down Expand Up @@ -55,6 +57,7 @@ class IMipPlugin extends SabreIMipPlugin {
public const METHOD_CANCEL = 'cancel';
public const IMIP_INDENT = 15; // Enough for the length of all body bullet items, in all languages
private EventComparisonService $eventComparisonService;
private IMailManager $mailManager;

public function __construct(IConfig $config,
IMailer $mailer,
Expand All @@ -63,7 +66,8 @@ public function __construct(IConfig $config,
Defaults $defaults,
IUserSession $userSession,
IMipService $imipService,
EventComparisonService $eventComparisonService) {
EventComparisonService $eventComparisonService,
IMailManager $mailManager) {
parent::__construct('');
$this->userSession = $userSession;
$this->config = $config;
Expand All @@ -73,6 +77,7 @@ public function __construct(IConfig $config,
$this->defaults = $defaults;
$this->imipService = $imipService;
$this->eventComparisonService = $eventComparisonService;
$this->mailManager = $mailManager;
}

public function initialize(DAV\Server $server): void {
Expand Down Expand Up @@ -212,21 +217,6 @@ public function schedule(Message $iTipMessage) {
$fromEMail = Util::getDefaultEmailAddress('invitations-noreply');
$fromName = $this->imipService->getFrom($senderName, $this->defaults->getName());

$message = $this->mailer->createMessage()
->setFrom([$fromEMail => $fromName]);

if ($recipientName !== null) {
$message->setTo([$recipient => $recipientName]);
} else {
$message->setTo([$recipient]);
}

if ($senderName !== null) {
$message->setReplyTo([$sender => $senderName]);
} else {
$message->setReplyTo([$sender]);
}

$template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
$template->addHeader();

Expand Down Expand Up @@ -268,18 +258,60 @@ public function schedule(Message $iTipMessage) {
}

$template->addFooter();

$message->useTemplate($template);

// convert iTip Message to string
$itip_msg = $iTipMessage->message->serialize();
$message->attachInline(
$itip_msg,
'event.ics',
'text/calendar; method=' . $iTipMessage->method,
);

$user = null;
$mailService = null;

try {
$failed = $this->mailer->send($message);
// retrieve user object
$user = $this->userSession->getUser();
// evaluate if user object exist
if ($user !== null) {
// retrieve appropriate service with the same address as sender
$mailService = $this->mailManager->findServiceByAddress($user->getUID(), $sender);
}
// evaluate if a mail service was found and has sending capabilities
if ($mailService !== null && $mailService instanceof IMessageSend) {
// construct mail message and set required parameters
$message = $mailService->initiateMessage();
$message->setFrom(
(new \OCP\Mail\Provider\Address($sender, $fromName))
);
$message->setTo(
(new \OCP\Mail\Provider\Address($recipient, $recipientName))
);
$message->setSubject($template->renderSubject());
$message->setBodyPlain($template->renderText());
$message->setBodyHtml($template->renderHtml());
$message->setAttachments((new \OCP\Mail\Provider\Attachment(
$itip_msg,
'event.ics',
'text/calendar; method=' . $iTipMessage->method,
true
)));
// send message
$mailService->sendMessage($message);
} else {
// construct symfony mailer message and set required parameters
$message = $this->mailer->createMessage();
$message->setFrom([$fromEMail => $fromName]);
$message->setTo(
(($recipientName !== null) ? [$recipient => $recipientName] : [$recipient])
);
$message->setReplyTo(
(($senderName !== null) ? [$sender => $senderName] : [$sender])
);
$message->useTemplate($template);
$message->attachInline(
$itip_msg,
'event.ics',
'text/calendar; method=' . $iTipMessage->method
);
$failed = $this->mailer->send($message);
}

$iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
if (!empty($failed)) {
$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ public function __construct(IRequest $request, string $baseUri) {
\OC::$server->get(\OCP\Defaults::class),
$userSession,
\OC::$server->get(\OCA\DAV\CalDAV\Schedule\IMipService::class),
\OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class)
\OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class),
\OC::$server->get(\OCP\Mail\Provider\IManager::class)
));
}
$this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
Expand Down
9 changes: 8 additions & 1 deletion apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
use OCP\Mail\Provider\IManager as IMailManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Sabre\VObject\Component\VCalendar;
Expand Down Expand Up @@ -67,6 +68,9 @@ class IMipPluginTest extends TestCase {
/** @var EventComparisonService|MockObject */
private $eventComparisonService;

/** @var MailManager|MockObject */
private $mailManager;

protected function setUp(): void {
$this->mailMessage = $this->createMock(IMessage::class);
$this->mailMessage->method('setFrom')->willReturn($this->mailMessage);
Expand Down Expand Up @@ -107,6 +111,8 @@ protected function setUp(): void {

$this->eventComparisonService = $this->createMock(EventComparisonService::class);

$this->mailManager = $this->createMock(IMailManager::class);

$this->plugin = new IMipPlugin(
$this->config,
$this->mailer,
Expand All @@ -115,7 +121,8 @@ protected function setUp(): void {
$this->defaults,
$this->userSession,
$this->service,
$this->eventComparisonService
$this->eventComparisonService,
$this->mailManager,
);
}

Expand Down
13 changes: 13 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,18 @@
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => $baseDir . '/lib/public/Mail/IMessage.php',
'OCP\\Mail\\Provider\\Address' => $baseDir . '/lib/public/Mail/Provider/Address.php',
'OCP\\Mail\\Provider\\Attachment' => $baseDir . '/lib/public/Mail/Provider/Attachment.php',
'OCP\\Mail\\Provider\\IAddress' => $baseDir . '/lib/public/Mail/Provider/IAddress.php',
'OCP\\Mail\\Provider\\IAttachment' => $baseDir . '/lib/public/Mail/Provider/IAttachment.php',
'OCP\\Mail\\Provider\\IManager' => $baseDir . '/lib/public/Mail/Provider/IManager.php',
'OCP\\Mail\\Provider\\IMessage' => $baseDir . '/lib/public/Mail/Provider/IMessage.php',
'OCP\\Mail\\Provider\\IMessageSend' => $baseDir . '/lib/public/Mail/Provider/IMessageSend.php',
'OCP\\Mail\\Provider\\IProvider' => $baseDir . '/lib/public/Mail/Provider/IProvider.php',
'OCP\\Mail\\Provider\\IService' => $baseDir . '/lib/public/Mail/Provider/IService.php',
'OCP\\Mail\\Provider\\IServiceIdentity' => $baseDir . '/lib/public/Mail/Provider/IServiceIdentity.php',
'OCP\\Mail\\Provider\\IServiceLocation' => $baseDir . '/lib/public/Mail/Provider/IServiceLocation.php',
'OCP\\Mail\\Provider\\Message' => $baseDir . '/lib/public/Mail/Provider/Message.php',
'OCP\\Migration\\BigIntMigration' => $baseDir . '/lib/public/Migration/BigIntMigration.php',
'OCP\\Migration\\IMigrationStep' => $baseDir . '/lib/public/Migration/IMigrationStep.php',
'OCP\\Migration\\IOutput' => $baseDir . '/lib/public/Migration/IOutput.php',
Expand Down Expand Up @@ -1605,6 +1617,7 @@
'OC\\Mail\\EMailTemplate' => $baseDir . '/lib/private/Mail/EMailTemplate.php',
'OC\\Mail\\Mailer' => $baseDir . '/lib/private/Mail/Mailer.php',
'OC\\Mail\\Message' => $baseDir . '/lib/private/Mail/Message.php',
'OC\\Mail\\Provider\\Manager' => $baseDir . '/lib/private/Mail/Provider/Manager.php',
'OC\\Memcache\\APCu' => $baseDir . '/lib/private/Memcache/APCu.php',
'OC\\Memcache\\ArrayCache' => $baseDir . '/lib/private/Memcache/ArrayCache.php',
'OC\\Memcache\\CADTrait' => $baseDir . '/lib/private/Memcache/CADTrait.php',
Expand Down
13 changes: 13 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,18 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => __DIR__ . '/../../..' . '/lib/public/Mail/IMessage.php',
'OCP\\Mail\\Provider\\Address' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/Address.php',
'OCP\\Mail\\Provider\\Attachment' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/Attachment.php',
'OCP\\Mail\\Provider\\IAddress' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IAddress.php',
'OCP\\Mail\\Provider\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IAttachment.php',
'OCP\\Mail\\Provider\\IManager' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IManager.php',
'OCP\\Mail\\Provider\\IMessage' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IMessage.php',
'OCP\\Mail\\Provider\\IMessageSend' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IMessageSend.php',
'OCP\\Mail\\Provider\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IProvider.php',
'OCP\\Mail\\Provider\\IService' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IService.php',
'OCP\\Mail\\Provider\\IServiceIdentity' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IServiceIdentity.php',
'OCP\\Mail\\Provider\\IServiceLocation' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/IServiceLocation.php',
'OCP\\Mail\\Provider\\Message' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/Message.php',
'OCP\\Migration\\BigIntMigration' => __DIR__ . '/../../..' . '/lib/public/Migration/BigIntMigration.php',
'OCP\\Migration\\IMigrationStep' => __DIR__ . '/../../..' . '/lib/public/Migration/IMigrationStep.php',
'OCP\\Migration\\IOutput' => __DIR__ . '/../../..' . '/lib/public/Migration/IOutput.php',
Expand Down Expand Up @@ -1638,6 +1650,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Mail\\EMailTemplate' => __DIR__ . '/../../..' . '/lib/private/Mail/EMailTemplate.php',
'OC\\Mail\\Mailer' => __DIR__ . '/../../..' . '/lib/private/Mail/Mailer.php',
'OC\\Mail\\Message' => __DIR__ . '/../../..' . '/lib/private/Mail/Message.php',
'OC\\Mail\\Provider\\Manager' => __DIR__ . '/../../..' . '/lib/private/Mail/Provider/Manager.php',
'OC\\Memcache\\APCu' => __DIR__ . '/../../..' . '/lib/private/Memcache/APCu.php',
'OC\\Memcache\\ArrayCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/ArrayCache.php',
'OC\\Memcache\\CADTrait' => __DIR__ . '/../../..' . '/lib/private/Memcache/CADTrait.php',
Expand Down
24 changes: 24 additions & 0 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\Http\WellKnown\IHandler;
use OCP\Mail\Provider\IProvider as IMailProvider;
use OCP\Notification\INotifier;
use OCP\Profile\ILinkAction;
use OCP\Search\IProvider;
Expand Down Expand Up @@ -148,6 +149,9 @@ class RegistrationContext {

/** @var ServiceRegistration<\OCP\TaskProcessing\ITaskType>[] */
private array $taskProcessingTaskTypes = [];

/** @var ServiceRegistration<IMailProvider>[] */
private $mailProviders = [];

public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
Expand Down Expand Up @@ -411,6 +415,13 @@ public function registerTaskProcessingTaskType(string $taskProcessingTaskTypeCla
$taskProcessingTaskTypeClass
);
}

public function registerMailProvider(string $class): void {
$this->context->registerMailProvider(
$this->appId,
$class
);
}
};
}

Expand Down Expand Up @@ -603,6 +614,12 @@ public function registerTaskProcessingProvider(string $appId, string $taskProces
public function registerTaskProcessingTaskType(string $appId, string $taskProcessingTaskTypeClass) {
$this->taskProcessingTaskTypes[] = new ServiceRegistration($appId, $taskProcessingTaskTypeClass);
}
/**
* @psalm-param class-string<IMailProvider> $migratorClass
*/
public function registerMailProvider(string $appId, string $class): void {
$this->mailProviders[] = new ServiceRegistration($appId, $class);
}

/**
* @param App[] $apps
Expand Down Expand Up @@ -948,4 +965,11 @@ public function getTaskProcessingProviders(): array {
public function getTaskProcessingTaskTypes(): array {
return $this->taskProcessingTaskTypes;
}

/**
* @return ServiceRegistration<IMailProvider>[]
*/
public function getMailProviders(): array {
return $this->mailProviders;
}
}
Loading

0 comments on commit b1f6e45

Please sign in to comment.