Skip to content
Open
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
23 changes: 21 additions & 2 deletions app/Bus/Commands/Subscriber/SubscribeSubscriberCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@
*/
final class SubscribeSubscriberCommand
{
/**
* The subscriber name (identifier).
*
* @var string
*/
public $name;

/**
* The subscriber email.
*
* @var string
*/
public $email;

/**
* The url of the Mattermost webhook.
*
* @var string
*/
public $webhook_url;

/**
* The subscriber auto verification.
*
Expand All @@ -45,21 +59,26 @@ final class SubscribeSubscriberCommand
* @var array
*/
public $rules = [
'email' => 'required|email',
'email' => 'nullable|email',
'webhook_url' => 'nullable|url',
];

/**
* Create a new subscribe subscriber command instance.
*
* @param string $name
* @param string $email
* @param string $webhook_url
* @param bool $verified
* @param array|null $subscriptions
*
* @return void
*/
public function __construct($email, $verified = false, $subscriptions = null)
public function __construct($name, $email, $webhook_url, $verified = false, $subscriptions = null)
{
$this->name = $name;
$this->email = $email;
$this->webhook_url = $webhook_url;
$this->verified = $verified;
$this->subscriptions = $subscriptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,44 @@ final class UpdateSubscriberSubscriptionCommand
*/
public $subscriptions;

/**
* If the subscriber wants to subscribe to component status updates.
*
* @var bool
*/
public $to_component_status;

/**
* If the subscriber wants to subscribe to maintenance schedules.
*
* @var bool
*/
public $to_maintenance_schedules;

/**
* If the subscriber wants to subscribe to all incidents (component-indenpendent).
*
* @var bool
*/
public $to_all_incidents;

/**
* Create a new subscribe subscriber command instance.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
* @param null|array $subscriptions
* @param bool $to_component_status
* @param bool $to_maintenance_schedules
* @param bool $to_all_incidents
*
* @return void
*/
public function __construct($subscriber, $subscriptions = null)
public function __construct($subscriber, $subscriptions = null, $to_component_status = false, $to_maintenance_schedules = false, $to_all_incidents=false)
{
$this->subscriber = $subscriber;
$this->subscriptions = $subscriptions;
$this->to_component_status = $to_component_status;
$this->to_maintenance_schedules = $to_maintenance_schedules;
$this->to_all_incidents = $to_all_incidents;
}
}
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;

if ($command->status && (int) $originalStatus !== (int) $command->status) {
if ((int) $originalStatus !== (int) $command->status) { // Notify even if the new status is Unknown
event(new ComponentStatusWasChangedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function handle(UpdateIncidentCommand $command)
// Update the component.
if ($component = Component::find($command->component_id)) {
execute(new UpdateComponentCommand(
Component::find($command->component_id),
$component,
null,
null,
$command->component_status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function handle(CreateIncidentUpdateCommand $command)
$command->status,
null,
null,
null,
null,
$command->component_id,
$command->component_status,
null,
null,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ class SubscribeSubscriberCommandHandler
*
* @param \CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand $command
*
* @return \CachetHQ\Cachet\Models\Subscriber
* @return boolean (true if created else false)
*/
public function handle(SubscribeSubscriberCommand $command)
{
if ($subscriber = Subscriber::where('email', '=', $command->email)->first()) {
return $subscriber;
if (Subscriber::where('name', '=', $command->name)->first()) {
return false;
}

$subscriber = Subscriber::firstOrCreate(['email' => $command->email]);
if ($command->email and Subscriber::where('email', '=', $command->email)->first()) {
return false;
}

$subscriber = Subscriber::create([
'name' => $command->name,
'email' => $command->email,
'mattermost_webhook_url' => $command->webhook_url
]);

// Decide what to subscribe the subscriber to.
if ($subscriptions = $command->subscriptions) {
Expand All @@ -67,6 +75,6 @@ public function handle(SubscribeSubscriberCommand $command)

$subscriber->load('subscriptions');

return $subscriber;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function handle(UpdateSubscriberSubscriptionCommand $command)
return in_array($item->id, $subscriptions);
});

$subscriber->global = ($updateSubscriptions->count() === $components->count());
$subscriber->global = $command->to_all_incidents;
$subscriber->component_status = $command->to_component_status;
$subscriber->maintenance_schedules = $command->to_maintenance_schedules;

$subscriber->subscriptions()->delete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,12 @@ public function handle(ComponentStatusWasChangedEvent $event)
return;
}

// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();

$globalSubscribers->each(function ($subscriber) use ($component, $event) {
$subscriber->notify(new ComponentStatusChangedNotification($component, $event->new_status));
});

$notified = $globalSubscribers->pluck('id')->all();

// Notify the remaining component specific subscribers.
// Notify subscribers subscribed to the component and to status updates.
$componentSubscribers = $this->subscriber
->isVerified()
->isSubscribedToStatus()
->forComponent($component->id)
->get()
->reject(function ($subscriber) use ($notified) {
return in_array($subscriber->id, $notified);
});
->get();

$componentSubscribers->each(function ($subscriber) use ($component, $event) {
$subscriber->notify(new ComponentStatusChangedNotification($component, $event->new_status));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Notifications\Schedule\NewScheduleNotification;

use Illuminate\Support\Facades\Notification;

/**
* This is the send schedule event notification handler.
*
Expand Down Expand Up @@ -55,9 +57,8 @@ public function handle(ScheduleEventInterface $event)
return false;
}

// First notify all global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get()->each(function ($subscriber) use ($schedule) {
$subscriber->notify(new NewScheduleNotification($schedule));
});
// Notify all subscribers subscribed to schedules.
$scheduleSubscribers = $this->subscriber->isVerified()->isSubscribedToSchedules()->get();
Notification::send($scheduleSubscribers, new NewScheduleNotification($schedule));
}
}
46 changes: 46 additions & 0 deletions app/Channels/MattermostWebhookChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace CachetHQ\Cachet\Channels;

use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;

class MattermostWebhookChannel
{
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;

/**
* Create a new Mattermost channel instance.
*
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http)
{
$this->http = $http;
}

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
if (! $url = $notifiable->routeNotificationFor('mattermost', $notification)) {
return;
}

$this->http->post($url, [
'json' => $notification->toMattermost($notifiable),
]);
}
}
6 changes: 1 addition & 5 deletions app/Http/Controllers/Dashboard/IncidentUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ public function createIncidentUpdateAction(Incident $incident)
->withErrors($e->getMessageBag());
}

if ($incident->component) {
$incident->component->update(['status' => Binput::get('component_status')]);
}

return cachet_redirect('dashboard.incidents')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.updates.success')));
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.updates.add.success')));
}

/**
Expand Down
Loading