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(),
@@ -8,7 +8,9 @@ use App\Models\Airport;
use App\Models\FlightClass;
use App\Models\FlightReason;
use App\Models\ImportedFlight;
use App\Models\Notification;
use App\Models\SeatType;
use App\Models\User;
use App\Models\UserAction;
use App\Models\UserFlight;
use Carbon\Carbon;
@@ -268,10 +270,43 @@ class FlightImportController extends Controller
],
]);
$this->notifyFollowersOnCreation($newFlight);
ImportedFlight::destroy($validated['imported_flight_id']);
return to_route('reconcile');
}
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,
]);
}
}
);
}
private function validateCsvFormat(string $path): ?string
{
+19 -9
View File
@@ -38,6 +38,14 @@ class FollowerController extends Controller
if ($existing) {
$existing->delete();
$this->clearFollowingCache(auth()->user());
Notification::where('user_id', $user->id)
->whereIn('title', ['New follower', 'Follow request'])
->where('url', $canView ?? true
? '/u/' . auth()->user()->name
: '/follow-requests')
->delete();
return response()->json(['status' => 'none']);
}
@@ -51,15 +59,17 @@ class FollowerController extends Controller
$this->clearFollowingCache(auth()->user());
Notification::create([
'user_id' => $user->id,
'title' => $canView ? 'New follower' : 'Follow request',
'body' => $canView
? auth()->user()->name . ' is now following you.'
: auth()->user()->name . ' wants to follow you.',
'is_achievement' => false,
'url' => $canView ? '/u/' . auth()->user()->name : '/follow-requests',
]);
if($user->getSetting('notify_on_new_follower')) {
Notification::create([
'user_id' => $user->id,
'title' => $canView ? 'New follower' : 'Follow request',
'body' => $canView
? auth()->user()->name . ' is now following you.'
: auth()->user()->name . ' wants to follow you.',
'is_achievement' => false,
'url' => $canView ? '/u/' . auth()->user()->name : '/follow-requests',
]);
}
return response()->json(['status' => $canView ? 'following' : 'requested']);
}