RSM-38 Added Discord Routing for relevant notifications

This commit is contained in:
Miguel Nogueira 2020-10-10 00:39:53 +01:00
parent 2a43e213f9
commit 82123b1c26
9 changed files with 90 additions and 16 deletions

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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)
{

View File

@ -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.
*

View File

@ -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;
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Traits;
trait DiscordRoutable
{
public function routeNotificationForDiscord()
{
return config('channels.notifications.discord.webhook_url');
}
}

17
config/channels.php Normal file
View File

@ -0,0 +1,17 @@
<?php
return [
'notifications' => [
'discord' => [
'webhook_url' => env('DISCORD_INTEGRATION_WEBHOOK')
],
'slack' => [
'webhook_url' => env('SLACK_INTEGRATION_WEBHOOK')
]
]
];

View File

@ -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;