From 8725ab2b9b57a1946b163cc4d1c9ad1567548b71 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 16 Jan 2017 20:48:00 +0000 Subject: [PATCH 1/2] Component notifications can be silenced. Closes #2316 --- .../Commands/Component/UpdateComponentCommand.php | 12 +++++++++++- .../Component/ComponentStatusWasUpdatedEvent.php | 11 ++++++++++- .../Component/UpdateComponentCommandHandler.php | 2 +- .../Incident/ReportIncidentCommandHandler.php | 3 ++- .../SendComponentUpdateEmailNotificationHandler.php | 5 +++++ app/Http/Controllers/Api/ComponentController.php | 3 ++- .../Component/UpdateComponentCommandTest.php | 4 +++- .../ComponentStatusWasUpdatedEventTest.php | 13 ++++++++++--- 8 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/Bus/Commands/Component/UpdateComponentCommand.php b/app/Bus/Commands/Component/UpdateComponentCommand.php index 7905d7cd..6da4e02c 100644 --- a/app/Bus/Commands/Component/UpdateComponentCommand.php +++ b/app/Bus/Commands/Component/UpdateComponentCommand.php @@ -78,6 +78,13 @@ final class UpdateComponentCommand */ public $meta; + /** + * If this is true, we won't notify subscribers of the change. + * + * @var bool + */ + public $silent; + /** * The validation rules. * @@ -92,6 +99,7 @@ final class UpdateComponentCommand 'group_id' => 'nullable|int', 'enabled' => 'nullable|bool', 'meta' => 'nullable|string', + 'silent' => 'nullable|bool', ]; /** @@ -106,10 +114,11 @@ final class UpdateComponentCommand * @param int $group_id * @param bool $enabled * @param string|null $meta + * @param bool $silent * * @return void */ - public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled, $meta) + public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled, $meta, $silent) { $this->component = $component; $this->name = $name; @@ -120,5 +129,6 @@ final class UpdateComponentCommand $this->group_id = $group_id; $this->enabled = $enabled; $this->meta = $meta; + $this->silent = $silent; } } diff --git a/app/Bus/Events/Component/ComponentStatusWasUpdatedEvent.php b/app/Bus/Events/Component/ComponentStatusWasUpdatedEvent.php index e54ee375..14b87777 100644 --- a/app/Bus/Events/Component/ComponentStatusWasUpdatedEvent.php +++ b/app/Bus/Events/Component/ComponentStatusWasUpdatedEvent.php @@ -50,6 +50,13 @@ final class ComponentStatusWasUpdatedEvent implements ActionInterface, Component */ public $new_status; + /** + * If silent, we won't notify. + * + * @var bool + */ + public $silent; + /** * Create a new component was updated event instance. * @@ -57,15 +64,17 @@ final class ComponentStatusWasUpdatedEvent implements ActionInterface, Component * @param \CachetHQ\Cachet\Models\Component $component * @param int $original_status * @param int $new_status + * @param bool $silent * * @return void */ - public function __construct(User $user, Component $component, $original_status, $new_status) + public function __construct(User $user, Component $component, $original_status, $new_status, $silent) { $this->user = $user; $this->component = $component; $this->original_status = $original_status; $this->new_status = $new_status; + $this->silent = $silent; } /** diff --git a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php index 27e6cbb3..17d37165 100644 --- a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -50,7 +50,7 @@ class UpdateComponentCommandHandler $component = $command->component; $originalStatus = $component->status; - event(new ComponentStatusWasUpdatedEvent($this->auth->user(), $component, $originalStatus, $command->status)); + event(new ComponentStatusWasUpdatedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent)); $component->update($this->filter($command)); diff --git a/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index 445b295b..85fcd648 100644 --- a/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -111,7 +111,8 @@ class ReportIncidentCommandHandler null, null, null, - null + null, + false )); } diff --git a/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php b/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php index aed7ce1a..6c7de880 100644 --- a/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php +++ b/app/Bus/Handlers/Events/Component/SendComponentUpdateEmailNotificationHandler.php @@ -48,6 +48,11 @@ class SendComponentUpdateEmailNotificationHandler { $component = $event->component; + // If we're silent, then don't send this. + if ($event->silent) { + return; + } + // Don't email anything if the status hasn't changed. if ($event->original_status === $event->new_status) { return; diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 2692965f..cfb91730 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -120,7 +120,8 @@ class ComponentController extends AbstractApiController Binput::get('order'), Binput::get('group_id'), (bool) Binput::get('enabled', true), - Binput::get('meta', null) + Binput::get('meta', null), + (bool) Binput::get('silent', false) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/tests/Bus/Commands/Component/UpdateComponentCommandTest.php b/tests/Bus/Commands/Component/UpdateComponentCommandTest.php index 9d7bd3ad..59f808be 100644 --- a/tests/Bus/Commands/Component/UpdateComponentCommandTest.php +++ b/tests/Bus/Commands/Component/UpdateComponentCommandTest.php @@ -39,6 +39,7 @@ class UpdateComponentCommandTest extends AbstractTestCase 'group_id' => 0, 'enabled' => true, 'meta' => null, + 'silent' => false, ]; $object = new UpdateComponentCommand( @@ -50,7 +51,8 @@ class UpdateComponentCommandTest extends AbstractTestCase $params['order'], $params['group_id'], $params['enabled'], - $params['meta'] + $params['meta'], + $params['silent'] ); return compact('params', 'object'); diff --git a/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php b/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php index e59bcfce..d9d1d4c0 100644 --- a/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php +++ b/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php @@ -40,7 +40,7 @@ class ComponentStatusWasUpdatedEventTest extends AbstractComponentEventTestCase $subscriber->subscriptions()->create(['component_id' => $component->id]); - $this->app['events']->fire(new ComponentStatusWasUpdatedEvent($user, $component, 1, 2)); + $this->app['events']->fire(new ComponentStatusWasUpdatedEvent($user, $component, 1, 2, false)); $this->seeMessageFor($subscriber->email); $this->seeMessageWithSubject(trans('notifications.component.status_update.mail.subject')); @@ -58,12 +58,19 @@ class ComponentStatusWasUpdatedEventTest extends AbstractComponentEventTestCase protected function getObjectAndParams() { - $params = ['user' => new User(), 'component' => new Component(), 'original_status' => 1, 'new_status' => 2]; + $params = [ + 'user' => new User(), + 'component' => new Component(), + 'original_status' => 1, + 'new_status' => 2, + 'silent' => false + ]; $object = new ComponentStatusWasUpdatedEvent( $params['user'], $params['component'], $params['original_status'], - $params['new_status'] + $params['new_status'], + $params['silent'] ); return compact('params', 'object'); From c9423604caff5553725a6349a2e0785e05ae1bc6 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 6 Feb 2017 18:59:18 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI [ci skip] [skip ci] --- .../Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php b/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php index d9d1d4c0..6417ba8c 100644 --- a/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php +++ b/tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php @@ -63,7 +63,7 @@ class ComponentStatusWasUpdatedEventTest extends AbstractComponentEventTestCase 'component' => new Component(), 'original_status' => 1, 'new_status' => 2, - 'silent' => false + 'silent' => false, ]; $object = new ComponentStatusWasUpdatedEvent( $params['user'],