From 6978510147d9cf9dcdffd5c9b5b211639aacbd2d Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 30 Jun 2026 21:42:51 +1000 Subject: [PATCH] Added sockets for notifications --- .../Commands/UpdateDepartedFlights.php | 13 ++++----- app/Events/NotificationCreated.php | 1 - app/Models/UserFlight.php | 16 +++++++++-- ...6_30_105446_add_departure_check_column.php | 27 +++++++++++++++++++ docker/entrypoint.sh | 1 + resources/js/bootstrap.js | 12 +++++---- routes/web.php | 9 ++++++- 7 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 database/migrations/2026_06_30_105446_add_departure_check_column.php diff --git a/app/Console/Commands/UpdateDepartedFlights.php b/app/Console/Commands/UpdateDepartedFlights.php index f480283..093c414 100644 --- a/app/Console/Commands/UpdateDepartedFlights.php +++ b/app/Console/Commands/UpdateDepartedFlights.php @@ -21,20 +21,17 @@ class UpdateDepartedFlights extends Command { $now = now()->utc(); - $now = now()->utc(); - $userFlights = UserFlight::where('departure_date', '<=', $now->toDateTimeString()) - ->where('departure_date', '>', $now->copy()->subMinute()->toDateTimeString()) + ->where(function ($query) { + $query->whereNull('departure_processed_at') + ->orWhereColumn('departure_processed_at', '<', 'departure_date'); + }) ->get(); $this->info("Found {$userFlights->count()} flights."); foreach ($userFlights as $flight) { - $flight->touch(); + $flight->update(['departure_processed_at' => $now]); } - - $this->info("Touching flight {$flight->id}"); - $result = $flight->touch(); - $this->info("Touch result: " . var_export($result, true)); } } diff --git a/app/Events/NotificationCreated.php b/app/Events/NotificationCreated.php index 699d2b7..41861ce 100644 --- a/app/Events/NotificationCreated.php +++ b/app/Events/NotificationCreated.php @@ -5,7 +5,6 @@ namespace App\Events; use App\Models\Notification; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; -use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illuminate\Queue\SerializesModels; class NotificationCreated implements ShouldBroadcast diff --git a/app/Models/UserFlight.php b/app/Models/UserFlight.php index 8b572ce..4c569f0 100644 --- a/app/Models/UserFlight.php +++ b/app/Models/UserFlight.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; class UserFlight extends Model @@ -30,12 +31,14 @@ class UserFlight extends Model 'crew_type_id', 'note', 'auto_update', + 'departure_processed_at' ]; protected $casts = [ 'departure_date' => 'immutable_datetime', 'arrival_date' => 'immutable_datetime', 'auto_update' => 'boolean', + 'departure_processed_at' => 'datetime', ]; protected $appends = [ @@ -50,9 +53,17 @@ class UserFlight extends Model 'livery_url', 'scope', 'range', - 'region_range' + 'region_range', ]; + protected static function booted(): void + { + static::creating(function (UserFlight $flight) { + $flight->departure_processed_at = now(); + }); + } + + public function calculateGreatCircleDistance(): float{ $earthRadiusKm = 6371; [$depLat, $depLong] = [$this->departureAirport->latitude_deg, $this->departureAirport->longitude_deg]; @@ -182,7 +193,8 @@ class UserFlight extends Model return !$this->isDomestic(); } - public function otherUsersOnFlight(){ + public function otherUsersOnFlight(): Collection + { return UserFlight::where('user_id', '!=', $this->user_id) ->where('departure_airport_id', $this->departure_airport_id) ->where('arrival_airport_id', $this->arrival_airport_id) diff --git a/database/migrations/2026_06_30_105446_add_departure_check_column.php b/database/migrations/2026_06_30_105446_add_departure_check_column.php new file mode 100644 index 0000000..01a002a --- /dev/null +++ b/database/migrations/2026_06_30_105446_add_departure_check_column.php @@ -0,0 +1,27 @@ +timestamp('departure_processed_at')->nullable()->after('departure_date'); + }); + } + + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index fa5327b..823237f 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -6,5 +6,6 @@ php artisan config:cache php artisan migrate --force crond -f -l 8 & +php artisan reverb:start --host=0.0.0.0 --port=8080 & php-fpm -D nginx -g 'daemon off;' diff --git a/resources/js/bootstrap.js b/resources/js/bootstrap.js index 7397d90..d56d6ea 100644 --- a/resources/js/bootstrap.js +++ b/resources/js/bootstrap.js @@ -7,12 +7,14 @@ import Pusher from 'pusher-js'; window.Pusher = Pusher; +const { data } = await axios.get('/broadcasting/config'); + window.Echo = new Echo({ broadcaster: 'reverb', - key: import.meta.env.VITE_REVERB_APP_KEY, - wsHost: import.meta.env.VITE_REVERB_HOST, - wsPort: import.meta.env.VITE_REVERB_PORT ?? 80, - wssPort: import.meta.env.VITE_REVERB_PORT ?? 443, - forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https', + key: data.key, + wsHost: data.host, + wsPort: data.port, + wssPort: data.port, + forceTLS: data.scheme === 'https', enabledTransports: ['ws', 'wss'], }); diff --git a/routes/web.php b/routes/web.php index 9496d7e..9b0b2f0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,7 +20,14 @@ use Inertia\Inertia; Route::get('/', [UserProfileController::class, 'index'])->name('home'); Route::get('/splash', [HomePageController::class, 'splash'])->name('splash'); - + Route::get('/broadcasting/config', function () { + return response()->json([ + 'key' => config('broadcasting.connections.reverb.key'), + 'host' => config('broadcasting.connections.reverb.options.host'), + 'port' => config('broadcasting.connections.reverb.options.port'), + 'scheme' => config('broadcasting.connections.reverb.options.scheme'), + ]); + }); Route::get('/dashboard', function () { return Inertia::render('Dashboard');