From 221f1f449d8d9ac9e835e8366ce29b9c59dbcd15 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 29 Jun 2016 15:28:23 +0100 Subject: [PATCH 1/6] Look for `syndicate-to` when deciding wether to syndicate to twitter --- app/Services/NoteService.php | 6 +++--- changelog.md | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index defa5330..4be6fe6d 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -48,11 +48,11 @@ class NoteService $this->dispatch(new SendWebMentions($note)); if (//micropub request, syndication sent as array - (is_array($request->input('mp-syndicate-to')) + (is_array($request->input('syndicate-to')) && - (in_array('twitter.com/jonnybarnes', $request->input('mp-syndicate-to'))) + (in_array('twitter.com/jonnybarnes', $request->input('syndicate-to'))) || //micropub request, syndication sent as string - ($request->input('mp-syndicate-to') == 'twitter.com/jonnybarnes') + ($request->input('syndicate-to') == 'twitter.com/jonnybarnes') || //local admin cp request ($request->input('twitter') == true)) ) { diff --git a/changelog.md b/changelog.md index e1251433..e718feb1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version {next} + - Fix an issue with dispatching the syndication job + ## Version 0.0.6.2 (2016-06-28) - Fix an issue with sending webmentions From 90e403a829d8ba22ecf5f95eda43aeff3953016b Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 29 Jun 2016 15:29:30 +0100 Subject: [PATCH 2/6] Bump version number --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index e718feb1..eadef67b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -## Version {next} +## Version 0.0.6.3 (2016-06-29) - Fix an issue with dispatching the syndication job ## Version 0.0.6.2 (2016-06-28) From 88b30bb8159620be809472e6ead43cdf3fb44f4e Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 29 Jun 2016 23:53:48 +0100 Subject: [PATCH 3/6] Use JSON response for q=syndicate-to request --- .../Controllers/MicropubClientController.php | 13 +++++------- app/Http/Controllers/MicropubController.php | 20 ++++++++++++++----- app/Services/NoteService.php | 4 ++-- changelog.md | 3 +++ tests/MicropubTest.php | 2 +- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/MicropubClientController.php b/app/Http/Controllers/MicropubClientController.php index 858822fb..60c714d4 100644 --- a/app/Http/Controllers/MicropubClientController.php +++ b/app/Http/Controllers/MicropubClientController.php @@ -37,9 +37,7 @@ class MicropubClientController extends Controller public function newNotePage(Request $request) { $url = $request->session()->get('me'); - $syndication = $this->parseSyndicationTargets( - $request->session()->get('syndication') - ); + $syndication = $request->session()->get('syndication'); return view('micropubnewnotepage', [ 'url' => $url, @@ -113,7 +111,7 @@ class MicropubClientController extends Controller return redirect('notes/new')->withErrors('Bad response when refreshing syndication targets', 'endpoint'); } $body = (string) $response->getBody(); - $syndication = str_replace(['&', '[]'], [';', ''], $body); + $syndication = $this->parseSyndicationTargets($body); $request->session()->put('syndication', $syndication); @@ -321,10 +319,9 @@ class MicropubClientController extends Controller return; } $syndicateTo = []; - $parts = explode(';', $syndicationTargets); - foreach ($parts as $part) { - $target = explode('=', $part); - $syndicateTo[] = urldecode($target[1]); + $data = json_decode($syndicationTargets, true); + foreach ($syndicateTo['syndicate-to'] as $syn) { + $syndicateTo[] = $syn['uid']; } if (count($syndicateTo) > 0) { return $syndicateTo; diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index b348ade1..3b180ee1 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -108,12 +108,22 @@ class MicropubController extends Controller } //we have a valid token, is `syndicate-to` set? if ($request->input('q') === 'syndicate-to') { - $content = http_build_query([ - 'syndicate-to' => 'twitter.com/jonnybarnes', + return response()->json([ + 'syndicate-to' => [[ + 'uid' => 'https://twitter.com/jonnybarnes', + 'name' => 'jonnybarnes on Twitter', + 'service' => [ + 'name' => 'Twitter', + 'url' => 'https://twitter.com', + 'photo' => 'https://upload.wikimedia.org/wikipedia/en/9/9f/Twitter_bird_logo_2012.svg', + ], + 'user' => [ + 'name' => 'jonnybarnes', + 'url' => 'https://twitter.com/jonnybarnes', + 'photo' => 'https://pbs.twimg.com/profile_images/1853565405/jmb-bw.jpg', + ], + ]], ]); - - return (new Response($content, 200)) - ->header('Content-Type', 'application/x-www-form-urlencoded'); } //nope, how about a geo URL? if (substr($request->input('q'), 0, 4) === 'geo:') { diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 4be6fe6d..087aa594 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -50,9 +50,9 @@ class NoteService if (//micropub request, syndication sent as array (is_array($request->input('syndicate-to')) && - (in_array('twitter.com/jonnybarnes', $request->input('syndicate-to'))) + (in_array('https://twitter.com/jonnybarnes', $request->input('syndicate-to'))) || //micropub request, syndication sent as string - ($request->input('syndicate-to') == 'twitter.com/jonnybarnes') + ($request->input('syndicate-to') == 'https://twitter.com/jonnybarnes') || //local admin cp request ($request->input('twitter') == true)) ) { diff --git a/changelog.md b/changelog.md index eadef67b..7009f734 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version {next} + - Use JSON for syndication endpoint query response + ## Version 0.0.6.3 (2016-06-29) - Fix an issue with dispatching the syndication job diff --git a/tests/MicropubTest.php b/tests/MicropubTest.php index 1854f3fb..cdc1e4a4 100644 --- a/tests/MicropubTest.php +++ b/tests/MicropubTest.php @@ -44,7 +44,7 @@ class MicropubTest extends TestCase public function testMicropubRequestForSyndication() { $this->call('GET', $this->appurl . '/api/post', ['q' => 'syndicate-to'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); - $this->see('twitter.com%2Fjonnybarnes'); + $this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']); } public function testMicropubRequestForNearbyPlacesThatExist() From 128f649722c05c87d990fa1fa96a0d135a7436d0 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 4 Jul 2016 13:53:33 +0100 Subject: [PATCH 4/6] Use JSON with micripub endpoint responses --- app/Http/Controllers/MicropubController.php | 89 +++++++++++++++------ tests/MicropubClientTest.php | 5 +- tests/MicropubTest.php | 6 +- 3 files changed, 71 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 3b180ee1..35df30fa 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -27,7 +27,7 @@ class MicropubController extends Controller protected $placeService; /** - * Injest the dependency. + * Inject the dependencies. */ public function __construct( TokenService $tokenService = null, @@ -59,31 +59,53 @@ class MicropubController extends Controller $type = $request->input('h'); if ($type == 'entry') { $note = $this->noteService->createNote($request, $clientId); - $content = 'Note created at ' . $note->longurl; + $content = <<longurl" +} +EOD; return (new Response($content, 201)) - ->header('Location', $note->longurl); + ->header('Location', $note->longurl) + ->header('Content-Type', 'application/json'); } if ($type == 'card') { $place = $this->placeService->createPlace($request); - $content = 'Place created at ' . $place->longurl; + $content = <<longurl" +} +EOD; return (new Response($content, 201)) - ->header('Location', $place->longurl); + ->header('Location', $place->longurl) + ->header('Content-Type', 'application/json'); } } } - $content = http_build_query([ - 'error' => 'invalid_token', - 'error_description' => 'The token provided is not valid or does not have the necessary scope', - ]); + $content = <<header('Content-Type', 'application/x-www-form-urlencoded'); + ->header('Content-Type', 'application/json'); } - $content = 'No OAuth token sent with request.'; + $content = <<header('Content-Type', 'application/json'); } /** @@ -104,7 +126,15 @@ class MicropubController extends Controller $valid = $this->tokenService->validateToken($token); if ($valid === null) { - return new Response('Invalid token', 400); + $content = <<header('Content-Type', 'application/json'); } //we have a valid token, is `syndicate-to` set? if ($request->input('q') === 'syndicate-to') { @@ -133,21 +163,32 @@ class MicropubController extends Controller $longitude = $latlng[1]; $places = Place::near($latitude, $longitude, 1000); - return (new Response(json_encode($places), 200)) - ->header('Content-Type', 'application/json'); + return response()->json([ + 'response' => 'places', + 'places' => $places + ]); } - //nope, just return the token - $content = http_build_query([ - 'me' => $valid->getClaim('me'), - 'scope' => $valid->getClaim('scope'), - 'client_id' => $valid->getClaim('client_id'), - ]); - return (new Response($content, 200)) - ->header('Content-Type', 'application/x-www-form-urlencoded'); + //nope, just return the token + return response()->json([ + 'response' => 'token', + 'token' => [ + 'me' => $valid->getClaim('me'), + 'scope' => $valid->getClaim('scope'), + 'client_id' => $valid->getClaim('client_id'), + ], + ]); } $content = 'No OAuth token sent with request.'; + $content = <<header('Content-Type', 'application/json'); } } diff --git a/tests/MicropubClientTest.php b/tests/MicropubClientTest.php index d0c47e4d..0fb4bdf3 100644 --- a/tests/MicropubClientTest.php +++ b/tests/MicropubClientTest.php @@ -32,12 +32,13 @@ class MicropubClientTest extends TestCase public function testClientPageRecentAuth() { + $syndication = ['https://twitter.com/jonnybarnes']; $this->withSession([ 'me' => $this->appurl, - 'syndication' => 'mp-syndicate-to=twitter.com%2Fjbl5', + 'syndication' => $syndication, ])->visit($this->appurl . '/notes/new') ->see($this->appurl) - ->see('twitter.com/jbl5'); + ->see('https://twitter.com/jonnybarnes'); } /** diff --git a/tests/MicropubTest.php b/tests/MicropubTest.php index cdc1e4a4..6d100d9a 100644 --- a/tests/MicropubTest.php +++ b/tests/MicropubTest.php @@ -25,20 +25,20 @@ class MicropubTest extends TestCase { $this->call('GET', $this->appurl . '/api/post'); $this->assertResponseStatus(400); - $this->see('No OAuth token sent with request.'); + $this->seeJson(['error_description' => 'No token provided with request']); } public function testMicropubRequestWithoutValidToken() { $this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']); $this->assertResponseStatus(400); - $this->see('Invalid token'); + $this->seeJson(['error_description' => 'The provided token did not pass validation']); } public function testMicropubRequestWithValidToken() { $this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); - $this->see('me=https%3A%2F%2Fjonnybarnes.localhost'); + $this->seeJson(['response' => 'token']); } public function testMicropubRequestForSyndication() From a9d30c3a575134be18323a9d18dd1b6180a7fb6b Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 4 Jul 2016 15:35:17 +0100 Subject: [PATCH 5/6] Respond to a `q=config` request --- app/Http/Controllers/MicropubController.php | 19 +++++++++++++++++++ tests/MicropubTest.php | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 35df30fa..175e9b79 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -168,6 +168,25 @@ EOD; 'places' => $places ]); } + //nope, ho about a config query? + if ($request->input('q') == 'config') { + return response()->json([ + 'syndicate-to' => [[ + 'uid' => 'https://twitter.com/jonnybarnes', + 'name' => 'jonnybarnes on Twitter', + 'service' => [ + 'name' => 'Twitter', + 'url' => 'https://twitter.com', + 'photo' => 'https://upload.wikimedia.org/wikipedia/en/9/9f/Twitter_bird_logo_2012.svg', + ], + 'user' => [ + 'name' => 'jonnybarnes', + 'url' => 'https://twitter.com/jonnybarnes', + 'photo' => 'https://pbs.twimg.com/profile_images/1853565405/jmb-bw.jpg', + ], + ]], + ]); + } //nope, just return the token return response()->json([ diff --git a/tests/MicropubTest.php b/tests/MicropubTest.php index 6d100d9a..4da3853d 100644 --- a/tests/MicropubTest.php +++ b/tests/MicropubTest.php @@ -59,6 +59,12 @@ class MicropubTest extends TestCase $this->see('[]'); } + public function testMicropubRequestForConfig() + { + $this->call('GET', $this->appurl . '/api/post', ['q' => 'config'], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); + $this->seeJson(['uid' => 'https://twitter.com/jonnybarnes']); + } + public function testMicropubRequestCreateNewNote() { $faker = \Faker\Factory::create(); From 4bb87a1dea9f426c2e863c54c5759cda0d58ce43 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 4 Jul 2016 15:37:11 +0100 Subject: [PATCH 6/6] Update changelog --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 7009f734..4140a2ea 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,8 @@ ## Version {next} - Use JSON for syndication endpoint query response + - Use JSON for all micropub requests + - Add support for `q=config` query of the micropub endpoint ## Version 0.0.6.3 (2016-06-29) - Fix an issue with dispatching the syndication job