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

View File

@@ -0,0 +1,156 @@
<?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\Notifications\Component;
use CachetHQ\Cachet\Models\Component;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
/**
* This is the component status changed notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class ComponentStatusChangedNotification extends Notification
{
use Queueable;
/**
* The component that changed.
*
* @var \CachetHQ\Cachet\Models\Component
*/
protected $component;
/**
* The component status we're now at.
*
* @var int
*/
protected $status;
/**
* Create a new notification instance.
*
* @param \CachetHQ\Cachet\Models\Component $component
* @param int $status
*
* @return void
*/
public function __construct(Component $component, $status)
{
$this->component = AutoPresenter::decorate($component);
$this->status = $status;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail', 'nexmo', 'slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$content = trans('notifications.component.status_update.content', [
'name' => $this->component->name,
'old_status' => $this->component->human_status,
'new_status' => trans("cachet.components.status.{$this->status}"),
]);
if ($this->status <= 1) {
$status = 'success';
} else {
$status = 'error';
}
return (new MailMessage())
->subject(trans('notifications.component.status_update.subject'))
->$status()
->greeting(trans('notifications.component.status_update.title'))
->line($content)
->action('View Component', $this->component->link);
}
/**
* Get the Nexmo / SMS representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\NexmoMessage
*/
public function toNexmo($notifiable)
{
$content = trans('notifications.component.status_update.content', [
'name' => $this->component->name,
'old_status' => $this->component->human_status,
'new_status' => trans("cachet.components.status.{$this->status}"),
]);
return (new NexmoMessage())->content($content);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\SlackMessage
*/
public function toSlack($notifiable)
{
$content = trans('notifications.component.status_update.content', [
'name' => $this->component->name,
'old_status' => $this->component->human_status,
'new_status' => trans("cachet.components.status.{$this->status}"),
]);
$status = 'info';
if ($this->status <= 1) {
$status = 'success';
} elseif ($this->status === 2) {
$status = 'warning';
} elseif ($this->status >= 3) {
$status = 'error';
}
return (new SlackMessage())
->$status()
->content(trans('notifications.component.status_update.title'))
->attachment(function ($attachment) use ($content) {
$attachment->title($content, cachet_route('status-page'))
->fields(array_filter([
'Component' => $this->component->name,
'Old Status' => $this->component->human_status,
'New Status' => trans("cachet.components.status.{$this->status}"),
'Link' => $this->component->link,
]));
});
}
}

View File

@@ -0,0 +1,141 @@
<?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\Notifications\Incident;
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
/**
* This is the new incident notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class NewIncidentNotification extends Notification
{
use Queueable;
/**
* The incident.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
protected $incident;
/**
* Create a new notification instance.
*
* @param \CachetHQ\Cachet\Models\Incident $incident
*
* @return void
*/
public function __construct(Incident $incident)
{
$this->incident = AutoPresenter::decorate($incident);
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail', 'nexmo', 'slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$content = trans('notifications.incident.new.content', [
'name' => $this->incident->name,
]);
if ($this->incident->status === Incident::FIXED) {
$status = 'success';
} else {
$status = 'error';
}
return (new MailMessage())
->subject(trans('notifications.incident.new.subject'))
->$status()
->greeting(trans('notifications.incident.new.title', ['app_name' => Config::get('setting.app_name')]))
->line($content)
->action('View Component', $this->incident->link);
}
/**
* Get the Nexmo / SMS representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\NexmoMessage
*/
public function toNexmo($notifiable)
{
$content = trans('notifications.incident.new.content', [
'name' => $this->incident->name,
]);
return (new NexmoMessage())->content($content);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\SlackMessage
*/
public function toSlack($notifiable)
{
$content = trans('notifications.incident.new.content', [
'name' => $this->incident->name,
]);
$status = 'info';
if ($this->incident->status === Incident::FIXED) {
$status = 'success';
} elseif ($this->incident->status === Incident::WATCHED) {
$status = 'warning';
} else {
$status = 'error';
}
return (new SlackMessage())
->$status()
->content(trans('notifications.incident.new.title', ['app_name' => Config::get('setting.app_name')]))
->attachment(function ($attachment) use ($content) {
$attachment->title($content)
->timestamp($this->incident->getWrappedObject()->occurred_at)
->fields(array_filter([
'ID' => "#{$this->incident->id}",
'Link' => $this->incident->permalink,
]));
});
}
}

View File

