Merge pull request #662 from cachethq/subscribers

Subscribers
This commit is contained in:
James Brooks
2015-06-10 14:06:15 +01:00
48 changed files with 1214 additions and 35 deletions
+1 -1
View File
@@ -32,6 +32,6 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
//
$schedule->command('queue:work --sleep=3 --tries=3')->everyMinute();
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events;
use CachetHQ\Cachet\Models\Subscriber;
class CustomerHasSubscribedEvent
{
/**
* The customer who has subscribed.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
public $subscriber;
/**
* Create a new customer has subscribed event instance.
*
* @return void
*/
public function __construct(Subscriber $subscriber)
{
$this->subscriber = $subscriber;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events;
use CachetHQ\Cachet\Models\Incident;
class IncidentHasReportedEvent
{
/**
* The incident that has been reported.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
public $incident;
/**
* Create a new incident has reported event instance.
*
* @return void
*/
public function __construct(Incident $incident)
{
$this->incident = $incident;
}
}
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events;
use CachetHQ\Cachet\Models\Incident;
class MaintenanceHasScheduledEvent
{
/**
* The incident that has been reported.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
public $incident;
/**
* Create a new maintenance has scheduled event instance.
*
* @return void
*/
public function __construct(Incident $incident)
{
$this->incident = $incident;
}
}
View File
View File
@@ -0,0 +1,88 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Events;
use CachetHQ\Cachet\Events\IncidentHasReportedEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
use McCool\LaravelAutoPresenter\PresenterDecorator;
class SendIncidentEmailNotificationHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $mailer;
/**
* The subscriber instance.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
protected $subscriber;
/**
* The presenter instance.
*
* @var \McCool\LaravelAutoPresenter\PresenterDecorator
*/
protected $presenter;
/**
* Create a new send incident email notification handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
* @param \McCool\LaravelAutoPresenter\PresenterDecorator $presenter
*
* @return void
*/
public function __construct(MailQueue $mailer, Subscriber $subscriber, PresenterDecorator $presenter)
{
$this->mailer = $mailer;
$this->subscriber = $subscriber;
$this->presenter = $presenter;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Events\IncidentHasReportedEvent $event
*
* @return void
*/
public function handle(IncidentHasReportedEvent $event)
{
$data = $this->presenter->decorate($event->incident);
foreach ($this->subscriber->all() as $subscriber) {
$mail = [
'email' => $subscriber->email,
'subject' => 'New incident reported.',
'status' => $data->humanStatus,
'htmlContent' => $data->formattedMessage,
'textContent' => $data->message,
'token' => $subscriber->token,
];
$this->mailer->queue([
'html' => 'emails.incidents.new-html',
'text' => 'emails.incidents.new-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
}
@@ -0,0 +1,88 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Events;
use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
use McCool\LaravelAutoPresenter\PresenterDecorator;
class SendMaintenanceEmailNotificationHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\MailQueue
*/
protected $mailer;
/**
* The subscriber instance.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
protected $subscriber;
/**
* The presenter instance.
*
* @var \McCool\LaravelAutoPresenter\PresenterDecorator
*/
protected $presenter;
/**
* Create a new send maintenance email notification handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
* @param \McCool\LaravelAutoPresenter\PresenterDecorator $presenter
*
* @return void
*/
public function __construct(MailQueue $mailer, Subscriber $subscriber, PresenterDecorator $presenter)
{
$this->mailer = $mailer;
$this->subscriber = $subscriber;
$this->presenter = $presenter;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent $event
*
* @return void
*/
public function handle(MaintenanceHasScheduledEvent $event)
{
$data = $this->presenter->decorate($event->incident);
foreach ($this->subscriber->all() as $subscriber) {
$mail = [
'email' => $subscriber->email,
'subject' => 'Scheduled maintenance.',
'status' => $data->humanStatus,
'htmlContent' => $data->formattedMessage,
'textContent' => $data->message,
'token' => $subscriber->token,
];
$this->mailer->queue([
'html' => 'emails.incidents.maintenance-html',
'text' => 'emails.incidents.maintenance-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
}
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Events;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
class SendSubscriberVerificationEmailHandler
{
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\MailQueue
*/
protected $mailer;
/**
* Create a new send subscriber verification email handler.
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
*
* @return void
*/
public function __construct(MailQueue $mailer)
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* @param \CachetHQ\Cachet\Events\CustomerHasSubscribedEvent $event
*
* @return void
*/
public function handle(CustomerHasSubscribedEvent $event)
{
$mail = [
'email' => $event->subscriber->email,
'subject' => 'Confirm your subscription.',
'link' => route('subscribe-verify', ['code' => $event->subscriber->verify_code]),
];
$this->mailer->queue([
'html' => 'emails.subscribers.verify-html',
'text' => 'emails.subscribers.verify-text',
], $mail, function (Message $message) use ($mail) {
$message->to($mail['email'])->subject($mail['subject']);
});
}
}
@@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Http\Controllers\Admin;
use CachetHQ\Cachet\Events\IncidentHasReportedEvent;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Http\Controllers\AbstractController;
use CachetHQ\Cachet\Models\Component;
@@ -157,6 +158,10 @@ class IncidentController extends AbstractController
trans('dashboard.incidents.add.success')
);
if (array_get($incidentData, 'notify')) {
event(new IncidentHasReportedEvent($incident));
}
return Redirect::back()->with('success', $successMsg);
}
@@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Http\Controllers\Admin;
use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Http\Controllers\AbstractController;
use CachetHQ\Cachet\Models\Incident;
@@ -137,6 +138,10 @@ class ScheduleController extends AbstractController
trans('dashboard.schedule.add.success')
);
if (array_get($scheduleData, 'notify')) {
event(new MaintenanceHasScheduledEvent($incident));
}
return Redirect::back()->with('success', $successMsg);
}
@@ -0,0 +1,149 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Subscriber;
use Carbon\Carbon;
use GrahamCampbell\Binput\Facades\Binput;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SubscribeController extends AbstractController
{
/**
* Show the subscribe by email page.
*
* @return \Illuminate\View\View
*/
public function showSubscribe()
{
return View::make('subscribe', [
'pageTitle' => Setting::get('app_name'),
'aboutApp' => Markdown::convertToHtml(Setting::get('app_about')),
]);
}
/**
* Handle the subscribe user.
*
* @return \Illuminate\View\View
*/
public function postSubscribe()
{
$subscriber = Subscriber::create(['email' => Binput::get('email')]);
if (!$subscriber->isValid()) {
segment_track('Subscribers', [
'event' => 'Customer Subscribed',
'success' => false,
]);
return Redirect::back()->withInput(Binput::all())
->with('title', sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.whoops'),
trans('cachet.subscriber.email.failure')
))
->with('errors', $subscriber->getErrors());
}
segment_track('Subscribers', [
'event' => 'Customer Subscribed',
'success' => true,
]);
$successMsg = sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.awesome'),
trans('cachet.subscriber.email.subscribed')
);
event(new CustomerHasSubscribedEvent($subscriber));
return Redirect::route('status-page')->with('success', $successMsg);
}
/**
* Handle the verify subscriber email.
*
* @param string $code
*
* @return \Illuminate\View\View
*/
public function getVerify($code = null)
{
if (is_null($code)) {
throw new NotFoundHttpException();
}
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || $subscriber->verified()) {
return Redirect::route('status-page');
}
$subscriber->verified_at = Carbon::now();
$subscriber->save();
segment_track('Subscribers', [
'event' => 'Customer Email Verified',
'success' => true,
]);
$successMsg = sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.awesome'),
trans('cachet.subscriber.email.verified')
);
return Redirect::route('status-page')->with('success', $successMsg);
}
/**
* Handle the unsubscribe.
*
* @param string $code
*
* @return \Illuminate\View\View
*/
public function getUnsubscribe($code = null)
{
if (is_null($code)) {
throw new NotFoundHttpException();
}
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || !$subscriber->verified()) {
return Redirect::route('status-page');
}
$subscriber->delete();
segment_track('Subscribers', [
'event' => 'Customer Unsubscribed',
'success' => true,
]);
$successMsg = sprintf(
'<strong>%s</strong> %s',
trans('dashboard.notifications.awesome'),
trans('cachet.subscriber.email.unsuscribed')
);
return Redirect::route('status-page')->with('success', $successMsg);
}
}
+16
View File
@@ -28,6 +28,22 @@ class StatusPageRoutes
'as' => 'status-page',
'uses' => 'HomeController@showIndex',
]);
$router->get('subscribe', [
'as' => 'subscribe-page',
'uses' => 'SubscribeController@showSubscribe',
]);
$router->post('subscribe', [
'as' => 'subscribe',
'uses' => 'SubscribeController@postSubscribe',
]);
$router->get('subscribe/verify/{code}', [
'as' => 'subscribe-verify',
'uses' => 'SubscribeController@getVerify',
]);
$router->get('unsubscribe/{code}', [
'as' => 'unsubscribe',
'uses' => 'SubscribeController@getUnsubscribe',
]);
$router->get('/atom/{component_group?}', 'AtomController@feedAction');
$router->get('/rss/{component_group?}', 'RssController@feedAction');
});
+46 -1
View File
@@ -18,6 +18,8 @@ use Watson\Validating\ValidatingTrait;
/**
* @property int $id
* @property string $email
* @property string $verify_code
* @property \Carbon\Carbon $verified_at
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at
@@ -32,7 +34,7 @@ class Subscriber extends Model
* @var string[]
*/
protected $rules = [
'email' => 'required|email',
'email' => 'required|email|unique:subscribers',
];
/**
@@ -41,4 +43,47 @@ class Subscriber extends Model
* @var string[]
*/
protected $fillable = ['email'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at', 'verified_at'];
/**
* Overrides the models boot method.
*
* @return void
*/
public static function boot()
{
parent::boot();
self::creating(function ($user) {
if (!$user->verify_code) {
$user->verify_code = self::generateVerifyCode();
}
});
}
/**
* Determines if the subscriber is verified.
*
* @return bool
*/
public function verified()
{
return !is_null($this->verified_at);
}
/**
* Returns an new verify code.
*
* @return string
*/
public static function generateVerifyCode()
{
return str_random(42);
}
}
+2 -2
View File
@@ -28,8 +28,8 @@ class ComposerServiceProvider extends ServiceProvider
public function register()
{
$this->app->view->composer('*', LoggedUserComposer::class);
$this->app->view->composer('index', IndexComposer::class);
$this->app->view->composer('index', ThemeComposer::class);
$this->app->view->composer(['index', 'subscribe'], IndexComposer::class);
$this->app->view->composer(['index', 'subscribe'], ThemeComposer::class);
$this->app->view->composer('dashboard.*', DashboardComposer::class);
$this->app->view->composer(['setup', 'dashboard.settings.app-setup'], TimezoneLocaleComposer::class);
}
+8 -2
View File
@@ -21,8 +21,14 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'event.name' => [
'EventListener',
'CachetHQ\Cachet\Events\CustomerHasSubscribedEvent' => [
'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler',
],
'CachetHQ\Cachet\Events\IncidentHasReportedEvent' => [
'CachetHQ\Cachet\Handlers\Events\SendIncidentEmailNotificationHandler',
],
'CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent' => [
'CachetHQ\Cachet\Handlers\Events\SendMaintenanceEmailNotificationHandler',
],
];
}
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*/
public function boot()
{
//
}
/**
* Register the service provider.
*/
public function register()
{
$this->app->view->composer('*', 'CachetHQ\Cachet\Composers\LoggedUserComposer');
$this->app->view->composer('index', 'CachetHQ\Cachet\Composers\IndexComposer');
$this->app->view->composer('index', 'CachetHQ\Cachet\Composers\ThemeComposer');
$this->app->view->composer('subscribe', 'CachetHQ\Cachet\Composers\ThemeComposer');
$this->app->view->composer('dashboard.*', 'CachetHQ\Cachet\Composers\DashboardComposer');
$this->app->view->composer([
'setup',
'dashboard.settings.app-setup',
], 'CachetHQ\Cachet\Composers\TimezoneLocaleComposer');
}
}