From 6fd5ad2bf1cf90948cbfd5613fbcd5dda94d8793 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 21 May 2017 11:09:31 +0200 Subject: [PATCH 1/3] skip importing trips that are too short --- compass/app/Jobs/TripComplete.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compass/app/Jobs/TripComplete.php b/compass/app/Jobs/TripComplete.php index f2a9d99..874a800 100644 --- a/compass/app/Jobs/TripComplete.php +++ b/compass/app/Jobs/TripComplete.php @@ -126,6 +126,11 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue $endDate->setTimeZone(new DateTimeZone($end->timezone)); } + if($endDate->format('U') - $startDate->format('U') < 15) { + Log::info("Skipping trip since it was too short"); + return; + } + $params = [ 'h' => 'entry', 'published' => $endDate->format('c'), From 3b1496c317f719d2efd4c2babfa72c495efafa84 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 21 May 2017 11:10:21 +0200 Subject: [PATCH 2/3] gitignore --- .gitignore | 1 + compass/.gitignore | 1 + 2 files changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/compass/.gitignore b/compass/.gitignore index 24ba008..58dd97b 100644 --- a/compass/.gitignore +++ b/compass/.gitignore @@ -1,3 +1,4 @@ /vendor .env .DS_Store +data/ From 62ad13065d254d5ff9a4be39784c7d14c33af491 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 21 May 2017 11:54:25 +0200 Subject: [PATCH 3/3] 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.

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