Skip to content

Commit 9116356

Browse files
Jonas HeinrichJonas Heinrich
authored andcommitted
Send notification mail for circle invitation
1 parent 50650d2 commit 9116356

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

lib/Listeners/Notifications/RequestingMember.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
use OCA\Circles\Events\RequestingCircleMemberEvent;
3838
use OCA\Circles\Exceptions\RequestBuilderException;
3939
use OCA\Circles\Service\NotificationService;
40+
use OCA\Circles\Service\SendMailService;
4041
use OCP\EventDispatcher\Event;
4142
use OCP\EventDispatcher\IEventListener;
43+
use OCP\IUserManager;
4244

4345
/**
4446
* Class RequestingMember
@@ -52,12 +54,23 @@ class RequestingMember implements IEventListener {
5254
/** @var NotificationService */
5355
private $notificationService;
5456

57+
/** @var SendMailService */
58+
private $sendMailService;
59+
60+
/** @var IUserManager */
61+
protected $userManager;
5562

5663
/**
5764
* RequestingMember constructor.
5865
*/
59-
public function __construct(NotificationService $notificationService) {
66+
public function __construct(
67+
NotificationService $notificationService,
68+
SendMailService $sendMailService,
69+
IUserManager $userManager
70+
) {
6071
$this->notificationService = $notificationService;
72+
$this->sendMailService = $sendMailService;
73+
$this->userManager = $userManager;
6174

6275
$this->setup('app', Application::APP_ID);
6376
}
@@ -74,11 +87,28 @@ public function handle(Event $event): void {
7487
}
7588

7689
$member = $event->getMember();
90+
$circle = $event->getCircle();
7791

7892
if ($event->getType() === CircleGenericEvent::REQUESTED) {
7993
$this->notificationService->notificationRequested($member);
8094
} else {
8195
$this->notificationService->notificationInvited($member);
96+
97+
if ($member->hasInvitedBy()) {
98+
$author = $member->getInvitedBy()->getDisplayName();
99+
} else {
100+
$author = 'someone';
101+
}
102+
$userId = $member->getUserId();
103+
$user = $this->userManager->get($userId);
104+
$mails = [$user->getEMailAddress()];
105+
106+
$this->sendMailService->generateInvitationMail(
107+
$author,
108+
$circle,
109+
$member,
110+
$mails
111+
);
82112
}
83113
}
84114
}

lib/Service/SendMailService.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,93 @@ public function __construct(
8888
}
8989

9090

91+
/**
92+
* @param string $author
93+
* @param Circle $circle
94+
* @param Member $member
95+
* @param array $mails
96+
*/
97+
public function generateInvitationMail(
98+
string $author,
99+
Circle $circle,
100+
Member $member,
101+
array $mails,
102+
): void {
103+
104+
105+
if ($member->getUserType() === Member::TYPE_MAIL) {
106+
$mails = [$member->getUserId()];
107+
}
108+
109+
if (empty($mails)) {
110+
return;
111+
}
112+
113+
$circleName = $circle->getDisplayName();
114+
115+
$template = $this->generateMailInvitation(
116+
$author,
117+
$circleName
118+
);
119+
120+
foreach ($mails as $mail) {
121+
try {
122+
$this->sendMailInvitation($template, $author, $mail, $circleName);
123+
} catch (Exception $e) {
124+
}
125+
}
126+
}
127+
128+
/**
129+
* @param string $author
130+
* @param string $circleName
131+
*
132+
* @return IEMailTemplate
133+
*/
134+
private function generateMailInvitation(
135+
string $author,
136+
string $circleName
137+
): IEMailTemplate {
138+
$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
139+
$emailTemplate->addHeader();
140+
141+
$text = $this->l10n->t('%s invited you to the circle %s.', [$author, $circleName]);
142+
$emailTemplate->addBodyText(htmlspecialchars($text), $text);
143+
144+
return $emailTemplate;
145+
}
146+
147+
148+
/**
149+
* @param IEMailTemplate $emailTemplate
150+
* @param string $author
151+
* @param string $recipient
152+
* @param string $circleName
153+
*
154+
* @throws Exception
155+
*/
156+
private function sendMailInvitation(
157+
IEMailTemplate $emailTemplate,
158+
string $author,
159+
string $recipient,
160+
string $circleName
161+
) {
162+
$instanceName = $this->defaults->getName();
163+
$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
164+
$subject = $this->l10n->t('%s invited you to the circle %s.', [$author, $circleName]);
165+
166+
$message = $this->mailer->createMessage();
167+
168+
$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
169+
$message->setSubject($subject);
170+
$message->setPlainBody($emailTemplate->renderText());
171+
$message->setHtmlBody($emailTemplate->renderHtml());
172+
$message->setTo([$recipient]);
173+
174+
$this->mailer->send($message);
175+
}
176+
177+
91178
/**
92179
* @param string $author
93180
* @param Circle $circle

0 commit comments

Comments
 (0)