Refactor broadcast notifications (#1402)
* Refactor broadcast notifications * StyleCI fixes * Fix the planned_distance accidental changes
This commit is contained in:
@@ -4,8 +4,6 @@ namespace App\Notifications\Messages;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\User;
|
||||
use App\Notifications\Channels\Discord\DiscordMessage;
|
||||
use App\Notifications\Channels\Discord\DiscordWebhook;
|
||||
use App\Notifications\Channels\MailChannel;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
@@ -32,32 +30,21 @@ class AdminUserRegistered extends Notification implements ShouldQueue
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $notifiable
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['mail', DiscordWebhook::class];
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Discord notification
|
||||
* @param $notifiable
|
||||
*
|
||||
* @param User $pirep
|
||||
* @param mixed $user
|
||||
*
|
||||
* @return DiscordMessage|null
|
||||
* @return array
|
||||
*/
|
||||
public function toDiscordChannel($user): ?DiscordMessage
|
||||
{
|
||||
if (empty(setting('notifications.discord_private_webhook_url'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$dm = new DiscordMessage();
|
||||
return $dm->webhook(setting('notifications.discord_private_webhook_url'))
|
||||
->success()
|
||||
->title('New User Registered: '.$user->ident)
|
||||
->fields([]);
|
||||
}
|
||||
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages\Broadcast;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\News;
|
||||
use App\Notifications\Channels\Discord\DiscordMessage;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class NewsAdded extends Notification implements ShouldQueue
|
||||
{
|
||||
private $news;
|
||||
|
||||
public function __construct(News $news)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->news = $news;
|
||||
}
|
||||
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['discord_webhook'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param News $news
|
||||
*
|
||||
* @return DiscordMessage|null
|
||||
*/
|
||||
public function toDiscordChannel($news): ?DiscordMessage
|
||||
{
|
||||
$dm = new DiscordMessage();
|
||||
return $dm->success()
|
||||
->title('News: '.$news->subject)
|
||||
->author([
|
||||
'name' => $news->user->ident.' - '.$news->user->name_private,
|
||||
'url' => '',
|
||||
'icon_url' => $news->user->resolveAvatarUrl(),
|
||||
])
|
||||
->description($news->body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
'news_id' => $this->news->id,
|
||||
];
|
||||
}
|
||||
}
|
||||
+14
-29
@@ -1,22 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages;
|
||||
namespace App\Notifications\Messages\Broadcast;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\Pirep;
|
||||
use App\Notifications\Channels\Discord\DiscordMessage;
|
||||
use App\Notifications\Channels\Discord\DiscordWebhook;
|
||||
use App\Notifications\Channels\MailChannel;
|
||||
use App\Support\Units\Distance;
|
||||
use App\Support\Units\Time;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
|
||||
class PirepSubmitted extends Notification implements ShouldQueue
|
||||
class PirepFiled extends Notification implements ShouldQueue
|
||||
{
|
||||
use MailChannel;
|
||||
|
||||
private $pirep;
|
||||
|
||||
/**
|
||||
@@ -29,17 +25,11 @@ class PirepSubmitted extends Notification implements ShouldQueue
|
||||
parent::__construct();
|
||||
|
||||
$this->pirep = $pirep;
|
||||
|
||||
$this->setMailable(
|
||||
'New PIREP Submitted',
|
||||
'notifications.mail.admin.pirep.submitted',
|
||||
['pirep' => $this->pirep]
|
||||
);
|
||||
}
|
||||
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['mail', DiscordWebhook::class];
|
||||
return ['discord_webhook'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,35 +41,30 @@ class PirepSubmitted extends Notification implements ShouldQueue
|
||||
*/
|
||||
public function toDiscordChannel($pirep): ?DiscordMessage
|
||||
{
|
||||
if (empty(setting('notifications.discord_public_webhook_url'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$title = 'Flight '.$pirep->ident.' Filed';
|
||||
$fields = [
|
||||
'Flight' => $pirep->ident,
|
||||
'Departure Airport' => $pirep->dpt_airport_id,
|
||||
'Arrival Airport' => $pirep->arr_airport_id,
|
||||
'Equipment' => $pirep->aircraft->ident,
|
||||
'Flight Time (Planned)' => Time::minutesToTimeString($pirep->planned_flight_time),
|
||||
'Flight' => $pirep->ident,
|
||||
'Departure Airport' => $pirep->dpt_airport_id,
|
||||
'Arrival Airport' => $pirep->arr_airport_id,
|
||||
'Equipment' => $pirep->aircraft->ident,
|
||||
'Flight Time' => Time::minutesToTimeString($pirep->flight_time),
|
||||
];
|
||||
|
||||
if ($pirep->planned_distance) {
|
||||
if ($pirep->distance) {
|
||||
try {
|
||||
$planned_distance = new Distance(
|
||||
$pirep->planned_distance,
|
||||
$distance = new Distance(
|
||||
$pirep->distance,
|
||||
config('phpvms.internal_units.distance')
|
||||
);
|
||||
|
||||
$pd = $planned_distance[$planned_distance->unit].' '.$planned_distance->unit;
|
||||
$fields['Distance (Planned)'] = $pd;
|
||||
$pd = $distance[$distance->unit].' '.$distance->unit;
|
||||
$fields['Distance'] = $pd;
|
||||
} catch (NonNumericValue|NonStringUnitName $e) {
|
||||
}
|
||||
}
|
||||
|
||||
$dm = new DiscordMessage();
|
||||
return $dm->webhook(setting('notifications.discord_public_webhook_url'))
|
||||
->success()
|
||||
return $dm->success()
|
||||
->title($title)
|
||||
->description($pirep->user->discord_id ? 'Flight by <@'.$pirep->user->discord_id.'>' : '')
|
||||
->url(route('frontend.pireps.show', [$pirep->id]))
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages;
|
||||
namespace App\Notifications\Messages\Broadcast;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\Pirep;
|
||||
+4
-7
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages;
|
||||
namespace App\Notifications\Messages\Broadcast;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\Enums\PirepStatus;
|
||||
@@ -9,9 +9,11 @@ use App\Notifications\Channels\Discord\DiscordMessage;
|
||||
use App\Notifications\Channels\Discord\DiscordWebhook;
|
||||
use App\Support\Units\Distance;
|
||||
use App\Support\Units\Time;
|
||||
use function config;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
||||
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
||||
use function route;
|
||||
|
||||
/**
|
||||
* Send the PIREP accepted message to a particular user, can also be sent to Discord
|
||||
@@ -73,10 +75,6 @@ class PirepStatusChanged extends Notification implements ShouldQueue
|
||||
*/
|
||||
public function toDiscordChannel($pirep): ?DiscordMessage
|
||||
{
|
||||
if (empty(setting('notifications.discord_public_webhook_url'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$title = 'Flight '.$pirep->ident.' '.self::$verbs[$pirep->status];
|
||||
|
||||
$fields = [
|
||||
@@ -112,8 +110,7 @@ class PirepStatusChanged extends Notification implements ShouldQueue
|
||||
}
|
||||
|
||||
$dm = new DiscordMessage();
|
||||
return $dm->webhook(setting('notifications.discord_public_webhook_url'))
|
||||
->success()
|
||||
return $dm->success()
|
||||
->title($title)
|
||||
->description($pirep->user->discord_id ? 'Flight by <@'.$pirep->user->discord_id.'>' : '')
|
||||
->url(route('frontend.pireps.show', [$pirep->id]))
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages\Broadcast;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\User;
|
||||
use App\Notifications\Channels\Discord\DiscordMessage;
|
||||
use App\Notifications\Channels\MailChannel;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
/**
|
||||
* Send a message to a Discord channel that a user was registered
|
||||
*/
|
||||
class UserRegistered extends Notification implements ShouldQueue
|
||||
{
|
||||
use MailChannel;
|
||||
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $notifiable
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['discord_webhook'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Discord notification
|
||||
*
|
||||
* @param $notifiable
|
||||
*
|
||||
* @return DiscordMessage|null
|
||||
*/
|
||||
public function toDiscordChannel($notifiable): ?DiscordMessage
|
||||
{
|
||||
$dm = new DiscordMessage();
|
||||
return $dm->success()
|
||||
->title('New User Registered: '.$this->user->ident)
|
||||
->author([
|
||||
'name' => $this->user->ident.' - '.$this->user->name_private,
|
||||
'url' => '',
|
||||
'icon_url' => $this->user->resolveAvatarUrl(),
|
||||
])
|
||||
->fields([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
'user_id' => $this->user->id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Messages;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Models\Pirep;
|
||||
use App\Notifications\Channels\MailChannel;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class PirepFiled extends Notification implements ShouldQueue
|
||||
{
|
||||
use MailChannel;
|
||||
|
||||
private $pirep;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @param \App\Models\Pirep $pirep
|
||||
*/
|
||||
public function __construct(Pirep $pirep)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->pirep = $pirep;
|
||||
|
||||
$this->setMailable(
|
||||
'New PIREP Submitted',
|
||||
'notifications.mail.admin.pirep.submitted',
|
||||
['pirep' => $this->pirep]
|
||||
);
|
||||
}
|
||||
|
||||
public function via($notifiable)
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
'pirep_id' => $this->pirep->id,
|
||||
'user_id' => $this->pirep->user_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user