Added native os notifications

This commit is contained in:
2026-07-02 00:33:28 +10:00
parent 84379baf6a
commit 027437dc9b
13 changed files with 451 additions and 117 deletions
+64 -10
View File
@@ -9,7 +9,9 @@ use App\Models\CrewType;
use App\Models\FlightClass;
use App\Models\FlightReason;
use App\Models\IataEquipmentCode;
use App\Models\Notification;
use App\Models\SeatType;
use App\Models\User;
use App\Models\UserAction;
use App\Models\UserFlight;
use App\Services\FlightStatsService;
@@ -246,10 +248,41 @@ class FlightController extends Controller
],
]);
$this->notifyFollowersOnCreation($newFlight);
return redirect()->route('profile.departure-board', [Auth::user()->name, $newFlight->id]);
}
protected function notifyFollowersOnCreation(UserFlight $flight): void
{
$user = $flight->user;
$isFuture = $flight->departure_date->isFuture();
$title = $isFuture
? "{$user->name} booked a new flight"
: "{$user->name} logged a new flight";
$flightNumber = $flight->flight_number ? "{$flight->flight_number} from" : 'From';
$body = "{$flightNumber} {$flight->departureAirport->municipality} ({$flight->departureAirport->display_code}) → {$flight->arrivalAirport->municipality} ({$flight->arrivalAirport->display_code}) on {$flight->departure_date_display}";
$url = route('profile.departure-board', [$user->name, $flight->id]);
$user->followers()->get()->each(function (User $follower) use ($title, $body, $url, $isFuture) {
$notifyForFuture = $follower->getSetting('notify_on_upcoming_flight_added');
$notifyForHistoric = $follower->getSetting('notify_on_historic_flight_added');
if (($isFuture && $notifyForFuture) || (!$isFuture && $notifyForHistoric)) {
Notification::create([
'user_id' => $follower->id,
'title' => $title,
'body' => $body,
'url' => $url,
]);
}
}
);
}
public function update(Request $request, UserFlight $flight)
@@ -280,25 +313,46 @@ class FlightController extends Controller
$this->authorize('delete', $flight);
$snapshot = $flight->snapshot($flight->id);
if(now()->utc()->isBefore($flight->departure_date)){
$action = 'flight_deleted';
} else {
$action = 'flight_cancelled';
}
$isFuture = now()->utc()->isBefore($flight->departure_date);
UserAction::create([
'user_id' => $flight->user_id,
'type' => $action,
'data' => [
'user_id' => $flight->user_id,
'type' => $isFuture ? 'flight_cancelled' : 'flight_deleted',
'data' => [
'flight' => $snapshot,
]
],
]);
if ($isFuture) {
$this->notifyFollowersOnCancellation($flight);
}
$flight->delete();
return redirect()->route('profile.'.$referrer, [Auth::user()->name]);
}
protected function notifyFollowersOnCancellation(UserFlight $flight): void
{
$user = $flight->user;
$title = "{$user->name} cancelled a flight";
$flightNumber = $flight->flight_number ? "{$flight->flight_number} from" : 'From';
$body = "{$flightNumber} {$flight->departureAirport->municipality} ({$flight->departureAirport->display_code}) → {$flight->arrivalAirport->municipality} ({$flight->arrivalAirport->display_code}) on {$flight->departure_date_display}";
$url = route('profile.view', [$user->name]);
$user->followers()->get()->each(function (User $follower) use ($title, $body, $url) {
if ($follower->getSetting('notify_on_flight_cancellation')) {
Notification::create([
'user_id' => $follower->id,
'title' => $title,
'body' => $body,
'url' => $url,
]);
}
});
}
public function staticData() : array {
return [
'seat_types' => SeatType::orderBy('id')->get()->toArray(),