Skip to content

Commit

Permalink
Send notification mail for circle invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Heinrich authored and Jonas Heinrich committed Jul 25, 2023
1 parent 50650d2 commit 9116356
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/Listeners/Notifications/RequestingMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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
);
}
}
}
87 changes: 87 additions & 0 deletions lib/Service/SendMailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9116356

Please sign in to comment.