Browse Source

load recent history from shared links

main
Aaron Parecki 2 years ago
parent
commit
c9c8e012c9
4 changed files with 74 additions and 3 deletions
  1. +1
    -1
      compass/app/Http/Controllers/Api.php
  2. +56
    -0
      compass/app/Http/Controllers/Share.php
  3. +1
    -0
      compass/app/Http/routes.php
  4. +16
    -2
      compass/public/assets/share.js

+ 1
- 1
compass/app/Http/Controllers/Api.php View File

@ -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

+ 56
- 0
compass/app/Http/Controllers/Share.php View File

@ -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');
}
}

+ 1
- 0
compass/app/Http/routes.php View File

@ -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');

+ 16
- 2
compass/public/assets/share.js View File

@ -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();

Loading…
Cancel
Save