From 17fb0e236ff22d2922cde7f478a52cfed4c167b2 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Mon, 31 Aug 2020 22:06:00 +0100 Subject: [PATCH] Make notifications cancellable This commit makes certain notifications cancellable. This enables notifications to be sent conditionally based on the user's choice. --- app/Notifications/ApplicationApproved.php | 20 ++++----- app/Notifications/ApplicationMoved.php | 14 +++---- app/Notifications/NewApplicant.php | 25 +++++++----- app/Notifications/NewComment.php | 14 +++---- app/Notifications/NewUser.php | 20 +++++---- app/Notifications/VacancyClosed.php | 14 +++---- app/Traits/Cancellable.php | 49 +++++++++++++++++++++++ database/seeds/DefaultOptionsSeeder.php | 12 +++--- 8 files changed, 106 insertions(+), 62 deletions(-) create mode 100644 app/Traits/Cancellable.php diff --git a/app/Notifications/ApplicationApproved.php b/app/Notifications/ApplicationApproved.php index b1415ec..bfde408 100644 --- a/app/Notifications/ApplicationApproved.php +++ b/app/Notifications/ApplicationApproved.php @@ -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; } /** diff --git a/app/Notifications/ApplicationMoved.php b/app/Notifications/ApplicationMoved.php index 0559cc8..9598258 100644 --- a/app/Notifications/ApplicationMoved.php +++ b/app/Notifications/ApplicationMoved.php @@ -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; } /** diff --git a/app/Notifications/NewApplicant.php b/app/Notifications/NewApplicant.php index d054b34..0fdbac9 100644 --- a/app/Notifications/NewApplicant.php +++ b/app/Notifications/NewApplicant.php @@ -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; } /** diff --git a/app/Notifications/NewComment.php b/app/Notifications/NewComment.php index 2f6d875..10c7bfc 100644 --- a/app/Notifications/NewComment.php +++ b/app/Notifications/NewComment.php @@ -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; } /** diff --git a/app/Notifications/NewUser.php b/app/Notifications/NewUser.php index 30ffe1f..ec31cd6 100644 --- a/app/Notifications/NewUser.php +++ b/app/Notifications/NewUser.php @@ -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; } /** diff --git a/app/Notifications/VacancyClosed.php b/app/Notifications/VacancyClosed.php index 5f2f867..8ae85e4 100644 --- a/app/Notifications/VacancyClosed.php +++ b/app/Notifications/VacancyClosed.php @@ -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; } /** diff --git a/app/Traits/Cancellable.php b/app/Traits/Cancellable.php new file mode 100644 index 0000000..cbccb67 --- /dev/null +++ b/app/Traits/Cancellable.php @@ -0,0 +1,49 @@ +optOut($notifiable)) + { + return []; + } + + return $this->channels(); + } + + + public function optOut($notifiable) + { + return false; + } + +} diff --git a/database/seeds/DefaultOptionsSeeder.php b/database/seeds/DefaultOptionsSeeder.php index c7fbebc..90e73c5 100644 --- a/database/seeds/DefaultOptionsSeeder.php +++ b/database/seeds/DefaultOptionsSeeder.php @@ -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');