From c9c8e012c92bc57ab137c7eee52a7dd15ecdffa0 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Tue, 25 Oct 2022 17:58:32 +0000 Subject: [PATCH] load recent history from shared links --- compass/app/Http/Controllers/Api.php | 2 +- compass/app/Http/Controllers/Share.php | 56 ++++++++++++++++++++++++++ compass/app/Http/routes.php | 1 + compass/public/assets/share.js | 18 ++++++++- 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index 3812ec4..cf0041a 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -63,7 +63,7 @@ class Api extends BaseController foreach($results as $id=>$record) { // When returning a linestring, separate out the "event" records from the "location" records - if($record->data) { + if(is_object($record) && $record->data) { if(property_exists($record->data->properties, 'action')) { $rec = $record->data; # add a unixtime property diff --git a/compass/app/Http/Controllers/Share.php b/compass/app/Http/Controllers/Share.php index db3d2fc..8df8a74 100644 --- a/compass/app/Http/Controllers/Share.php +++ b/compass/app/Http/Controllers/Share.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Laravel\Lumen\Routing\Controller as BaseController; use Illuminate\Http\Request; use DB; +use Quartz; class Share extends BaseController { @@ -38,11 +39,66 @@ class Share extends BaseController public function current_location(Request $request) { $database = $this->_databaseFromToken($request->input('token')); + if(!$database) { + return response(json_encode(['error' => 'invalid']))->header('Content-Type', 'application/json'); + } + $response = [ 'data' => json_decode($database->last_location), ]; return response(json_encode($response))->header('Content-Type', 'application/json'); } + + public function history(Request $request) { + $database = $this->_databaseFromToken($request->input('token')); + + if(!$database) { + return response(json_encode(['error' => 'invalid']))->header('Content-Type', 'application/json'); + } + + $share = DB::table('shares') + ->where('token', $request->input('token')) + ->first(); + $share_date = strtotime($share->created_at); + + $locations = []; + + $db = new Quartz\DB(env('STORAGE_DIR').$database->name, 'r'); + $results = $db->queryLast(100); + foreach($results as $id=>$record) { + if(!is_object($record) || !$record->data) + continue; + + if(!property_exists($record->data->properties, 'horizontal_accuracy') + || $record->data->properties->horizontal_accuracy >= 5000) + continue; + + // Make sure this is from after the share was created + $record_date = $record->date->format('U'); + + if($record_date < $share_date) + continue; + + $locations[] = $record->data; + } + + $linestring = array( + 'type' => 'LineString', + 'coordinates' => [], + ); + foreach($locations as $loc) { + if(property_exists($loc, 'geometry')) + $linestring['coordinates'][] = $loc->geometry->coordinates; + else + $linestring['coordinates'][] = null; + } + + $response = array( + 'linestring' => $linestring, + ); + + return response(json_encode($response))->header('Content-Type', 'application/json'); + } } diff --git a/compass/app/Http/routes.php b/compass/app/Http/routes.php index c5ee012..a1d04ce 100644 --- a/compass/app/Http/routes.php +++ b/compass/app/Http/routes.php @@ -4,6 +4,7 @@ $app->get('/', 'Controller@index'); $app->get('/s/{token:[A-Za-z0-9]+}', 'Share@view'); $app->get('/share/current.json', 'Share@current_location'); +$app->get('/share/history.json', 'Share@history'); $app->post('/auth/start', 'IndieAuth@start'); $app->get('/auth/callback', 'IndieAuth@callback'); diff --git a/compass/public/assets/share.js b/compass/public/assets/share.js index 909ea74..8225b24 100644 --- a/compass/public/assets/share.js +++ b/compass/public/assets/share.js @@ -35,6 +35,7 @@ var startIcon = L.icon({ var currentLocationMarker; var currentTrack; +var lastSeenTimestamp; function getCurrentLocation() { var interval = 5000; @@ -46,14 +47,27 @@ function getCurrentLocation() { $.getJSON("/share/current.json?token="+$("#share_token").val(), function(data){ if(data.data) { moveMarkerToPosition(data.data); - map.setView(currentLocationMarker.getLatLng()); + if(lastSeenTimestamp != data.data.properties.timestamp) { + map.setView(currentLocationMarker.getLatLng()); + } + lastSeenTimestamp = data.data.properties.timestamp; } setTimeout(getCurrentLocation, interval); }); } -getCurrentLocation(); +function getRecentHistory() { + $.getJSON("/share/history.json?token="+$("#share_token").val(), function(data){ + if(data.linestring) { + L.geoJson(data.linestring, { + style: geojsonLineOptions + }).addTo(map); + } + }); +} +getCurrentLocation(); +getRecentHistory();