diff --git a/.env.example b/.env.example index 72c19df3..57ea1e36 100644 --- a/.env.example +++ b/.env.example @@ -52,5 +52,3 @@ TWITTER_ACCESS_TOKEN_SECRET= SCOUT_DRIVER=pgsql PIWIK=false - -PSYSH_CONFIG=tinker.config.php diff --git a/.psysh.php b/.psysh.php new file mode 100644 index 00000000..d237947d --- /dev/null +++ b/.psysh.php @@ -0,0 +1,3 @@ +> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + - psql -U travis -c 'create database travis_ci_test' + - psql -U travis -d travis_ci_test -c 'create extension postgis' - mkdir travis-phantomjs - wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 -O $PWD/travis-phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 - tar -xvf $PWD/travis-phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C $PWD/travis-phantomjs @@ -51,10 +55,6 @@ install: - travis/install-nginx.sh before_script: - - echo 'error_log = "/tmp/php.error.log"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - - psql -U travis -c 'create database travis_ci_test' - - psql -U travis -d travis_ci_test -c 'create extension postgis' - - cp .env.travis .env - php artisan key:generate - php artisan migrate - php artisan db:seed diff --git a/app/Place.php b/app/Place.php index 0e1e0c9b..b594f752 100644 --- a/app/Place.php +++ b/app/Place.php @@ -76,6 +76,20 @@ class Place extends Model return $query->where($field, '<=', $distance)->orderBy($field); } + public function scopeWhereExternalURL(Builder $query, string $url) + { + $type = $this->getType($url); + if ($type === null) { + // we haven’t set a type, therefore result must be empty set + // id can’t be null, so this will return empty set + return $query->whereNull('id'); + } + + return $query->where('external_urls', '@>', json_encode([ + $type => $url, + ])); + } + /** * Get the latitude from the `location` property. * @@ -115,4 +129,31 @@ class Place extends Model { return config('app.shorturl') . '/places/' . $this->slug; } + + public function setExternalUrlsAttribute($url) + { + $type = $this->getType($url); + if ($type === null) { + throw new \Exception('Unkown external url type ' . $url); + } + $already = []; + if (array_key_exists('external_urls', $this->attributes)) { + $already = json_decode($this->attributes['external_urls'], true); + } + $already[$type] = $url; + $this->attributes['external_urls'] = json_encode($already); + } + + private function getType(string $url): ?string + { + $host = parse_url($url, PHP_URL_HOST); + if (ends_with($host, 'foursquare.com') === true) { + return 'foursquare'; + } + if (ends_with($host, 'openstreetmap.org') === true) { + return 'osm'; + } + + return null; + } } diff --git a/app/Services/PlaceService.php b/app/Services/PlaceService.php index 95dd1418..2a823f33 100644 --- a/app/Services/PlaceService.php +++ b/app/Services/PlaceService.php @@ -46,10 +46,10 @@ class PlaceService public function createPlaceFromCheckin(array $checkin): Place { //check if the place exists if from swarm - if (array_key_exists('url', $checkin['properties']) && ends_with(parse_url($checkin['properties']['url'][0], PHP_URL_HOST), 'foursquare.com')) { - $place = Place::where('foursquare', $checkin['properties']['url'][0])->get(); + if (array_key_exists('url', $checkin['properties'])) { + $place = Place::whereExternalURL($checkin['properties']['url'][0])->get(); if (count($place) === 1) { - return $place; + return $place->first(); } } if (array_key_exists('name', $checkin['properties']) === false) { @@ -60,9 +60,7 @@ class PlaceService } $place = new Place(); $place->name = $checkin['properties']['name'][0]; - if (ends_with(parse_url($checkin['properties']['url'][0], PHP_URL_HOST), 'foursquare.com')) { - $place->foursquare = $checkin['properties']['url'][0]; - } + $place->external_urls = $checkin['properties']['url'][0]; $place->location = new Point( (float) $checkin['properties']['latitude'][0], (float) $checkin['properties']['longitude'][0] diff --git a/changelog.md b/changelog.md index 29a0c216..186985ca 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version 0.5.20 (2017-06-30) + - Transition to using a JSON column for external urls of places + ## Version 0.5.19 (2017-06-27) - Fix error in App\\WebMention.php diff --git a/composer.json b/composer.json index a29172c5..9a4384a1 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "predis/predis": "~1.0", "ramsey/uuid": "^3.5", "sensiolabs/security-checker": "^4.0", + "spatie/laravel-tinker-tools": "^1.0", "thujohn/twitter": "~2.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index aa0c6e31..0c532174 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "af5dc6e36b50396acecb6c17d3392645", + "content-hash": "17da475fc29dcd3b362b3e0d21c312d9", "packages": [ { "name": "aws/aws-sdk-php", - "version": "3.30.1", + "version": "3.30.2", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "d234cb5e111945eb4bfca7eda0eef07a25750b29" + "reference": "20539a38dc643a2700f80ae8647dbd1a05d3f7a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/d234cb5e111945eb4bfca7eda0eef07a25750b29", - "reference": "d234cb5e111945eb4bfca7eda0eef07a25750b29", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/20539a38dc643a2700f80ae8647dbd1a05d3f7a9", + "reference": "20539a38dc643a2700f80ae8647dbd1a05d3f7a9", "shasum": "" }, "require": { @@ -84,7 +84,7 @@ "s3", "sdk" ], - "time": "2017-06-22T22:40:58+00:00" + "time": "2017-06-23T18:54:40+00:00" }, { "name": "barnabywalters/mf-cleaner", @@ -2728,16 +2728,16 @@ }, { "name": "psy/psysh", - "version": "v0.8.7", + "version": "v0.8.8", "source": { "type": "git", "url": "https://github.com/bobthecow/psysh.git", - "reference": "be969b9dc89dcaefdb9a3117fa91fa38bca19f50" + "reference": "fe65c30cbc55c71e61ba3a38b5a581149be31b8e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bobthecow/psysh/zipball/be969b9dc89dcaefdb9a3117fa91fa38bca19f50", - "reference": "be969b9dc89dcaefdb9a3117fa91fa38bca19f50", + "url": "https://api.github.com/repos/bobthecow/psysh/zipball/fe65c30cbc55c71e61ba3a38b5a581149be31b8e", + "reference": "fe65c30cbc55c71e61ba3a38b5a581149be31b8e", "shasum": "" }, "require": { @@ -2797,7 +2797,7 @@ "interactive", "shell" ], - "time": "2017-06-20T12:51:31+00:00" + "time": "2017-06-24T06:16:19+00:00" }, { "name": "ramsey/uuid", @@ -2926,6 +2926,57 @@ "description": "A security checker for your composer.lock", "time": "2017-03-31T14:50:32+00:00" }, + { + "name": "spatie/laravel-tinker-tools", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-tinker-tools.git", + "reference": "4674f9a3d874c8ad488e25ffb7cd936bb7348e43" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-tinker-tools/zipball/4674f9a3d874c8ad488e25ffb7cd936bb7348e43", + "reference": "4674f9a3d874c8ad488e25ffb7cd936bb7348e43", + "shasum": "" + }, + "require": { + "illuminate/support": "5.3.*|5.4.*", + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\TinkerTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Flysystem Adapter for the Dropbox v2 API", + "homepage": "https://github.com/spatie/laravel-tinker-tools", + "keywords": [ + "Flysystem", + "api", + "dropbox", + "laravel-tinker-tools", + "spatie", + "v2" + ], + "time": "2017-05-22T11:30:34+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v5.4.8", diff --git a/database/migrations/2017_06_27_164743_update_places_table_add_external_urls.php b/database/migrations/2017_06_27_164743_update_places_table_add_external_urls.php new file mode 100644 index 00000000..bd58db29 --- /dev/null +++ b/database/migrations/2017_06_27_164743_update_places_table_add_external_urls.php @@ -0,0 +1,34 @@ +jsonb('external_urls')->nullable(); + $table->index('external_urls'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('places', function (Blueprint $table) { + $table->dropIndex('places_external_urls_index'); + $table->dropColumn('external_urls'); + }); + } +} diff --git a/database/seeds/PlacesTableSeeder.php b/database/seeds/PlacesTableSeeder.php index 1fe4bea8..4b787536 100644 --- a/database/seeds/PlacesTableSeeder.php +++ b/database/seeds/PlacesTableSeeder.php @@ -1,6 +1,8 @@ insert([ - 'name' => 'The Bridgewater Pub', - 'slug' => 'the-bridgewater-pub', - 'description' => 'A lovely local pub with a decent selection of cask ales', - 'location' => 'POINT(-2.3805 53.4983)', - 'created_at' => '2016-01-12 16:19:00', - 'updated_at' => '2016-01-12 16:19:00', - ]); + $place = new Place(); + $place->name = 'The Bridgewater Pub'; + $place->description = 'A lovely local pub with a decent selection of cask ales'; + $place->location = new Point('53.4983', '-2.3805'); + $place->external_urls = 'https://foursquare.com/v/123435/the-bridgewater-pub'; + $place->external_urls = 'https://www.openstreetmap.org/way/987654'; + $place->save(); } } diff --git a/package-lock.json b/package-lock.json index 04964a83..91e22361 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4329,9 +4329,9 @@ } }, "webStorage": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/webStorage/-/webStorage-1.2.3.tgz", - "integrity": "sha1-CHN87eWk2ouophKKYoQHB0Cwxxg=" + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/webStorage/-/webStorage-1.2.4.tgz", + "integrity": "sha1-/jNN8N5uLe58i9A2uxVaw115FTY=" }, "webworkify": { "version": "1.4.0", diff --git a/package.json b/package.json index 1cfbde8b..3a5f43dc 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "mapbox-gl": "^0.38.0", "marked": "^0.3.6", "normalize.css": "^7.0.0", - "webStorage": "^1.2.3" + "webStorage": "^1.2.4" }, "devDependencies": { "babel-cli": "^6.18.0", diff --git a/tests/Feature/SwarmTest.php b/tests/Feature/SwarmTest.php index 16ae8cbe..9f395adf 100644 --- a/tests/Feature/SwarmTest.php +++ b/tests/Feature/SwarmTest.php @@ -44,7 +44,7 @@ class SwarmTest extends TestCase ->assertStatus(201) ->assertJson(['response' => 'created']); $this->assertDatabaseHas('places', [ - 'foursquare' => 'https://foursquare.com/v/123456' + 'external_urls' => '{"foursquare": "https://foursquare.com/v/123456"}' ]); $this->assertDatabaseHas('notes', [ 'swarm_url' => 'https://www.swarmapp.com/checkin/abc' diff --git a/tinker.config.php b/tinker.config.php deleted file mode 100644 index 63111c79..00000000 --- a/tinker.config.php +++ /dev/null @@ -1,38 +0,0 @@ -files()->name('*.php')->in(base_path().'/app'); - foreach ($finder as $file) { - $namespace = 'App\\'; - if ($relativePath = $file->getRelativePath()) { - $namespace .= strtr($relativePath, '/', '\\') . '\\'; - } - $class = $namespace . $file->getBasename('.php'); - try { - $r = new \ReflectionClass($class); - if ($r->isSubclassOf('Illuminate\\Database\\Eloquent\\Model')) { - class_alias($class, $file->getBasename('.php')); - } - } catch (Exception $e) { - // - } - } - } -} - -aliasModels(); - -return [ - 'startupMessage' => 'Using local config file (tinker.config.php)', - - 'commands' => [ - // new \App\Tinker\TestCommand, - ], -];