From f95ca8578264799c6007e95eff384b1b829d679f Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 28 Jan 2018 16:04:39 -0800 Subject: [PATCH] change "ping URLs" to "web hooks" and send more events sends an event when a trip is started or ended new location web hook also includes active trip closes #22 --- compass/app/Http/Controllers/Api.php | 8 ++++ compass/app/Jobs/NotifyOfNewLocations.php | 11 ++++- compass/app/Jobs/TripComplete.php | 37 +++++++++++++++++ compass/app/Jobs/TripStarted.php | 47 ++++++++++++++++++++++ compass/resources/views/settings.blade.php | 12 ++++-- 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 compass/app/Jobs/TripStarted.php diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index c6d5f16..e49b98e 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -8,6 +8,7 @@ use DB, Log, Cache; use Quartz; use DateTime, DateTimeZone, DateInterval; use App\Jobs\TripComplete; +use App\Jobs\TripStarted; use App\Jobs\NotifyOfNewLocations; class Api extends BaseController @@ -347,10 +348,17 @@ class Api extends BaseController } if($request->input('trip')) { + $existing_trip = $db->current_trip; + DB::table('databases')->where('id', $db->id) ->update([ 'current_trip' => json_encode($request->input('trip'), JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT) ]); + + if(!$existing_trip && $db->ping_urls) { + $job = (new TripStarted($db->id))->onQueue('compass'); + $this->dispatch($job); + } } else { DB::table('databases')->where('id', $db->id)->update(['current_trip' => null]); } diff --git a/compass/app/Jobs/NotifyOfNewLocations.php b/compass/app/Jobs/NotifyOfNewLocations.php index 5f71ad1..f436b28 100644 --- a/compass/app/Jobs/NotifyOfNewLocations.php +++ b/compass/app/Jobs/NotifyOfNewLocations.php @@ -19,6 +19,15 @@ class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue $db = DB::table('databases')->where('id','=',$this->_dbid)->first(); $urls = preg_split('/\s+/', $db->ping_urls); + $location = [ + 'location' => json_decode($db->last_location, true) + ]; + + if($db->current_trip) + $location['trip'] = json_decode($db->current_trip, true); + + $location = json_encode($location, JSON_UNESCAPED_SLASHES); + foreach($urls as $url) { if(trim($url)) { $ch = curl_init($url); @@ -28,7 +37,7 @@ class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue 'Authorization: Bearer '.$db->read_token, 'Compass-Url: '.env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1' ]); - curl_setopt($ch, CURLOPT_POSTFIELDS, $db->last_location); + curl_setopt($ch, CURLOPT_POSTFIELDS, $location); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); $timestamp = ''; diff --git a/compass/app/Jobs/TripComplete.php b/compass/app/Jobs/TripComplete.php index 511500b..005958a 100644 --- a/compass/app/Jobs/TripComplete.php +++ b/compass/app/Jobs/TripComplete.php @@ -31,6 +31,43 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue Log::debug(json_encode($this->_data)); + ////////////////////////////////// + // Web Hooks + + $urls = preg_split('/\s+/', $db->ping_urls); + + // Build a trip object that looks like the active trip format from Overland + $trip = [ + 'trip' => [ + 'mode' => $this->_data['properties']['mode'], + 'start_location' => $this->_data['properties']['start_location'], + 'end_location' => $this->_data['properties']['end_location'], + 'distance' => $this->_data['properties']['distance'], + 'start' => $this->_data['properties']['start'], + 'end' => $this->_data['properties']['end'] + ] + ]; + $trip = json_encode($trip, JSON_UNESCAPED_SLASHES); + + foreach($urls as $url) { + if(trim($url)) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer '.$db->read_token, + 'Compass-Url: '.env('BASE_URL').'api/trip?token='.$db->read_token + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, $trip); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_exec($ch); + Log::info("Notifying ".$url." of a completed trip"); + } + } + + ////////////////////////////////// + // Micropub + if(!$db->micropub_endpoint) { Log::info('No micropub endpoint configured for database ' . $db->name); return; diff --git a/compass/app/Jobs/TripStarted.php b/compass/app/Jobs/TripStarted.php new file mode 100644 index 0000000..f126165 --- /dev/null +++ b/compass/app/Jobs/TripStarted.php @@ -0,0 +1,47 @@ +_dbid = $dbid; + } + + public function handle() { + $db = DB::table('databases')->where('id','=',$this->_dbid)->first(); + + $urls = preg_split('/\s+/', $db->ping_urls); + + $trip = [ + 'trip' => json_decode($db->current_trip, true) + ]; + $trip = json_encode($trip, JSON_UNESCAPED_SLASHES); + + foreach($urls as $url) { + if(trim($url)) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer '.$db->read_token, + 'Compass-Url: '.env('BASE_URL').'api/trip?token='.$db->read_token + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, $trip); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_exec($ch); + Log::info("Notifying ".$url." of a new trip"); + } + } + } +} diff --git a/compass/resources/views/settings.blade.php b/compass/resources/views/settings.blade.php index 539c4c5..9c2a33a 100644 --- a/compass/resources/views/settings.blade.php +++ b/compass/resources/views/settings.blade.php @@ -84,11 +84,11 @@
-

Realtime Micropub Export

+

Micropub Trip Export

@if (empty($database->micropub_token)) -

Authorize Compass with a micropub endpoint and any trips that are written to this database will be sent to that endpoint as well.

+

Authorize Compass with a Micropub endpoint and any trips that are written to this database will be sent to that endpoint as well.

@@ -110,9 +110,13 @@
-

Ping on New Location

+

Web Hooks

-

Enter one or more URLs to ping when new location data is available. This will send a POST request to the URLs with the URL to fetch the last location from the database, e.g. url=https://compass.p3k.io/api/last?token=xxxx. Enter one or more URLs separated by whitespace.

+

Enter one or more URLs to ping when events occur. This will send a POST request to each URL configured when new a location is received, or a trip is started or ended.

+ +

The POST body will be JSON encoded and will contain either a top-level property location or trip. If the trip has started, then the trip object will contain a current_location property. If the trip has ended, the trip object will contain a end_location property.

+ +

Enter one or more URLs separated by whitespace.