refactor location code to support h-cards from instagram

This commit is contained in:
Jonny Barnes 2017-06-13 18:10:51 +01:00
parent 87ccb1c93e
commit dece5c8d0a
2 changed files with 24 additions and 13 deletions

View file

@ -78,10 +78,20 @@ class MicropubController extends Controller
$data['content'] = $request->input('properties.content')[0]['html'];
}
$data['in-reply-to'] = $request->input('properties.in-reply-to')[0];
$data['location'] = $request->input('properties.location');
//flatten location if array
if (is_array($data['location'])) {
$data['location'] = $data['location'][0];
// check location is geo: string
if (is_string($request->input('properties.location.0'))) {
$data['location'] = $request->input('properties.location.0');
}
// check location is h-card
if (is_array($request->input('properties.location.0'))) {
if ($request->input('properties.location.0.type' === 'h-card')) {
try {
$place = $this->placeService->createPlaceFromCheckin($request->input('properties.location.0'));
$data['checkin'] = $place->longurl;
} catch (\Exception $e) {
//
}
}
}
$data['published'] = $request->input('properties.published')[0];
//create checkin place
@ -89,7 +99,8 @@ class MicropubController extends Controller
$data['checkin'] = $request->input('properties.checkin.0.properties.url.0');
$data['swarm-url'] = $request->input('properties.syndication.0');
try {
$this->placeService->createPlaceFromCheckin($request->input('properties.checkin.0'));
$place = $this->placeService->createPlaceFromCheckin($request->input('properties.checkin.0'));
$data['checkin'] = $place->longurl;
} catch (\Exception $e) {
$data['checkin'] = null;
}

View file

@ -41,15 +41,15 @@ class PlaceService
* Create a place from a h-card checkin, for exameple from OwnYourSwarm.
*
* @param array
* @return bool
* @return Place
*/
public function createPlaceFromCheckin(array $checkin): bool
public function createPlaceFromCheckin(array $checkin): Place
{
//check if the place exists if from swarm
if (array_key_exists('url', $checkin['properties'])) {
$search = Place::where('foursquare', $checkin['properties']['url'][0])->count();
if ($search === 1) {
return true;
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 (count($place) === 1) {
return $place;
}
}
if (array_key_exists('name', $checkin['properties']) === false) {
@ -60,7 +60,7 @@ class PlaceService
}
$place = new Place();
$place->name = $checkin['properties']['name'][0];
if (starts_with($checkin['properties']['url'][0], 'https://foursquare.com')) {
if (ends_with(parse_url($checkin['properties']['url'][0], PHP_URL_HOST), 'foursquare.com')) {
$place->foursquare = $checkin['properties']['url'][0];
}
$place->location = new Point(
@ -69,6 +69,6 @@ class PlaceService
);
$place->save();
return true;
return $place;
}
}