Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send notification mail for circle invitation #1349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
$author = ucfirst($author);

I see in the screen shot that test started with lower-case so, we want to start the statement and name in this case with caps.

$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) {
}
}
Comment on lines +123 to +125
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens during this exception? You probably want to write this on the logs?

}

/**
* @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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know but it could be possible to build all the mail templates and call the send function with an array of messages? (Related to the loop that calls sendMailInvitation on linr 122)

}


/**
* @param string $author
* @param Circle $circle
Expand Down