From ce6de436885daaa510959a6d15d90445e65fc016 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Tue, 18 Jul 2023 16:03:44 +0200 Subject: [PATCH] Send notification mail for circle invitation Signed-off-by: Jonas Heinrich --- .../Notifications/RequestingMember.php | 32 ++++++- lib/Service/SendMailService.php | 87 +++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/lib/Listeners/Notifications/RequestingMember.php b/lib/Listeners/Notifications/RequestingMember.php index 50a91c0cd..e83baa482 100644 --- a/lib/Listeners/Notifications/RequestingMember.php +++ b/lib/Listeners/Notifications/RequestingMember.php @@ -37,8 +37,10 @@ use OCA\Circles\Events\RequestingCircleMemberEvent; use OCA\Circles\Exceptions\RequestBuilderException; use OCA\Circles\Service\NotificationService; +use OCA\Circles\Service\SendMailService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +use OCP\IUserManager; /** * Class RequestingMember @@ -52,12 +54,23 @@ class RequestingMember implements IEventListener { /** @var NotificationService */ private $notificationService; + /** @var SendMailService */ + private $sendMailService; + + /** @var IUserManager */ + protected $userManager; /** * RequestingMember constructor. */ - public function __construct(NotificationService $notificationService) { + public function __construct( + NotificationService $notificationService, + SendMailService $sendMailService, + IUserManager $userManager + ) { $this->notificationService = $notificationService; + $this->sendMailService = $sendMailService; + $this->userManager = $userManager; $this->setup('app', Application::APP_ID); } @@ -74,11 +87,28 @@ public function handle(Event $event): void { } $member = $event->getMember(); + $circle = $event->getCircle(); if ($event->getType() === CircleGenericEvent::REQUESTED) { $this->notificationService->notificationRequested($member); } else { $this->notificationService->notificationInvited($member); + + if ($member->hasInvitedBy()) { + $author = $member->getInvitedBy()->getDisplayName(); + } else { + $author = 'someone'; + } + $userId = $member->getUserId(); + $user = $this->userManager->get($userId); + $mails = [$user->getEMailAddress()]; + + $this->sendMailService->generateInvitationMail( + $author, + $circle, + $member, + $mails + ); } } } diff --git a/lib/Service/SendMailService.php b/lib/Service/SendMailService.php index 0662578f6..f993adc8e 100644 --- a/lib/Service/SendMailService.php +++ b/lib/Service/SendMailService.php @@ -88,6 +88,93 @@ public function __construct( } + /** + * @param string $author + * @param Circle $circle + * @param Member $member + * @param array $mails + */ + public function generateInvitationMail( + string $author, + Circle $circle, + Member $member, + array $mails, + ): void { + + + if ($member->getUserType() === Member::TYPE_MAIL) { + $mails = [$member->getUserId()]; + } + + if (empty($mails)) { + return; + } + + $circleName = $circle->getDisplayName(); + + $template = $this->generateMailInvitation( + $author, + $circleName + ); + + foreach ($mails as $mail) { + try { + $this->sendMailInvitation($template, $author, $mail, $circleName); + } catch (Exception $e) { + } + } + } + + /** + * @param string $author + * @param string $circleName + * + * @return IEMailTemplate + */ + private function generateMailInvitation( + string $author, + string $circleName + ): IEMailTemplate { + $emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []); + $emailTemplate->addHeader(); + + $text = $this->l10n->t('%s invited you to the circle %s.', [$author, $circleName]); + $emailTemplate->addBodyText(htmlspecialchars($text), $text); + + return $emailTemplate; + } + + + /** + * @param IEMailTemplate $emailTemplate + * @param string $author + * @param string $recipient + * @param string $circleName + * + * @throws Exception + */ + private function sendMailInvitation( + IEMailTemplate $emailTemplate, + string $author, + string $recipient, + string $circleName + ) { + $instanceName = $this->defaults->getName(); + $senderName = $this->l10n->t('%s on %s', [$author, $instanceName]); + $subject = $this->l10n->t('%s invited you to the circle %s.', [$author, $circleName]); + + $message = $this->mailer->createMessage(); + + $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]); + $message->setSubject($subject); + $message->setPlainBody($emailTemplate->renderText()); + $message->setHtmlBody($emailTemplate->renderHtml()); + $message->setTo([$recipient]); + + $this->mailer->send($message); + } + + /** * @param string $author * @param Circle $circle