Skip to content

Commit

Permalink
Merge pull request #2877 from nstapelbroek/feature/2720-suppress-noti…
Browse files Browse the repository at this point in the history
…fications-in-maintenance

Suppress notifications while in maintenance mode
  • Loading branch information
jbrooksuk committed Jan 21, 2018
2 parents 1c91124 + 14e15ca commit 88e9cdf
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
namespace CachetHQ\Cachet\Bus\Handlers\Events\Component;

use CachetHQ\Cachet\Bus\Events\Component\ComponentStatusWasChangedEvent;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Notifications\Component\ComponentStatusChangedNotification;

class SendComponentUpdateEmailNotificationHandler
{
/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;

/**
* The subscriber instance.
*
Expand All @@ -32,8 +40,9 @@ class SendComponentUpdateEmailNotificationHandler
*
* @return void
*/
public function __construct(Subscriber $subscriber)
public function __construct(System $system, Subscriber $subscriber)
{
$this->system = $system;
$this->subscriber = $subscriber;
}

Expand All @@ -48,8 +57,8 @@ public function handle(ComponentStatusWasChangedEvent $event)
{
$component = $event->component;

// If we're silent, then don't send this.
if ($event->silent) {
// If we're silent or the notifications are suppressed don't send this.
if ($event->silent || !$this->system->canNotifySubscribers()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
namespace CachetHQ\Cachet\Bus\Handlers\Events\Incident;

use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasCreatedEvent;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Notifications\Incident\NewIncidentNotification;

class SendIncidentEmailNotificationHandler
{
/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;

/**
* The subscriber instance.
*
Expand All @@ -27,12 +35,14 @@ class SendIncidentEmailNotificationHandler
/**
* Create a new send incident email notification handler.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
* @param \CachetHQ\Cachet\Integrations\Contracts\System $system
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(Subscriber $subscriber)
public function __construct(System $system, Subscriber $subscriber)
{
$this->system = $system;
$this->subscriber = $subscriber;
}

Expand All @@ -47,7 +57,7 @@ public function handle(IncidentWasCreatedEvent $event)
{
$incident = $event->incident;

if (!$event->notify) {
if (!$event->notify || !$this->system->canNotifySubscribers()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
namespace CachetHQ\Cachet\Bus\Handlers\Events\IncidentUpdate;

use CachetHQ\Cachet\Bus\Events\IncidentUpdate\IncidentUpdateWasReportedEvent;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Notifications\IncidentUpdate\IncidentUpdatedNotification;

class SendIncidentUpdateEmailNotificationHandler
{
/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;

/**
* The subscriber instance.
*
Expand All @@ -27,12 +35,14 @@ class SendIncidentUpdateEmailNotificationHandler
/**
* Create a new send incident email notification handler.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
* @param \CachetHQ\Cachet\Integrations\Contracts\System $system
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return void
*/
public function __construct(Subscriber $subscriber)
public function __construct(System $system, Subscriber $subscriber)
{
$this->system = $system;
$this->subscriber = $subscriber;
}

Expand All @@ -48,8 +58,8 @@ public function handle(IncidentUpdateWasReportedEvent $event)
$update = $event->update;
$incident = $update->incident;

// Only send emails for public incidents.
if (!$incident->visible) {
// Only send emails for public incidents while the system is not under scheduled maintenance.
if (!$incident->visible || !$this->system->canNotifySubscribers()) {
return;
}

Expand Down
22 changes: 18 additions & 4 deletions app/Http/Controllers/Dashboard/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Bus\Commands\IncidentUpdate\CreateIncidentUpdateCommand;
use CachetHQ\Cachet\Bus\Commands\IncidentUpdate\UpdateIncidentUpdateCommand;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
Expand Down Expand Up @@ -48,16 +49,24 @@ class IncidentController extends Controller
*/
protected $auth;

/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;

/**
* Creates a new incident controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
*
* @return void
*/
public function __construct(Guard $auth)
public function __construct(Guard $auth, System $system)
{
$this->auth = $auth;
$this->system = $system;

View::share('sub_title', trans('dashboard.incidents.title'));
}
Expand Down Expand Up @@ -87,6 +96,7 @@ public function showAddIncident()
->withPageTitle(trans('dashboard.incidents.add.title').' - '.trans('dashboard.dashboard'))
->withComponentsInGroups(ComponentGroup::with('components')->get())
->withComponentsOutGroups(Component::where('group_id', '=', 0)->get())
->withNotificationsEnabled($this->system->canNotifySubscribers())
->withIncidentTemplates(IncidentTemplate::all());
}

Expand Down Expand Up @@ -225,7 +235,8 @@ public function showEditIncidentAction(Incident $incident)
->withPageTitle(trans('dashboard.incidents.edit.title').' - '.trans('dashboard.dashboard'))
->withIncident($incident)
->withComponentsInGroups(ComponentGroup::with('components')->get())
->withComponentsOutGroups(Component::where('group_id', '=', 0)->get());
->withComponentsOutGroups(Component::where('group_id', '=', 0)->get())
->withNotificationsEnabled($this->system->canNotifySubscribers());
}

/**
Expand Down Expand Up @@ -309,7 +320,9 @@ public function showIncidentUpdates(Incident $incident)
*/
public function showCreateIncidentUpdateAction(Incident $incident)
{
return View::make('dashboard.incidents.updates.add')->withIncident($incident);
return View::make('dashboard.incidents.updates.add')
->withIncident($incident)
->withNotificationsEnabled($this->system->canNotifySubscribers());
}

/**
Expand Down Expand Up @@ -351,7 +364,8 @@ public function showEditIncidentUpdateAction(Incident $incident, IncidentUpdate
{
return View::make('dashboard.incidents.updates.edit')
->withIncident($incident)
->withUpdate($incidentUpdate);
->withUpdate($incidentUpdate)
->withNotificationsEnabled($this->system->canNotifySubscribers());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions app/Integrations/Contracts/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ interface System
*/
public function getStatus();

/**
* Determine if Cachet is allowed to send notifications to users, subscribers or third party tools.
*
* @return bool
*/
public function canNotifySubscribers();

/**
* Get the cachet version.
*
Expand Down
16 changes: 16 additions & 0 deletions app/Integrations/Core/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CachetHQ\Cachet\Integrations\Contracts\System as SystemContract;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Schedule;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Config\Repository;

Expand Down Expand Up @@ -102,6 +103,21 @@ public function getStatus()
return $status;
}

/**
* Determine if Cachet is allowed to send notifications to users, subscribers or third party tools.
*
* @return bool
*/
public function canNotifySubscribers()
{
$maintenancePeriods = Schedule::inProgress()->count();
if ($maintenancePeriods === 0) {
return true;
}

return !$this->config->get('setting.suppress_notifications_in_maintenance');
}

/**
* Get the cachet version.
*
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\SchedulePresenter;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;

Expand Down Expand Up @@ -151,6 +152,20 @@ public function meta()
return $this->morphMany(Meta::class, 'meta');
}

/**
* Scope schedules that are in progress.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeInProgress(Builder $query)
{
return $query->where('scheduled_at', '<=', Carbon::now())->where('status', '!=', self::COMPLETE)->where(function ($query) {
$query->whereNull('completed_at')->orWhere('completed_at', '>', Carbon::now());
});
}

/**
* Scopes schedules to those in the future.
*
Expand Down
12 changes: 12 additions & 0 deletions config/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@

'enable_subscribers' => true,

/*
|--------------------------------------------------------------------------
| Suppress notifications while in maintenance
|--------------------------------------------------------------------------
|
| Whether to suppress notification channels if an issue is created during
| planned or in-progress maintenance periods.
|
*/

'suppress_notifications_in_maintenance' => true,

/*
|--------------------------------------------------------------------------
| Automatic Localization
Expand Down
30 changes: 16 additions & 14 deletions resources/lang/en/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'message-help' => 'You may also use Markdown.',
'occurred_at' => 'When did this incident occur?',
'notify_subscribers' => 'Notify subscribers?',
'notify_disabled' => 'Due to scheduled maintenance, notifications about this incident or its components will be suppressed.',
'visibility' => 'Incident Visibility',
'stick_status' => 'Stick Incident',
'stickied' => 'Stickied',
Expand Down Expand Up @@ -147,20 +148,21 @@
'settings' => [
// Application setup
'app-setup' => [
'site-name' => 'Site Name',
'site-url' => 'Site URL',
'display-graphs' => 'Display graphs on status page?',
'about-this-page' => 'About this page',
'days-of-incidents' => 'How many days of incidents to show?',
'time_before_refresh' => 'Status page refresh rate (in seconds).',
'banner' => 'Banner Image',
'banner-help' => "It's recommended that you upload files no bigger than 930px wide .",
'subscribers' => 'Allow people to signup to email notifications?',
'skip_subscriber_verification' => 'Skip verifying of users? (Be warned, you could be spammed)',
'automatic_localization' => 'Automatically localise your status page to your visitor\'s language?',
'enable_external_dependencies' => 'Enable Third Party Dependencies (Google Fonts, Trackers, etc...)',
'show_timezone' => 'Show the timezone the status page is running in.',
'only_disrupted_days' => 'Only show days containing incidents in the timeline?',
'site-name' => 'Site Name',
'site-url' => 'Site URL',
'display-graphs' => 'Display graphs on status page?',
'about-this-page' => 'About this page',
'days-of-incidents' => 'How many days of incidents to show?',
'time_before_refresh' => 'Status page refresh rate (in seconds).',
'banner' => 'Banner Image',
'banner-help' => "It's recommended that you upload files no bigger than 930px wide .",
'subscribers' => 'Allow people to signup to email notifications?',
'suppress_notifications_in_maintenance' => 'Suppress notifications when incident occurs during maintenance period?',
'skip_subscriber_verification' => 'Skip verifying of users? (Be warned, you could be spammed)',
'automatic_localization' => 'Automatically localise your status page to your visitor\'s language?',
'enable_external_dependencies' => 'Enable Third Party Dependencies (Google Fonts, Trackers, etc...)',
'show_timezone' => 'Show the timezone the status page is running in.',
'only_disrupted_days' => 'Only show days containing incidents in the timeline?',
],
'analytics' => [
'analytics_google' => 'Google Analytics code',
Expand Down
7 changes: 7 additions & 0 deletions resources/views/dashboard/incidents/add.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<div class="content-wrapper">
<div class="row">
<div class="col-md-12">
@if(!$notifications_enabled)
<div class="alert alert-info" role="alert">
{{ trans('forms.incidents.notify_disabled') }}
</div>
@endif
@include('dashboard.partials.errors')
<report-incident inline-template>
<form class="form-vertical" name="IncidentForm" role="form" method="POST" autocomplete="off">
Expand Down Expand Up @@ -115,13 +120,15 @@
<label>{{ trans('forms.incidents.occurred_at') }}</label> <small class="text-muted">{{ trans('forms.optional') }}</small>
<input type="text" name="occurred_at" class="form-control" rel="datepicker-custom" data-date-format="YYYY-MM-DD HH:mm" placeholder="{{ trans('forms.optional') }}">
</div>
@if($notifications_enabled)
<input type="hidden" name="notify" value="0">
<div class="checkbox">
<label>
<input type="checkbox" name="notify" value="1" checked="{{ Binput::old('notify', 'checked') }}">
{{ trans('forms.incidents.notify_subscribers') }}
</label>
</div>
@endif
</fieldset>

<div class="form-group">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/dashboard/incidents/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<div class="content-wrapper">
<div class="row">
<div class="col-md-12">
@if(!$notifications_enabled)
<div class="alert alert-info" role="alert">
{{ trans('forms.incidents.notify_disabled') }}
</div>
@endif
@include('dashboard.partials.errors')
<form class="form-vertical" name="IncidentForm" role="form" method="POST" autocomplete="off">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/dashboard/incidents/updates/add.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<div class="content-wrapper">
<div class="row">
<div class="col-md-12">
@if(!$notifications_enabled)
<div class="alert alert-info" role="alert">
{{ trans('forms.incidents.notify_disabled') }}
</div>
@endif
@include('dashboard.partials.errors')
<form class="form-vertical" name="IncidentUpdateForm" role="form" method="POST" autocomplete="off">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Expand Down
Loading

0 comments on commit 88e9cdf

Please sign in to comment.