Integrate Mail, Nexmo and Slack notifications into Cachet

This commit is contained in:
James Brooks
2016-12-30 16:22:05 +00:00
parent 056b80a2bc
commit b8a9f41ae4
43 changed files with 1104 additions and 682 deletions
@@ -1,41 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\System\Mail;
use CachetHQ\Cachet\Models\User;
/**
* This is the test mail command class.
*
* @author James Brooks <james@alt-three.com>
*/
final class TestMailCommand
{
/**
* The user to send the notification to.
*
* @var \CachetHQ\Cachet\Models\User
*/
public $user;
/**
* Create a new test mail command.
*
* @param \CachetHQ\Cachet\Models\User $user
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
}
@@ -22,15 +22,24 @@ final class IncidentWasReportedEvent implements IncidentEventInterface
*/
public $incident;
/**
* Whether to notify that the incident was reported.
*
* @var bool
*/
public $notify;
/**
* Create a new incident has reported event instance.
*
* @param \CachetHQ\Cachet\Models\Incident $incident
* @param bool $notify
*
* @return void
*/
public function __construct(Incident $incident)
public function __construct(Incident $incident, $notify)
{
$this->incident = $incident;
$this->notify = $notify;
}
}
@@ -0,0 +1,50 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Events\User;
use CachetHQ\Cachet\Models\User;
/**
* This is the user accepted invite event class.
*
* @author James Brooks <james@alt-three.com>
*/
final class UserAcceptedInviteEvent implements UserEventInterface
{
/**
* The user that accepted the invite.
*
* @var \CachetHQ\Cachet\Models\User
*/
public $user;
/**
* The invite that the user accepted.
*
* @var \CachetHQ\Cachet\Models\Invite
*/
public $invite;
/**
* Create a new user accepted invite event class.
*
* @param \CachetHQ\Cachet\Models\User $user
* @param \CachetHQ\Cachet\Models\Invite $invite
*
* @return void
*/
public function __construct(User $user, Invite $invite)
{
$this->user = $user;
$this->invite = $invite;
}
}
@@ -105,9 +105,7 @@ class ReportIncidentCommandHandler
));
}
$incident->update(['notify' => (bool) $command->notify]);
event(new IncidentWasReportedEvent($incident));
event(new IncidentWasReportedEvent($incident, (bool) $command->notify));
return $incident;
}
@@ -17,6 +17,7 @@ use CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Models\Subscription;
use CachetHQ\Cachet\Notifications\Subscriber\VerifySubscriptionNotification;
/**
* This is the subscribe subscriber command handler.
@@ -59,6 +60,8 @@ class SubscribeSubscriberCommandHandler
if ($command->verified) {
dispatch(new VerifySubscriberCommand($subscriber));
} else {
$subscriber->notify(new VerifySubscriptionNotification());
event(new SubscriberHasSubscribedEvent($subscriber));
}
@@ -1,64 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\System\Mail;
use CachetHQ\Cachet\Bus\Commands\System\Mail\TestMailCommand;
use Illuminate\Contracts\Mail\MailQueue;
/**
* This is the test mail command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class TestMailCommandHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $mailer;
/**
* Create a test mail command handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
*
* @return void
*/
public function __construct(MailQueue $mailer)
{
$this->mailer = $mailer;
}
/**
* Handle the test mail command.
*
* @param \CachetHQ\Cachet\Bus\Commands\System\Mail\TestMailCommand $command
*
* @return void
*/
public function handle(TestMailCommand $command)
{
$mail = [
'email' => $command->user->email,
'subject' => trans('dashboard.settings.mail.email.subject'),
];
$this->mailer->queue([
'html' => 'emails.system.test-html',
'text' => 'emails.system.test-text',
], $mail, function ($message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
@@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Bus\Handlers\Commands\User;
use CachetHQ\Cachet\Bus\Commands\User\InviteUserCommand;
use CachetHQ\Cachet\Bus\Events\User\UserWasInvitedEvent;
use CachetHQ\Cachet\Models\Invite;
use CachetHQ\Cachet\Notifications\User\InviteUserNotification;
/**
* This is the invite user command handler.
@@ -36,6 +37,8 @@ class InviteUserCommandHandler
'email' => $email,
]);
$invite->notify(new InviteUserNotification());
event(new UserWasInvitedEvent($invite));
}
}
@@ -14,18 +14,10 @@ namespace CachetHQ\Cachet\Bus\Handlers\Events\Component;
use CachetHQ\Cachet\Bus\Events\Component\ComponentStatusWasUpdatedEvent;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
use CachetHQ\Cachet\Notifications\Component\ComponentStatusChangedNotification;
class SendComponentUpdateEmailNotificationHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $mailer;
/**
* The subscriber instance.
*
@@ -36,14 +28,12 @@ class SendComponentUpdateEmailNotificationHandler
/**
* Create a new send incident email notification handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(MailQueue $mailer, Subscriber $subscriber)
public function __construct(Subscriber $subscriber)
{
$this->mailer = $mailer;
$this->subscriber = $subscriber;
}
@@ -66,9 +56,9 @@ class SendComponentUpdateEmailNotificationHandler
// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();
foreach ($globalSubscribers as $subscriber) {
$this->notify($component, $subscriber);
}
$globalSubscribers->map(function ($subscriber) use ($component, $event) {
$subscriber->notify(new ComponentStatusChangedNotification($component, $event->new_status));
});
$notified = $globalSubscribers->pluck('id')->all();
@@ -81,37 +71,8 @@ class SendComponentUpdateEmailNotificationHandler
return in_array($subscriber->id, $notified);
});
foreach ($componentSubscribers as $subscriber) {
$this->notify($component, $subscriber);
}
}
/**
* Send notification to subscriber.
*
* @param \CachetHQ\Cachet\Models\Component $component
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function notify(Component $component, Subscriber $subscriber)
{
$component = AutoPresenter::decorate($component);
$mail = [
'subject' => trans('cachet.subscriber.email.component.subject'),
'component_name' => $component->name,
'component_human_status' => $component->human_status,
];
$mail['email'] = $subscriber->email;
$mail['manage_link'] = cachet_route('subscribe.manage', [$subscriber->verify_code]);
$this->mailer->queue([
'html' => 'emails.components.update-html',
'text' => 'emails.components.update-text',
], $mail, function ($message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
$componentSubscribers->map(function ($subscriber) use ($component, $event) {
$subscriber->notify(new ComponentStatusChangedNotification($component, $event->new_status));
});
}
}
@@ -13,19 +13,10 @@ namespace CachetHQ\Cachet\Bus\Handlers\Events\Incident;
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasReportedEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
use CachetHQ\Cachet\Notifications\Incident\NewIncidentNotification;
class SendIncidentEmailNotificationHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $mailer;
/**
* The subscriber instance.
*
@@ -36,14 +27,12 @@ class SendIncidentEmailNotificationHandler
/**
* Create a new send incident email notification handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(MailQueue $mailer, Subscriber $subscriber)
public function __construct(Subscriber $subscriber)
{
$this->mailer = $mailer;
$this->subscriber = $subscriber;
}
@@ -56,23 +45,25 @@ class SendIncidentEmailNotificationHandler
*/
public function handle(IncidentWasReportedEvent $event)
{
if (!$event->incident->notify) {
$incident = $event->incident;
if (!$event->notify) {
return false;
}
// Only send emails for public incidents.
if ($event->incident->visible === 0) {
if (!$incident->visible) {
return;
}
// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();
foreach ($globalSubscribers as $subscriber) {
$this->notify($event, $subscriber);
}
$globalSubscribers->map(function ($subscriber) use ($incident) {
$subscriber->notify(new NewIncidentNotification($incident));
});
if (!$event->incident->component) {
if (!$incident->component) {
return;
}
@@ -81,53 +72,12 @@ class SendIncidentEmailNotificationHandler
// Notify the remaining component specific subscribers.
$componentSubscribers = $this->subscriber
->isVerified()
->forComponent($event->incident->component->id)
->forComponent($incident->component->id)
->get()
->reject(function ($subscriber) use ($notified) {
return in_array($subscriber->id, $notified);
})->map(function ($subscriber) use ($incident) {
$subscriber->notify(new NewIncidentNotification($incident));
});
foreach ($componentSubscribers as $subscriber) {
$this->notify($event, $subscriber);
}
}
/**
* Send notification to subscriber.
*
* @param \CachetHQ\Cachet\Bus\Events\IncidentWasReportedEvent $event
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function notify(IncidentWasReportedEvent $event, $subscriber)
{
$incident = AutoPresenter::decorate($event->incident);
$component = AutoPresenter::decorate($event->incident->component);
$mail = [
'email' => $subscriber->email,
'subject' => trans('cachet.subscriber.email.incident.subject', [
'status' => $incident->human_status,
'name' => $incident->name,
]),
'has_component' => ($event->incident->component) ? true : false,
'component_name' => $component ? $component->name : null,
'name' => $incident->name,
'timestamp' => $incident->occurred_at_formatted,
'status' => $incident->human_status,
'html_content' => $incident->formatted_message,
'text_content' => $incident->message,
'token' => $subscriber->token,
'manage_link' => cachet_route('subscribe.manage', [$subscriber->verify_code]),
'unsubscribe_link' => cachet_route('subscribe.unsubscribe', [$subscriber->verify_code]),
];
$this->mailer->queue([
'html' => 'emails.incidents.new-html',
'text' => 'emails.incidents.new-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
@@ -0,0 +1,80 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Events\IncidentUpdate;
use CachetHQ\Cachet\Bus\Events\IncidentUpdate\IncidentUpdateWasReportedEvent;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Notifications\IncidentUpdate\IncidentUpdatedNotification;
class SendIncidentUpdateEmailNotificationHandler
{
/**
* The subscriber instance.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
protected $subscriber;
/**
* Create a new send incident email notification handler.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(Subscriber $subscriber)
{
$this->subscriber = $subscriber;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Bus\Events\IncidentUpdate\IncidentUpdateWasReportedEvent $event
*
* @return void
*/
public function handle(IncidentUpdateWasReportedEvent $event)
{
$update = $event->update;
$incident = $update->incident;
// Only send emails for public incidents.
if (!$incident->visible) {
return;
}
// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();
$globalSubscribers->map(function ($subscriber) use ($update) {
$subscriber->notify(new IncidentUpdatedNotification($update));
});
if (!$incident->component) {
return;
}
$notified = $globalSubscribers->pluck('id')->all();
// Notify the remaining component specific subscribers.
$componentSubscribers = $this->subscriber
->isVerified()
->forComponent($incident->component->id)
->get()
->reject(function ($subscriber) use ($notified) {
return in_array($subscriber->id, $notified);
})->map(function ($subscriber) use ($incident) {
$subscriber->notify(new IncidentUpdatedNotification($incident));
});
}
}
@@ -13,9 +13,7 @@ namespace CachetHQ\Cachet\Bus\Handlers\Events\Schedule;
use CachetHQ\Cachet\Bus\Events\Schedule\ScheduleEventInterface;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
use CachetHQ\Cachet\Notifications\Schedule\NewScheduleNotification;
/**
* This is the send schedule event notification handler.
@@ -24,13 +22,6 @@ use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
*/
class SendScheduleEmailNotificationHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\MailQueue
*/
protected $mailer;
/**
* The subscriber instance.
*
@@ -39,16 +30,14 @@ class SendScheduleEmailNotificationHandler
protected $subscriber;
/**
* Create a new send maintenance email notification handler.
* Create a new send schedule email notification handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(MailQueue $mailer, Subscriber $subscriber)
public function __construct(Subscriber $subscriber)
{
$this->mailer = $mailer;
$this->subscriber = $subscriber;
}
@@ -61,62 +50,11 @@ class SendScheduleEmailNotificationHandler
*/
public function handle(ScheduleEventInterface $event)
{
$schedule = $event->schedule;
// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();
foreach ($globalSubscribers as $subscriber) {
$this->notify($event, $subscriber);
}
$notified = $globalSubscribers->pluck('id')->all();
// Notify the remaining component specific subscribers.
$componentSubscribers = $this->subscriber
->isVerified()
->forComponent($event->incident->component->id)
->get()
->reject(function ($subscriber) use ($notified) {
return in_array($subscriber->id, $notified);
});
foreach ($componentSubscribers as $subscriber) {
$this->notify($event, $subscriber);
}
}
/**
* Send notification to subscriber.
*
* @param \CachetHQ\Cachet\Bus\Events\Schedule\ScheduleEventInterface $event
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function notify(ScheduleEventInterface $event, $subscriber)
{
$incident = AutoPresenter::decorate($event->incident);
$component = AutoPresenter::decorate($event->incident->component);
$mail = [
'email' => $subscriber->email,
'subject' => trans('cachet.subscriber.email.maintenance.subject', [
'name' => $incident->name,
]),
'name' => $incident->name,
'timestamp' => $incident->scheduled_at_formatted,
'status' => $incident->human_status,
'html_content' => $incident->formatted_message,
'text_content' => $incident->message,
'token' => $subscriber->token,
'manage_link' => cachet_route('subscribe.manage', [$subscriber->verify_code]),
'unsubscribe_link' => cachet_route('subscribe.unsubscribe', [$subscriber->verify_code]),
];
$this->mailer->queue([
'html' => 'emails.incidents.maintenance-html',
'text' => 'emails.incidents.maintenance-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get()->map(function ($subscriber) use ($schedule) {
$subscriber->notify(new NewScheduleNotification($schedule));
});
}
}
@@ -1,61 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Events\Subscriber;
use CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
class SendSubscriberVerificationEmailHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\MailQueue
*/
protected $mailer;
/**
* Create a new send subscriber verification email handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
*
* @return void
*/
public function __construct(MailQueue $mailer)
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Bus\Events\SubscriberHasSubscribedEvent $event
*
* @return void
*/
public function handle(SubscriberHasSubscribedEvent $event)
{
$mail = [
'email' => $event->subscriber->email,
'subject' => 'Confirm your subscription.',
'link' => cachet_route('subscribe.verify', ['code' => $event->subscriber->verify_code]),
];
$this->mailer->queue([
'html' => 'emails.subscribers.verify-html',
'text' => 'emails.subscribers.verify-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
@@ -1,61 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Events\User;
use CachetHQ\Cachet\Bus\Events\User\UserWasInvitedEvent;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
class SendInviteUserEmailHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\MailQueue
*/
protected $mailer;
/**
* Create a new send invite user email handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
*
* @return void
*/
public function __construct(MailQueue $mailer)
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Bus\Events\UserWasInvitedEvent $event
*
* @return void
*/
public function handle(UserWasInvitedEvent $event)
{
$mail = [
'email' => $event->invite->email,
'subject' => 'You have been invited.',
'link' => cachet_route('signup.invite', [$event->invite->code]),
];
$this->mailer->queue([
'html' => 'emails.users.invite-html',
'text' => 'emails.users.invite-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}