From 82123b1c266701e6c94a0831d8576b249bd74ef6 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Sat, 10 Oct 2020 00:39:53 +0100 Subject: [PATCH] RSM-38 Added Discord Routing for relevant notifications --- app/Notifications/ApplicationApproved.php | 8 +++++++- app/Notifications/ApplicationDenied.php | 24 ++++++++++++++++++++--- app/Notifications/NewApplicant.php | 16 ++++++++------- app/Notifications/NewComment.php | 3 ++- app/Notifications/NewUser.php | 8 +++++++- app/Traits/Cancellable.php | 10 +++++++++- app/Traits/DiscordRoutable.php | 15 ++++++++++++++ config/channels.php | 17 ++++++++++++++++ routes/web.php | 5 +++-- 9 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 app/Traits/DiscordRoutable.php create mode 100644 config/channels.php diff --git a/app/Notifications/ApplicationApproved.php b/app/Notifications/ApplicationApproved.php index bfde408..aff0bef 100644 --- a/app/Notifications/ApplicationApproved.php +++ b/app/Notifications/ApplicationApproved.php @@ -4,6 +4,7 @@ namespace App\Notifications; use App\Facades\Options; use App\Traits\Cancellable; +use App\Traits\DiscordRoutable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -13,7 +14,7 @@ use App\Application; class ApplicationApproved extends Notification implements ShouldQueue { - use Queueable, Cancellable; + use Queueable, Cancellable, DiscordRoutable; public $application; @@ -80,6 +81,11 @@ class ApplicationApproved extends Notification implements ShouldQueue }); } + public function toDiscord($notifiable) + { + return $this->toSlack($notifiable); + } + /** * Get the array representation of the notification. * diff --git a/app/Notifications/ApplicationDenied.php b/app/Notifications/ApplicationDenied.php index 03a4382..09d4d98 100644 --- a/app/Notifications/ApplicationDenied.php +++ b/app/Notifications/ApplicationDenied.php @@ -2,6 +2,9 @@ namespace App\Notifications; +use App\Facades\Options; +use App\Traits\DiscordRoutable; +use Awssat\Notifications\Messages\DiscordMessage; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -11,7 +14,7 @@ use App\Application; class ApplicationDenied extends Notification implements ShouldQueue { - use Queueable; + use Queueable, DiscordRoutable; public $application; @@ -34,14 +37,22 @@ class ApplicationDenied extends Notification implements ShouldQueue */ public function via($notifiable) { - return ['mail', 'slack']; + $options = ['mail']; + + if (Options::getOption('enable_discord_notifications')) + array_push($options, 'discord'); + + if (Options::getOption('enable_slack_notifications')) + array_push($options, 'slack'); + + return $options; } /** * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { @@ -70,6 +81,13 @@ class ApplicationDenied extends Notification implements ShouldQueue }); } + + public function toDiscord($notifiable) + { + // SlackMessage is similar to DiscordMessage, so they're compatible + return $this->toSlack($notifiable); + } + /** * Get the array representation of the notification. * diff --git a/app/Notifications/NewApplicant.php b/app/Notifications/NewApplicant.php index 187ba38..79c43b2 100644 --- a/app/Notifications/NewApplicant.php +++ b/app/Notifications/NewApplicant.php @@ -2,6 +2,7 @@ namespace App\Notifications; +use App\Traits\DiscordRoutable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -15,7 +16,7 @@ use App\Facades\Options; class NewApplicant extends Notification implements ShouldQueue { - use Queueable, Cancellable; + use Queueable, Cancellable, DiscordRoutable; protected $application; @@ -37,12 +38,7 @@ class NewApplicant extends Notification implements ShouldQueue public function channels() { - if (Options::getOption('enable_slack_notifications') == 1) - { - return ['slack']; - } - - return []; + $this->chooseChannelsViaOptions(); } public function optOut($notifiable) @@ -91,6 +87,12 @@ class NewApplicant extends Notification implements ShouldQueue ->action('Review application', $url); }); } + + public function toDiscord($notifiable) + { + return $this->toSlack($notifiable); + } + /** * Get the array representation of the notification. * diff --git a/app/Notifications/NewComment.php b/app/Notifications/NewComment.php index 10c7bfc..1ec1be6 100644 --- a/app/Notifications/NewComment.php +++ b/app/Notifications/NewComment.php @@ -21,7 +21,8 @@ class NewComment extends Notification implements ShouldQueue /** * Create a new notification instance. * - * @return void + * @param Comment $comment + * @param Application $application */ public function __construct(Comment $comment, Application $application) { diff --git a/app/Notifications/NewUser.php b/app/Notifications/NewUser.php index ec31cd6..60db875 100644 --- a/app/Notifications/NewUser.php +++ b/app/Notifications/NewUser.php @@ -2,6 +2,7 @@ namespace App\Notifications; +use App\Traits\DiscordRoutable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -15,7 +16,7 @@ use App\Facades\Options; class NewUser extends Notification implements ShouldQueue { - use Queueable, Cancellable; + use Queueable, Cancellable, DiscordRoutable; public $user; @@ -83,6 +84,11 @@ class NewUser extends Notification implements ShouldQueue }); } + public function toDiscord($notifiable) + { + return $this->toSlack($notifiable); + } + /** * Get the array representation of the notification. * diff --git a/app/Traits/Cancellable.php b/app/Traits/Cancellable.php index cbccb67..91d6501 100644 --- a/app/Traits/Cancellable.php +++ b/app/Traits/Cancellable.php @@ -9,6 +9,8 @@ use App\Facades\Options; trait Cancellable { + // This method is only used if you want this default set of channels; + // Other channels can always be configured by overloading the channels method here. public function chooseChannelsViaOptions() { $channels = []; @@ -17,11 +19,17 @@ trait Cancellable { array_push($channels, 'slack'); } - elseif(Options::getOption('enable_email_notifications') == 1) + + if (Options::getOption('enable_email_notifications') == 1) { array_push($channels, 'email'); } + if (Options::getOption('enable_discord_notifications')) + { + array_push($channels, 'discord'); + } + return $channels; } diff --git a/app/Traits/DiscordRoutable.php b/app/Traits/DiscordRoutable.php new file mode 100644 index 0000000..5ee33d5 --- /dev/null +++ b/app/Traits/DiscordRoutable.php @@ -0,0 +1,15 @@ + [ + + 'discord' => [ + 'webhook_url' => env('DISCORD_INTEGRATION_WEBHOOK') + ], + + 'slack' => [ + 'webhook_url' => env('SLACK_INTEGRATION_WEBHOOK') + ] + + ] + +]; diff --git a/routes/web.php b/routes/web.php index 4467c7f..dc8f76a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,6 +15,7 @@ use App\Http\Controllers\TeamController; use App\Http\Controllers\UserController; use App\Http\Controllers\VacancyController; use App\Http\Controllers\VoteController; +use App\Http\Controllers\OptionsController; use Illuminate\Support\Facades\Route; use Mcamara\LaravelLocalization\Facades\LaravelLocalization; @@ -63,7 +64,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'l Route::resource('teams', TeamController::class); - + Route::post('teams/{team}/invites/send', [TeamController::class, 'invite']) ->name('sendInvite'); @@ -77,7 +78,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'l Route::get('teams/invites/{action}/{token}', [TeamController::class, 'processInviteAction']) ->name('processInvite'); - + Route::group(['prefix' => '/applications'], function (){