Don't send notifications when within maintenance window. Fixes #2720

This commit is contained in:
James Brooks 2017-09-14 19:02:34 +01:00 committed by Nico Stapelbroek
parent 8485821491
commit f53f88f950
6 changed files with 79 additions and 4 deletions

View File

@ -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.
*
@ -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;
}
@ -46,6 +55,11 @@ class SendComponentUpdateEmailNotificationHandler
*/
public function handle(ComponentStatusWasChangedEvent $event)
{
// Don't send component status updates if we're under maintenance.
if ($this->system->underMaintenance()) {
return false;
}
$component = $event->component;
// If we're silent, then don't send this.

View File

@ -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.
*
@ -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;
}
@ -45,6 +55,11 @@ class SendIncidentEmailNotificationHandler
*/
public function handle(IncidentWasCreatedEvent $event)
{
// Don't send incident updates if we're under maintenance.
if ($this->system->underMaintenance()) {
return false;
}
$incident = $event->incident;
if (!$event->notify) {

View File

@ -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.
*
@ -27,12 +35,14 @@ class SendIncidentUpdateEmailNotificationHandler
/**
* Create a new send incident email notification handler.
*
* @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;
}
@ -45,6 +55,11 @@ class SendIncidentUpdateEmailNotificationHandler
*/
public function handle(IncidentUpdateWasReportedEvent $event)
{
// Don't send incident updates if we're under maintenance.
if ($this->system->underMaintenance()) {
return false;
}
$update = $event->update;
$incident = $update->incident;

View File

@ -25,6 +25,13 @@ interface System
*/
public function getStatus();
/**
* Determine if Cachet has any open maintenance windows.
*
* @return bool
*/
public function underMaintenance();
/**
* Get the cachet version.
*

View File

@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Integrations\Core;
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;
@ -102,6 +103,16 @@ class System implements SystemContract
return $status;
}
/**
* Determine if Cachet has any open maintenance windows.
*
* @return bool
*/
public function underMaintenance()
{
return Schedule::inProgress()->count() > 0;
}
/**
* Get the cachet version.
*

View File

@ -16,6 +16,7 @@ use CachetHQ\Cachet\Models\Traits\SearchableTrait;
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;
@ -151,6 +152,18 @@ class Schedule extends Model implements HasPresenter
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())->whereNull('completed_at')->where('status', '!=', self::COMPLETE);
}
/**
* Scopes schedules to those in the future.
*