2017-12-23 02:00:57 +08:00
|
|
|
<?php
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
namespace App\Notifications;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Listener;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Events\NewsAdded;
|
2019-08-31 03:59:17 +08:00
|
|
|
use App\Events\PirepAccepted;
|
|
|
|
use App\Events\PirepFiled;
|
2021-06-04 22:51:59 +08:00
|
|
|
use App\Events\PirepPrefiled;
|
2019-08-31 03:59:17 +08:00
|
|
|
use App\Events\PirepRejected;
|
2021-06-04 22:51:59 +08:00
|
|
|
use App\Events\PirepStatusChange;
|
2018-01-01 01:09:56 +08:00
|
|
|
use App\Events\UserRegistered;
|
2018-01-01 13:15:12 +08:00
|
|
|
use App\Events\UserStateChanged;
|
2017-12-31 10:40:32 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2019-08-31 03:59:17 +08:00
|
|
|
use App\Models\User;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Notifications\Messages\UserRejected;
|
|
|
|
use App\Notifications\Notifiables\Broadcast;
|
|
|
|
use Exception;
|
2020-09-04 00:50:42 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2019-08-31 03:59:17 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
|
|
|
/**
|
2020-02-09 02:29:34 +08:00
|
|
|
* Listen for different events and map them to different notifications
|
2017-12-23 02:00:57 +08:00
|
|
|
*/
|
2021-06-04 22:51:59 +08:00
|
|
|
class NotificationEventsHandler extends Listener
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
private static $broadcastNotifyable;
|
|
|
|
|
|
|
|
public static $callbacks = [
|
2021-06-04 22:51:59 +08:00
|
|
|
NewsAdded::class => 'onNewsAdded',
|
|
|
|
PirepPrefiled::class => 'onPirepPrefile',
|
|
|
|
PirepStatusChange::class => 'onPirepStatusChange',
|
|
|
|
PirepAccepted::class => 'onPirepAccepted',
|
|
|
|
PirepFiled::class => 'onPirepFile',
|
|
|
|
PirepRejected::class => 'onPirepRejected',
|
|
|
|
UserRegistered::class => 'onUserRegister',
|
|
|
|
UserStateChanged::class => 'onUserStateChange',
|
2020-02-09 02:29:34 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct()
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
static::$broadcastNotifyable = app(Broadcast::class);
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-01 13:15:12 +08:00
|
|
|
/**
|
2019-08-31 03:59:17 +08:00
|
|
|
* Send a notification to all of the admins
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-09-04 00:50:42 +08:00
|
|
|
* @param \App\Contracts\Notification $notification
|
2018-01-01 13:26:10 +08:00
|
|
|
*/
|
2021-04-23 22:33:13 +08:00
|
|
|
protected function notifyAdmins(\App\Contracts\Notification $notification)
|
2018-01-01 13:26:10 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
$admin_users = User::whereRoleIs('admin')->get();
|
2019-09-18 07:11:02 +08:00
|
|
|
|
2021-04-12 02:32:58 +08:00
|
|
|
foreach ($admin_users as $user) {
|
|
|
|
if (empty($user->email)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-04 22:51:59 +08:00
|
|
|
$this->notifyUser($user, $notification);
|
|
|
|
// Notification::send([$user], $notification);
|
2021-04-12 02:32:58 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::emergency('Error emailing admin ('.$user->email.'). Error='.$e->getMessage());
|
|
|
|
}
|
2019-09-18 07:11:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-04 00:50:42 +08:00
|
|
|
* @param User $user
|
|
|
|
* @param \App\Contracts\Notification $notification
|
2019-09-18 07:11:02 +08:00
|
|
|
*/
|
2021-04-23 22:33:13 +08:00
|
|
|
protected function notifyUser(User $user, \App\Contracts\Notification $notification)
|
2019-09-18 07:11:02 +08:00
|
|
|
{
|
2021-04-23 22:33:13 +08:00
|
|
|
if ($user->state === UserState::DELETED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-18 07:11:02 +08:00
|
|
|
try {
|
|
|
|
$user->notify($notification);
|
2020-02-09 02:29:34 +08:00
|
|
|
} catch (Exception $e) {
|
2021-04-12 02:32:58 +08:00
|
|
|
Log::emergency('Error emailing user, '.$user->ident.'='.$user->email.', error='.$e->getMessage());
|
2020-02-09 02:29:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-04 00:50:42 +08:00
|
|
|
* Send a notification to all users. Also can specify if a particular notification
|
|
|
|
* requires an opt-in
|
2020-02-09 02:29:34 +08:00
|
|
|
*
|
2020-09-04 00:50:42 +08:00
|
|
|
* @param \App\Contracts\Notification $notification
|
2020-02-09 02:29:34 +08:00
|
|
|
*/
|
2020-09-04 00:50:42 +08:00
|
|
|
protected function notifyAllUsers(\App\Contracts\Notification $notification)
|
2020-02-09 02:29:34 +08:00
|
|
|
{
|
2020-09-04 00:50:42 +08:00
|
|
|
$where = [];
|
|
|
|
if ($notification->requires_opt_in === true) { // If the opt-in is required
|
|
|
|
$where['opt_in'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Collection $users */
|
2021-04-23 22:33:13 +08:00
|
|
|
$users = User::where($where)->where('state', '<>', UserState::DELETED)->get();
|
2020-09-04 00:50:42 +08:00
|
|
|
if (empty($users) || $users->count() === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Sending notification to '.$users->count().' users');
|
2020-02-09 02:29:34 +08:00
|
|
|
|
2021-04-12 02:32:58 +08:00
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->notifyUser($user, $notification);
|
2019-09-18 07:11:02 +08:00
|
|
|
}
|
2018-01-01 13:26:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
/**
|
|
|
|
* Send an email when the user registered
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 02:46:46 +08:00
|
|
|
* @param UserRegistered $event
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function onUserRegister(UserRegistered $event): void
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
Log::info('NotificationEvents::onUserRegister: '
|
2019-07-17 01:54:14 +08:00
|
|
|
.$event->user->ident.' is '
|
2020-02-09 02:29:34 +08:00
|
|
|
.UserState::label($event->user->state).', sending active email');
|
2017-12-23 02:46:46 +08:00
|
|
|
|
2019-08-31 03:59:17 +08:00
|
|
|
/*
|
|
|
|
* Send the user a confirmation email
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($event->user->state === UserState::ACTIVE) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::PENDING) {
|
2021-06-04 22:51:59 +08:00
|
|
|
$this->notifyUser($event->user, new Messages\UserPending($event->user));
|
2017-12-23 02:46:46 +08:00
|
|
|
}
|
2021-06-04 22:51:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send all of the admins a notification that a new user registered
|
|
|
|
*/
|
|
|
|
$this->notifyAdmins(new Messages\AdminUserRegistered($event->user));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discord and other notifications
|
|
|
|
*/
|
|
|
|
Notification::send([$event->user], new Messages\AdminUserRegistered($event->user));
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* When a user's state changes, send an email out
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param UserStateChanged $event
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function onUserStateChange(UserStateChanged $event): void
|
2017-12-23 06:32:21 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
Log::info('NotificationEvents::onUserStateChange: New user state='.$event->user->state);
|
2018-01-01 13:15:12 +08:00
|
|
|
|
2017-12-31 10:40:32 +08:00
|
|
|
if ($event->old_state === UserState::PENDING) {
|
2018-01-01 13:26:10 +08:00
|
|
|
if ($event->user->state === UserState::ACTIVE) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::REJECTED) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new UserRejected($event->user));
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
2019-08-31 03:59:17 +08:00
|
|
|
} elseif ($event->old_state === UserState::ACTIVE) {
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('User state change from active to ??');
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-31 03:59:17 +08:00
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
/**
|
|
|
|
* Prefile notification
|
|
|
|
*/
|
|
|
|
public function onPirepPrefile(PirepPrefiled $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepPrefile: '.$event->pirep->id.' prefiled');
|
|
|
|
Notification::send([$event->pirep], new Messages\PirepPrefiled($event->pirep));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status Change notification
|
|
|
|
*/
|
|
|
|
public function onPirepStatusChange(PirepStatusChange $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepStatusChange: '.$event->pirep->id.' prefiled');
|
|
|
|
Notification::send([$event->pirep], new Messages\PirepStatusChanged($event->pirep));
|
|
|
|
}
|
|
|
|
|
2019-08-31 03:59:17 +08:00
|
|
|
/**
|
|
|
|
* Notify the admins that a new PIREP has been filed
|
|
|
|
*
|
2021-06-04 22:51:59 +08:00
|
|
|
* @param PirepFiled $event
|
2019-08-31 03:59:17 +08:00
|
|
|
*/
|
|
|
|
public function onPirepFile(PirepFiled $event): void
|
|
|
|
{
|
2021-06-04 22:51:59 +08:00
|
|
|
Log::info('NotificationEvents::onPirepFile: '.$event->pirep->id.' filed');
|
|
|
|
$this->notifyAdmins(new Messages\PirepSubmitted($event->pirep));
|
|
|
|
Notification::send([$event->pirep], new Messages\PirepSubmitted($event->pirep));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user that their PIREP has been accepted
|
|
|
|
*
|
|
|
|
* @param \App\Events\PirepAccepted $event
|
|
|
|
*/
|
|
|
|
public function onPirepAccepted(PirepAccepted $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepAccepted: '.$event->pirep->id.' accepted');
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new Messages\PirepAccepted($event->pirep));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user that their PIREP has been accepted
|
|
|
|
*
|
|
|
|
* @param \App\Events\PirepRejected $event
|
|
|
|
*/
|
|
|
|
public function onPirepRejected(PirepRejected $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepRejected: '.$event->pirep->id.' rejected');
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new Messages\PirepRejected($event->pirep));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-04 00:50:42 +08:00
|
|
|
* Notify all users of a news event, but only the users which have opted in
|
2020-02-09 02:29:34 +08:00
|
|
|
*
|
|
|
|
* @param \App\Events\NewsAdded $event
|
|
|
|
*/
|
|
|
|
public function onNewsAdded(NewsAdded $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onNewsAdded');
|
|
|
|
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
|
2021-06-04 22:51:59 +08:00
|
|
|
Notification::send([$event->news], new Messages\NewsAdded($event->news));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|