Where-in-the-World-is-Ben/routes/web.php

83 lines
3.5 KiB
PHP
Raw Normal View History

2022-08-23 19:11:43 +00:00
<?php
use GuzzleHttp\Client;
use Illuminate\Http\Request;
2022-10-27 16:32:08 +00:00
use Illuminate\Support\Facades\Log;
2022-08-23 19:11:43 +00:00
use Illuminate\Support\Facades\Route;
2022-10-05 19:30:39 +00:00
use Illuminate\Support\Facades\Storage;
2022-10-27 21:40:38 +00:00
use App\Mail\WeeklyDigest;
use Illuminate\Support\Facades\Mail;
2022-08-23 19:11:43 +00:00
2022-10-19 22:53:52 +00:00
Route::get('/past-trips', function () {
return view('past-trips');
});
Route::get('/{tripId?}', function (Request $request, $tripId = null) {
2022-10-27 21:40:38 +00:00
//Mail::to('me@bengoldsworthy.net')->send(new WeeklyDigest());
2022-10-19 22:48:11 +00:00
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
$viewMode = $request->input('show', null);
2022-10-27 16:32:08 +00:00
// If there is a file in the local cache that is less than 13 hours old
// (depending on whether it was actively tracking at last check), use that.
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
2022-10-27 16:32:08 +00:00
2022-10-19 22:48:11 +00:00
if (Storage::disk('local')->exists($tripFileName)) {
2022-10-27 16:32:08 +00:00
Log::debug("Cached trip file '{$tripFileName}' found...");
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
if ($cachedData->is_active) {
Log::debug("Cached trip file '{$tripFileName}' is for an active trip.");
$cachedDataUpdatedAt = new DateTime($cachedData->updated_at);
$now = new DateTime();
2022-10-27 16:32:08 +00:00
$cachingTimeout = ($cachedData->is_tracking) ? 1 : 3;
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
if ($cachedDataAge <= $cachingTimeout) {
Log::debug("Cached trip file '{$tripFileName}' is younger than {$cachingTimeout} hours, showing from cache...");
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
}
} else {
2022-10-27 16:32:08 +00:00
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
2022-10-05 19:30:39 +00:00
}
}
2022-10-19 22:48:11 +00:00
/*
* Otherwise, download the latest tracking data.
*/
2022-10-27 16:32:08 +00:00
Log::debug("No cached trip file found for '{$tripFileName}'.");
2022-10-05 19:30:39 +00:00
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
'timeout' => 3.0
]);
2022-10-19 22:48:11 +00:00
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
2022-10-05 19:30:39 +00:00
if ($response->getStatusCode() == 200) {
$data = json_decode($response->getBody());
2022-10-19 22:48:11 +00:00
// Cache the downloaded file if it does not exist locally.
if (Storage::disk('local')->missing($tripFileName)) {
2022-10-27 16:32:08 +00:00
Log::debug("Caching new trip file '{$tripFileName}'.");
2022-10-19 22:48:11 +00:00
Storage::disk('local')->put($tripFileName, json_encode($data));
2022-09-21 19:40:14 +00:00
} else {
2022-10-19 22:48:11 +00:00
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
2022-10-05 19:30:39 +00:00
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
2022-10-27 16:32:08 +00:00
Log::debug("Cached trip file '{$tripFileName}' has different 'updated_at' time, updating cache...");
2022-10-19 22:48:11 +00:00
Storage::disk('local')->put($tripFileName, json_encode($data));
2022-10-05 19:30:39 +00:00
// TODO: Cache photos locally
} else {
2022-10-27 16:32:08 +00:00
Log::debug("Cached trip file '{$tripFileName}' has same 'updated_at' time, showing from cache...");
2022-10-05 19:30:39 +00:00
$data = $cachedData;
}
2022-09-21 19:40:14 +00:00
}
2022-10-05 19:30:39 +00:00
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
2022-09-21 19:40:14 +00:00
} else {
2022-10-27 16:32:08 +00:00
// TODO: Return proper error
2022-10-05 19:30:39 +00:00
return 'Something went wrong';
2022-09-21 19:40:14 +00:00
}
})->where('tripId', '[0-9a-z]{20}');