-
Notifications
You must be signed in to change notification settings - Fork 144
/
NotificationEventsHandler.php
320 lines (284 loc) · 10.4 KB
/
NotificationEventsHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
namespace App\Notifications;
use App\Contracts\Listener;
use App\Events\AwardAwarded;
use App\Events\NewsAdded;
use App\Events\NewsUpdated;
use App\Events\PirepAccepted;
use App\Events\PirepFiled;
use App\Events\PirepPrefiled;
use App\Events\PirepRejected;
use App\Events\PirepStatusChange;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Models\Enums\PirepStatus;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Notifications\Messages\UserRejected;
use App\Notifications\Notifiables\Broadcast;
use Exception;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
/**
* Listen for different events and map them to different notifications
*/
class NotificationEventsHandler extends Listener
{
private static $broadcastNotifyable;
public static $callbacks = [
AwardAwarded::class => 'onAwardAwarded',
NewsAdded::class => 'onNewsAdded',
NewsUpdated::class => 'onNewsUpdated',
PirepPrefiled::class => 'onPirepPrefile',
PirepStatusChange::class => 'onPirepStatusChange',
PirepAccepted::class => 'onPirepAccepted',
PirepFiled::class => 'onPirepFile',
PirepRejected::class => 'onPirepRejected',
UserStateChanged::class => 'onUserStateChange',
UserStatsChanged::class => 'onUserStatsChanged',
Verified::class => 'onEmailVerified',
];
public function __construct()
{
static::$broadcastNotifyable = app(Broadcast::class);
}
/**
* Send a notification to all of the admins
*
* @param \App\Contracts\Notification $notification
*/
protected function notifyAdmins(\App\Contracts\Notification $notification)
{
$admin_users = User::whereHasRole('admin')->get();
foreach ($admin_users as $user) {
if (empty($user->email)) {
continue;
}
try {
// $this->notifyUser($user, $notification);
Notification::send([$user], $notification);
} catch (Exception $e) {
Log::emergency('Error emailing admin ('.$user->email.'). Error='.$e->getMessage());
}
}
}
/**
* @param User $user
* @param \App\Contracts\Notification $notification
*/
protected function notifyUser(User $user, \App\Contracts\Notification $notification)
{
if ($user->state === UserState::DELETED) {
return;
}
try {
$user->notify($notification);
} catch (Exception $e) {
Log::emergency('Error emailing user, '.$user->ident.'='.$user->email.', error='.$e->getMessage());
}
}
/**
* Send a notification to all users. Also can specify if a particular notification
* requires an opt-in
*
* @param \App\Contracts\Notification $notification
*/
protected function notifyAllUsers(\App\Contracts\Notification $notification)
{
$where = [];
if ($notification->requires_opt_in === true) { // If the opt-in is required
$where['opt_in'] = true;
}
/** @var Collection $users */
$users = User::where($where)->whereIn('state', [UserState::ACTIVE, UserState::ON_LEAVE])->get();
if (empty($users) || $users->count() === 0) {
return;
}
Log::info('Sending notification to '.$users->count().' users');
foreach ($users as $user) {
$this->notifyUser($user, $notification);
}
}
public function onEmailVerified(Verified $event): void
{
// Return if the user has any flights (email change / admin requests new verification)
if ($event->user->flights > 0) {
return;
}
Log::info('NotificationEvents::onUserRegister: '
.$event->user->ident.' is '
.UserState::label($event->user->state).', sending active email');
/*
* Send the user a confirmation email
*/
if ($event->user->state === UserState::ACTIVE) {
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
} elseif ($event->user->state === UserState::PENDING) {
$this->notifyUser($event->user, new Messages\UserPending($event->user));
}
/*
* Send all of the admins a notification that a new user registered
*/
$this->notifyAdmins(new Messages\AdminUserRegistered($event->user));
/*
* Broadcast notifications
*/
Notification::send([$event->user], new Messages\Broadcast\UserRegistered($event->user));
}
/**
* When a user's state changes, send an email out
*
* @param UserStateChanged $event
*/
public function onUserStateChange(UserStateChanged $event): void
{
Log::info('NotificationEvents::onUserStateChange: New user state='.$event->user->state);
if ($event->old_state === UserState::PENDING) {
if ($event->user->state === UserState::ACTIVE) {
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
} elseif ($event->user->state === UserState::REJECTED) {
$this->notifyUser($event->user, new UserRejected($event->user));
}
} elseif ($event->old_state === UserState::ACTIVE) {
Log::info('User state change from active to ??');
}
}
/**
* Prefile notification. Disabled intentionally, No need to send it to Discord
*/
public function onPirepPrefile(PirepPrefiled $event): void
{
Log::info('NotificationEvents::onPirepPrefile: '.$event->pirep->id.' prefiled');
/*
* Broadcast notifications
*/
// Notification::send([$event->pirep], new Messages\Broadcast\PirepPrefiled($event->pirep));
}
/**
* Status Change notification.
* Reduced the messages (Boarding, Pushback, TakeOff, Landing and non-normals only)
* If needed array can be tied to a setting at admin side for further customization
*/
public function onPirepStatusChange(PirepStatusChange $event): void
{
Log::info('NotificationEvents::onPirepStatusChange: '.$event->pirep->id.' status changed');
$message_types = [
PirepStatus::BOARDING,
PirepStatus::PUSHBACK_TOW,
PirepStatus::GRND_RTRN,
PirepStatus::TAKEOFF,
PirepStatus::LANDED,
PirepStatus::DIVERTED,
PirepStatus::CANCELLED,
PirepStatus::PAUSED,
PirepStatus::EMERG_DESCENT,
];
if (setting('notifications.discord_pirep_status', true) && in_array($event->pirep->status, $message_types, true)) {
Notification::send([$event->pirep], new Messages\Broadcast\PirepStatusChanged($event->pirep));
}
}
/**
* Notify the admins that a new PIREP has been filed
*
* @param PirepFiled $event
*/
public function onPirepFile(PirepFiled $event): void
{
Log::info('NotificationEvents::onPirepFile: '.$event->pirep->id.' filed');
if (setting('notifications.mail_pirep_admin', true)) {
$this->notifyAdmins(new Messages\PirepFiled($event->pirep));
}
/*
* Broadcast notifications
*/
if (setting('notifications.discord_pirep_filed', true)) {
Notification::send([$event->pirep], new Messages\Broadcast\PirepFiled($event->pirep));
}
}
/**
* Notify the user that their PIREP has been accepted
*
* @param \App\Events\PirepAccepted $event
*/
public function onPirepAccepted(PirepAccepted $event): void
{
if (setting('notifications.mail_pirep_user_ack', true)) {
Log::info('NotificationEvents::onPirepAccepted: '.$event->pirep->id.' accepted');
$this->notifyUser($event->pirep->user, new Messages\PirepAccepted($event->pirep));
}
}
/**
* Notify the user that their PIREP has been rejected
*
* @param \App\Events\PirepRejected $event
*/
public function onPirepRejected(PirepRejected $event): void
{
if (setting('notifications.mail_pirep_user_rej', true)) {
Log::info('NotificationEvents::onPirepRejected: '.$event->pirep->id.' rejected');
$this->notifyUser($event->pirep->user, new Messages\PirepRejected($event->pirep));
}
}
/**
* Notify all users of a news event, but only the users which have opted in
*
* @param \App\Events\NewsAdded $event
*/
public function onNewsAdded(NewsAdded $event): void
{
Log::info('NotificationEvents::onNewsAdded');
if (setting('notifications.mail_news', true)) {
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
}
/*
* Broadcast notifications
*/
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}
/**
* Notify all users of a news event, but only the users which have opted in
*
* @param \App\Events\NewsUpdated $event
*/
public function onNewsUpdated(NewsUpdated $event): void
{
Log::info('NotificationEvents::onNewsAdded');
if (setting('notifications.mail_news', true)) {
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
}
/*
* Broadcast notifications
*/
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}
/**
* Notify all users that user has awarded a new award
*
* @param \App\Events\AwardAwarded $event
*/
public function onAwardAwarded(AwardAwarded $event): void
{
/*
* Broadcast notifications
*/
if (setting('notifications.discord_award_awarded', true)) {
Notification::send([$event->userAward], new Messages\Broadcast\AwardAwarded($event->userAward));
}
}
/**
* Notify all users of a user rank change
*
* @param \App\Events\UserStatsChanged $event
*/
public function onUserStatsChanged(UserStatsChanged $event): void
{
/*
* Broadcast notifications
*/
if (setting('notifications.discord_user_rank_changed', true) && $event->stat_name === 'rank') {
Notification::send([$event->user], new Messages\Broadcast\UserRankChanged($event->user));
}
}
}