Skip to content

Commit

Permalink
Component notifications can be silenced. Closes #2316
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk committed Jan 16, 2017
1 parent 02fe8d1 commit 8020817
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
12 changes: 11 additions & 1 deletion app/Bus/Commands/Component/UpdateComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -92,6 +99,7 @@ final class UpdateComponentCommand
'group_id' => 'nullable|int',
'enabled' => 'nullable|bool',
'meta' => 'nullable|string',
'silent' => 'nullable|bool',
];

/**
Expand All @@ -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;
Expand All @@ -120,5 +129,6 @@ public function __construct(Component $component, $name, $description, $status,
$this->group_id = $group_id;
$this->enabled = $enabled;
$this->meta = $meta;
$this->silent = $silent;
}
}
11 changes: 10 additions & 1 deletion app/Bus/Events/Component/ComponentStatusWasUpdatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,28 @@ final class ComponentStatusWasUpdatedEvent implements ComponentEventInterface
*/
public $new_status;

/**
* If silent, we won't notify.
*
* @var bool
*/
public $silent;

/**
* Create a new component was updated event instance.
*
* @param \CachetHQ\Cachet\Models\Component $component
* @param int $original_status
* @param int $new_status
* @param bool $silent
*
* @return void
*/
public function __construct(Component $component, $original_status, $new_status)
public function __construct(Component $component, $original_status, $new_status, $silent)
{
$this->component = $component;
$this->original_status = $original_status;
$this->new_status = $new_status;
$this->silent = $silent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle(UpdateComponentCommand $command)
$component = $command->component;
$originalStatus = $component->status;

event(new ComponentStatusWasUpdatedEvent($component, $originalStatus, $command->status));
event(new ComponentStatusWasUpdatedEvent($component, $originalStatus, $command->status, $command->silent));

$component->update($this->filter($command));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public function handle(ReportIncidentCommand $command)
null,
null,
null,
null
null,
false
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function handle(ComponentStatusWasUpdatedEvent $event)
{
$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;
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public function putComponent(Component $component)
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();
Expand Down
4 changes: 3 additions & 1 deletion tests/Bus/Commands/Component/UpdateComponentCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function getObjectAndParams()
'group_id' => 0,
'enabled' => true,
'meta' => null,
'silent' => false,
];

$object = new UpdateComponentCommand(
Expand All @@ -50,7 +51,8 @@ protected function getObjectAndParams()
$params['order'],
$params['group_id'],
$params['enabled'],
$params['meta']
$params['meta'],
$params['silent']
);

return compact('params', 'object');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testComponentUpdateEmailWasSent()

$subscriber->subscriptions()->create(['component_id' => $component->id]);

$this->app['events']->fire(new ComponentStatusWasUpdatedEvent($component, 1, 2));
$this->app['events']->fire(new ComponentStatusWasUpdatedEvent($component, 1, 2, false));

$this->seeMessageFor($subscriber->email);
$this->seeMessageWithSubject(trans('notifications.component.status_update.mail.subject'));
Expand All @@ -55,8 +55,8 @@ protected function objectHasHandlers()

protected function getObjectAndParams()
{
$params = ['component' => new Component(), 'original_status' => 1, 'new_status' => 2];
$object = new ComponentStatusWasUpdatedEvent($params['component'], $params['original_status'], $params['new_status']);
$params = ['component' => new Component(), 'original_status' => 1, 'new_status' => 2, 'silent' => false];
$object = new ComponentStatusWasUpdatedEvent($params['component'], $params['original_status'], $params['new_status'], $params['silent']);

return compact('params', 'object');
}
Expand Down

0 comments on commit 8020817

Please sign in to comment.