Browse Source

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
pull/23/head
Aaron Parecki 6 years ago
parent
commit
f95ca85782
No known key found for this signature in database GPG Key ID: 276C2817346D6056
5 changed files with 110 additions and 5 deletions
  1. +8
    -0
      compass/app/Http/Controllers/Api.php
  2. +10
    -1
      compass/app/Jobs/NotifyOfNewLocations.php
  3. +37
    -0
      compass/app/Jobs/TripComplete.php
  4. +47
    -0
      compass/app/Jobs/TripStarted.php
  5. +8
    -4
      compass/resources/views/settings.blade.php

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

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

+ 10
- 1
compass/app/Jobs/NotifyOfNewLocations.php View File

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

+ 37
- 0
compass/app/Jobs/TripComplete.php View File

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

+ 47
- 0
compass/app/Jobs/TripStarted.php View File

@ -0,0 +1,47 @@
<?php
namespace App\Jobs;
use DB;
use Log;
use Quartz;
use p3k\Multipart;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use DateTime, DateTimeZone;
class TripStarted extends Job implements SelfHandling, ShouldQueue
{
private $_dbid;
public function __construct($dbid) {
$this->_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");
}
}
}
}

+ 8
- 4
compass/resources/views/settings.blade.php View File

@ -84,11 +84,11 @@
</div>
<br>
<h2>Realtime Micropub Export</h2>
<h2>Micropub Trip Export</h2>
<div class="panel">
@if (empty($database->micropub_token))
<p>Authorize Compass with a micropub endpoint and any trips that are written to this database will be sent to that endpoint as well.</p>
<p>Authorize Compass with a Micropub endpoint and any trips that are written to this database will be sent to that endpoint as well.</p>
<form action="/settings/{{ $database->name }}/auth/start" method="post" class="ui form">
<div class="field">
<label for="micropub_endpoint">web sign-in</label>
@ -110,9 +110,13 @@
<br>
<h2>Ping on New Location</h2>
<h2>Web Hooks</h2>
<p>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. <code>url=https://compass.p3k.io/api/last?token=xxxx</code>. Enter one or more URLs separated by whitespace.</p>
<p>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.</p>
<p>The POST body will be JSON encoded and will contain either a top-level property <code>location</code> or <code>trip</code>. If the trip has started, then the trip object will contain a <code>current_location</code> property. If the trip has ended, the trip object will contain a <code>end_location</code> property.</p>
<p>Enter one or more URLs separated by whitespace.</p>
<div class="panel">
<form action="/settings/{{ $database->name }}" method="post" class="ui form">

Loading…
Cancel
Save