Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component notifications can be silenced #2318

Merged
merged 2 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -50,22 +50,31 @@ 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.
*
* @param \CachetHQ\Cachet\Models\User $user
* @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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handle(UpdateComponentCommand $command)
$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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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
13 changes: 10 additions & 3 deletions tests/Bus/Events/Component/ComponentStatusWasUpdatedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testComponentUpdateEmailWasSent()

$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'));
Expand All @@ -58,12 +58,19 @@ protected function objectHasHandlers()

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');
Expand Down