From 62ad13065d254d5ff9a4be39784c7d14c33af491 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 21 May 2017 11:54:25 +0200 Subject: [PATCH] add option to ping a URL when new location data is received --- compass/app/Http/Controllers/Api.php | 8 +++++ compass/app/Http/Controllers/Controller.php | 7 +++++ compass/app/Jobs/NotifyOfNewLocations.php | 31 +++++++++++++++++++ .../2017_05_21_093158_add_ping_urls.php | 29 +++++++++++++++++ compass/resources/views/settings.blade.php | 19 ++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 compass/app/Jobs/NotifyOfNewLocations.php create mode 100644 compass/database/migrations/2017_05_21_093158_add_ping_urls.php diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index 2dbfc83..8569264 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -258,6 +258,14 @@ class Api extends BaseController } } + if($num > 0) { + // Notify subscribers that new data is available + if($db->ping_urls) { + $job = (new NotifyOfNewLocations($db->id))->onQueue('compass'); + $this->dispatch($job); + } + } + return response(json_encode(['result' => 'ok', 'saved' => $num, 'trips' => $trips]))->header('Content-Type', 'application/json'); } diff --git a/compass/app/Http/Controllers/Controller.php b/compass/app/Http/Controllers/Controller.php index 9f87c29..77b4f7f 100644 --- a/compass/app/Http/Controllers/Controller.php +++ b/compass/app/Http/Controllers/Controller.php @@ -181,6 +181,13 @@ class Controller extends BaseController 'micropub_token' => $request->input('micropub_token'), ]); + return redirect('/settings/'.$db->name); + } else if($request->input('ping_urls')) { + DB::table('databases')->where('id', $db->id) + ->update([ + 'ping_urls' => $request->input('ping_urls'), + ]); + return redirect('/settings/'.$db->name); } } diff --git a/compass/app/Jobs/NotifyOfNewLocations.php b/compass/app/Jobs/NotifyOfNewLocations.php new file mode 100644 index 0000000..52e49d4 --- /dev/null +++ b/compass/app/Jobs/NotifyOfNewLocations.php @@ -0,0 +1,31 @@ +_dbid = $dbid; + } + + public function handle() { + $db = DB::table('databases')->where('id','=',$this->_dbid)->first(); + $urls = preg_split('/\s+/', $db->ping_urls); + foreach($urls as $url) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ + 'url' => env('BASE_URL').'api/last?token='.$db->token.'&geocode=1' + ])); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_exec($ch); + } + } +} diff --git a/compass/database/migrations/2017_05_21_093158_add_ping_urls.php b/compass/database/migrations/2017_05_21_093158_add_ping_urls.php new file mode 100644 index 0000000..4a381f8 --- /dev/null +++ b/compass/database/migrations/2017_05_21_093158_add_ping_urls.php @@ -0,0 +1,29 @@ +string('ping_urls', 1024); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/compass/resources/views/settings.blade.php b/compass/resources/views/settings.blade.php index 43c9bb5..b334d00 100644 --- a/compass/resources/views/settings.blade.php +++ b/compass/resources/views/settings.blade.php @@ -80,6 +80,25 @@ +
+ +

Ping on New Location

+ +

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.

+ +
+
+
+ + +
+ + +
+
+ +
+