From f53f88f9509d6e0b2bfe47d61ee5ca6705f3a77d Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 14 Sep 2017 19:02:34 +0100 Subject: [PATCH] Don't send notifications when within maintenance window. Fixes #2720 --- ...omponentUpdateEmailNotificationHandler.php | 16 +++++++++++++++- .../SendIncidentEmailNotificationHandler.php | 19 +++++++++++++++++-- ...IncidentUpdateEmailNotificationHandler.php | 17 ++++++++++++++++- app/Integrations/Contracts/System.php | 7 +++++++ app/Integrations/Core/System.php | 11 +++++++++++ app/Models/Schedule.php | 13 +++++++++++++ 6 files changed, 79 insertions(+), 4 deletions(-) diff --git a/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php b/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php index 33c77fb29..00435bb27 100644 --- a/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php +++ b/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php @@ -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. diff --git a/app/Bus/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Bus/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index b85b8fc86..bd7f83ff6 100644 --- a/app/Bus/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Bus/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -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) { diff --git a/app/Bus/Handlers/Events/IncidentUpdate/SendIncidentUpdateEmailNotificationHandler.php b/app/Bus/Handlers/Events/IncidentUpdate/SendIncidentUpdateEmailNotificationHandler.php index 7b3922007..0b8f9fa63 100644 --- a/app/Bus/Handlers/Events/IncidentUpdate/SendIncidentUpdateEmailNotificationHandler.php +++ b/app/Bus/Handlers/Events/IncidentUpdate/SendIncidentUpdateEmailNotificationHandler.php @@ -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; diff --git a/app/Integrations/Contracts/System.php b/app/Integrations/Contracts/System.php index 385ad5015..e023e4f49 100644 --- a/app/Integrations/Contracts/System.php +++ b/app/Integrations/Contracts/System.php @@ -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. * diff --git a/app/Integrations/Core/System.php b/app/Integrations/Core/System.php index ccf5bb0cd..9282a0bbf 100644 --- a/app/Integrations/Core/System.php +++ b/app/Integrations/Core/System.php @@ -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. * diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php index 46b573891..f5d1fd77f 100644 --- a/app/Models/Schedule.php +++ b/app/Models/Schedule.php @@ -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. *