@@ -0,0 +1,149 @@
<?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\Notifications\IncidentUpdate;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentUpdate;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
/**
* This is the incident updated notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class IncidentUpdatedNotification extends Notification
{
use Queueable;
/**
* The incident update.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
protected $update;
/**
* Create a new notification instance.
*
* @param \CachetHQ\Cachet\Models\IncidentUpdate $update
*
* @return void
*/
public function __construct(IncidentUpdate $update)
{
$this->update = AutoPresenter::decorate($update);
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail', 'nexmo', 'slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$content = trans('notifications.incident.update.content', [
'name' => $this->update->name,
'time' => $this->update->created_at_diff,
]);
if ($this->update->status === Incident::FIXED) {
$status = 'success';
} else {
$status = 'error';
}
return (new MailMessage())
->subject(trans('notifications.incident.update.subject'))
->$status()
->greeting(trans('notifications.incident.update.title', [
'name' => $this->update->incident->name,
'new_status' => $this->update->human_status,
]))
->line($content)
->action('View Component', $this->update->link);
}
/**
* Get the Nexmo / SMS representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\NexmoMessage
*/
public function toNexmo($notifiable)
{
$content = trans('notifications.incident.update.content', [
'name' => $this->update->incident->name,
]);
return (new NexmoMessage())->content($content);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\SlackMessage
*/
public function toSlack($notifiable)
{
$content = trans('notifications.incident.update.content', [
'name' => $this->update->incident->name,
]);
$status = 'info';
if ($this->update->status === Incident::FIXED) {
$status = 'success';
} elseif ($this->update->status === Incident::WATCHED) {
$status = 'warning';
} else {
$status = 'error';
}
return (new SlackMessage())
->$status()
->content(trans('notifications.incident.update.title', [
'name' => $this->update->incident->name,
'new_status' => $this->update->human_status,
]))
->attachment(function ($attachment) use ($content) {
$attachment->title($content)
->timestamp($this->update->getWrappedObject()->created_at)
->fields(array_filter([
'ID' => "#{$this->update->id}",
'Link' => $this->update->permalink,
]));
});
}
}

View File

@@ -0,0 +1,125 @@
<?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\Notifications\Schedule;
use CachetHQ\Cachet\Models\Schedule;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
/**
* This is the new schedule notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class NewScheduleNotification extends Notification
{
use Queueable;
/**
* The schedule.
*
* @var \CachetHQ\Cachet\Models\Schedule
*/
protected $schedule;
/**
* Create a new notification instance.
*
* @param \CachetHQ\Cachet\Models\Schedule $schedule
*
* @return void
*/
public function __construct(Schedule $schedule)
{
$this->schedule = AutoPresenter::decorate($schedule);
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail', 'nexmo', 'slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$content = trans('notifications.schedule.new.content', [
'name' => $this->schedule->name,
'date' => $this->schedule->scheduled_at_formatted,
]);
return (new MailMessage())
->subject(trans('notifications.schedule.new.subject'))
->greeting(trans('notifications.schedule.new.title'))
->line($content)
->action('View Component', $this->schedule->link);
}
/**
* Get the Nexmo / SMS representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\NexmoMessage
*/
public function toNexmo($notifiable)
{
$content = trans('notifications.schedule.new.content', [
'name' => $this->schedule->name,
'date' => $this->schedule->scheduled_at_formatted,
]);
return (new NexmoMessage())->content($content);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\SlackMessage
*/
public function toSlack($notifiable)
{
$content = trans('notifications.schedule.new.content', [
'name' => $this->schedule->name,
'date' => $this->schedule->scheduled_at_formatted,
]);
return (new SlackMessage())
->content(trans('notifications.schedule.new.title'))
->attachment(function ($attachment) use ($content) {
$attachment->title($content)
->timestamp($this->schedule->getWrappedObject()->scheduled_at)
->fields(array_filter([
'ID' => "#{$this->schedule->id}",
'Status' => $this->schedule->human_status,
]));
});
}
}

View File

@@ -0,0 +1,55 @@
<?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\Notifications\Subscriber;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
/**
* This is the verify subscription notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class VerifySubscriptionNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage())
->subject(trans('notifications.subscriber.verify.subject'))
->greeting(trans('notifications.subscriber.verify.title', ['app_name' => Config::get('setting.app_name')]))
->action(trans('notifications.subscriber.verify.action'), cachet_route('subscribe.verify', ['code' => $notifiable->verify_code]))
->line(trans('notifications.subscriber.verify.content', ['app_name' => Config::get('setting.app_name')]));
}
}

View File

@@ -0,0 +1,53 @@
<?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\Notifications\System;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
/**
* This is the system test notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class SystemTestNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage())
->subject(trans('notifications.system.test.subject'))
->greeting(trans('notifications.system.test.title'))
->line(trans('notifications.system.test.content'));
}
}

View File

@@ -0,0 +1,55 @@
<?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\Notifications\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
/**
* This is the invite user notification class.
*
* @author James Brooks <james@alt-three.com>
*/
class InviteUserNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return string[]
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage())
->subject(trans('notifications.user.invite.subject'))
->greeting(trans('notifications.user.invite.title', ['app_name' => Config::get('setting.app_name')]))
->action(trans('notifications.user.invite.action'), cachet_route('signup.invite', [$notifiable->code]))
->line(trans('notifications.user.invite.content', ['app_name' => Config::get('setting.app_name')]));
}
}