Make notifications cancellable

This commit makes certain notifications cancellable. This enables notifications to be sent conditionally based on the user's choice.
This commit is contained in:
Miguel Nogueira 2020-08-31 22:06:00 +01:00
parent 27b1f3170b
commit 17fb0e236f
8 changed files with 106 additions and 62 deletions

View File

@ -2,6 +2,8 @@
namespace App\Notifications;
use App\Facades\Options;
use App\Traits\Cancellable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
@ -11,7 +13,7 @@ use App\Application;
class ApplicationApproved extends Notification implements ShouldQueue
{
use Queueable;
use Queueable, Cancellable;
public $application;
@ -24,15 +26,15 @@ class ApplicationApproved extends Notification implements ShouldQueue
{
$this->application = $application;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function channels()
{
return ['mail', 'slack'];
return $this->chooseChannelsViaOptions();
}
public function optOut($notifiable)
{
return Options::getOption('notify_applicant_approved') !== 1;
}
/**

View File

@ -6,10 +6,12 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Traits\Cancellable;
use App\Facades\Options;
class ApplicationMoved extends Notification implements ShouldQueue
{
use Queueable;
use Queueable, Cancellable;
/**
* Create a new notification instance.
@ -21,15 +23,9 @@ class ApplicationMoved extends Notification implements ShouldQueue
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function optOut($notifiable)
{
return ['mail'];
return Options::getOption('notify_application_status_change') !== 1;
}
/**

View File

@ -10,9 +10,12 @@ use Illuminate\Notifications\Notification;
use App\Application;
use App\Vacancy;
use App\Traits\Cancellable;
use App\Facades\Options;
class NewApplicant extends Notification implements ShouldQueue
{
use Queueable;
use Queueable, Cancellable;
protected $application;
@ -31,15 +34,19 @@ class NewApplicant extends Notification implements ShouldQueue
$this->vacancy = $vacancy;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function channels()
{
return ['slack'];
if (Options::getOption('enable_slack_notifications') == 1)
{
return ['slack'];
}
return [];
}
public function optOut($notifiable)
{
return Options::getOption('notify_new_user') !== 1;
}
/**

View File

@ -8,10 +8,12 @@ use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Comment;
use App\Application;
use App\Traits\Cancellable;
use App\Facades\Options;
class NewComment extends Notification implements ShouldQueue
{
use Queueable;
use Queueable, Cancellable;
protected $application;
@ -26,15 +28,9 @@ class NewComment extends Notification implements ShouldQueue
$this->application = $application;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function optOut($notifiable)
{
return ['mail'];
return Options::getOption('notify_application_comment') !== 1;
}
/**

View File

@ -10,10 +10,12 @@ use Illuminate\Notifications\Messages\SlackMessage;
use App\User;
use App\Facades\UUID;
use App\Traits\Cancellable;
use App\Facades\Options;
class NewUser extends Notification implements ShouldQueue
{
use Queueable;
use Queueable, Cancellable;
public $user;
@ -27,18 +29,14 @@ class NewUser extends Notification implements ShouldQueue
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function channels($notifiable)
{
return ($notifiable->isStaffMember())
? ['slack', 'mail']
: ['mail'];
return $this->chooseChannelsViaOptions();
}
public function optOut($notifiable)
{
return Options::getOption('notify_new_user') !== 1;
}
/**

View File

@ -9,10 +9,12 @@ use Illuminate\Notifications\Notification;
use Illuminate\Queue\SerializesModels;
use App\Vacancy;
use App\Facades\Options;
use App\Traits\Cancellable;
class VacancyClosed extends Notification implements ShouldQueue
{
use Queueable, SerializesModels;
use Queueable, SerializesModels, Cancellable;
protected $vacancy;
@ -26,15 +28,9 @@ class VacancyClosed extends Notification implements ShouldQueue
$this->vacancy = $vacancy;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
public function optOut($notifiable)
{
return ['mail'];
return Options::getOption('notify_vacancystatus_change') !== 1;
}
/**

View File

@ -0,0 +1,49 @@
<?php
namespace App\Traits;
use App\Facades\Options;
trait Cancellable
{
public function chooseChannelsViaOptions()
{
$channels = [];
if (Options::getOption('enable_slack_notifications') == 1)
{
array_push($channels, 'slack');
}
elseif(Options::getOption('enable_email_notifications') == 1)
{
array_push($channels, 'email');
}
return $channels;
}
public function channels()
{
return ['mail'];
}
public function via($notifiable)
{
if ($this->optOut($notifiable))
{
return [];
}
return $this->channels();
}
public function optOut($notifiable)
{
return false;
}
}

View File

@ -12,12 +12,12 @@ class DefaultOptionsSeeder extends Seeder
*/
public function run()
{
Options::setOption('notify_new_application_email', true, 'Notify when a new application comes through');
Options::setOption('notify_application_comment', false, 'Notify when someone comments on an application');
Options::setOption('notify_new_user', true, 'Notify when someone signs up');
Options::setOption('notify_application_status_change', true, 'Notify when an application changes status');
Options::setOption('notify_applicant_approved', true, 'Notify when an applicant is approved');
Options::setOption('notify_vacancystatus_change', false, 'Notify when a vacancy\'s status changes');
Options::setOption('notify_new_application_email', true, 'Notify when a new application comes through'); // done
Options::setOption('notify_application_comment', false, 'Notify when someone comments on an application'); // done
Options::setOption('notify_new_user', true, 'Notify when someone signs up'); // done
Options::setOption('notify_application_status_change', true, 'Notify when an application changes status'); // done
Options::setOption('notify_applicant_approved', true, 'Notify when an applicant is approved'); // done
Options::setOption('notify_vacancystatus_change', false, 'Notify when a vacancy\'s status changes'); // done
Options::setOption('enable_slack_notifications', true, 'Enable slack notifications');