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 @@ -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 @@ -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 global subscribers.
$globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();
Notification::send($globalSubscribers, 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
65 changes: 55 additions & 10 deletions app/Http/Controllers/Dashboard/SubscriberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,69 @@

use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeMattermostHookCommand;
use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Models\Subscriber;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
use Illuminate\Support\MessageBag;

class SubscriberController extends Controller
{
/**
* Shows the subscribers view.
* Array of sub-menu items.
*
* @var array
*/
protected $subMenu = [];

/**
* Creates a new subscriber controller instance.
*
* @return void
*/
public function __construct()
{
$this->subMenu = [
'email' => [
'title' => trans('dashboard.subscribers.channel.email.name'),
'url' => cachet_route('dashboard.subscribers'),
'icon' => 'ion ion-ios-email-outline',
'active' => false,
],
'mattermost' => [
'title' => trans('dashboard.subscribers.channel.mattermost.name'),
'url' => cachet_route('dashboard.subscribers.mattermost'),
'icon' => 'ion ion-paper-airplane',
'active' => false,
],
];

View::share([
'subMenu' => $this->subMenu,
'subTitle' => trans('dashboard.subscribers.subscribers'),
]);
}

/**
* Shows the subscribers view (for emails).
*
* @return \Illuminate\View\View
*/
public function showSubscribers()
{
$this->subMenu['email']['active'] = true;

return View::make('dashboard.subscribers.index')
->withPageTitle(trans('dashboard.subscribers.subscribers').' - '.trans('dashboard.dashboard'))
->withSubscribers(Subscriber::with('subscriptions.component')->get());
->withPageTitle(trans('dashboard.subscribers.channel.email.subscribers').' - '.trans('dashboard.dashboard'))
->withSubscribers(Subscriber::whereNotNull('email')->with('subscriptions.component')->get())
->withSubMenu($this->subMenu);
}

/**
* Shows the add subscriber view.
* Shows the add subscriber view (for emails).
*
* @return \Illuminate\View\View
*/
Expand All @@ -46,19 +86,24 @@ public function showAddSubscriber()
}

/**
* Creates a new subscriber.
* Creates a new (email) subscriber.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createSubscriberAction()
{
$verified = app(Repository::class)->get('setting.skip_subscriber_verification');

$subscriberData = Binput::get('subscriber');
try {
$subscribers = preg_split("/\r\n|\n|\r/", Binput::get('email'));

foreach ($subscribers as $subscriber) {
execute(new SubscribeSubscriberCommand($subscriber, $verified));
$created = execute(new SubscribeSubscriberCommand(
$subscriberData['name'], // Name
$subscriberData['email'], // Email
null, // Webhook url
$verified // Verified
));
if (!$created) {
throw new ValidationException(new MessageBag([trans('dashboard.subscribers.add.email_exists')]));
}
} catch (ValidationException $e) {
return cachet_redirect('dashboard.subscribers.create')
Expand All @@ -67,7 +112,7 @@ public function createSubscriberAction()
->withErrors($e->getMessageBag());
}

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

Expand Down
Loading