Merge branch 'release/0.0.5'

This commit is contained in:
Jonny Barnes 2016-06-24 00:02:02 +01:00
commit 41d4f7016e
12 changed files with 126 additions and 88 deletions

View file

@ -6,6 +6,7 @@ use Exception;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Illuminate\Session\TokenMismatchException; use Illuminate\Session\TokenMismatchException;
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\AuthorizationException;
use Symfony\Component\Debug\Exception\FlattenException;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
@ -78,10 +79,12 @@ class Handler extends ExceptionHandler
}); });
$whoops->pushHandler($handler); $whoops->pushHandler($handler);
$flattened = FlattenException::create($exc);
return new \Illuminate\Http\Response( return new \Illuminate\Http\Response(
$whoops->handleException($exc), $whoops->handleException($exc),
$exc->getStatusCode(), $flattened->getStatusCode(),
$exc->getHeaders() $flattened->getHeaders()
); );
} }
} }

View file

@ -160,10 +160,10 @@ class MicropubClientController extends Controller
'contents' => $request->input('reply-to'), 'contents' => $request->input('reply-to'),
]; ];
} }
if ($request->input('mp-syndicate-to')) { if ($request->input('syndicate-to')) {
foreach ($request->input('mp-syndicate-to') as $syn) { foreach ($request->input('syndicate-to') as $syn) {
$multipart[] = [ $multipart[] = [
'name' => 'mp-syndicate-to', 'name' => 'syndicate-to',
'contents' => $syn, 'contents' => $syn,
]; ];
} }
@ -320,14 +320,14 @@ class MicropubClientController extends Controller
if ($syndicationTargets === null) { if ($syndicationTargets === null) {
return; return;
} }
$mpSyndicateTo = []; $syndicateTo = [];
$parts = explode(';', $syndicationTargets); $parts = explode(';', $syndicationTargets);
foreach ($parts as $part) { foreach ($parts as $part) {
$target = explode('=', $part); $target = explode('=', $part);
$mpSyndicateTo[] = urldecode($target[1]); $syndicateTo[] = urldecode($target[1]);
} }
if (count($mpSyndicateTo) > 0) { if (count($syndicateTo) > 0) {
return $mpSyndicateTo; return $syndicateTo;
} }
} }
} }

View file

@ -109,7 +109,7 @@ class MicropubController extends Controller
//we have a valid token, is `syndicate-to` set? //we have a valid token, is `syndicate-to` set?
if ($request->input('q') === 'syndicate-to') { if ($request->input('q') === 'syndicate-to') {
$content = http_build_query([ $content = http_build_query([
'mp-syndicate-to' => 'twitter.com/jonnybarnes', 'syndicate-to' => 'twitter.com/jonnybarnes',
]); ]);
return (new Response($content, 200)) return (new Response($content, 200))

View file

@ -9,6 +9,13 @@ use App\Services\NoteService;
class NotesAdminController extends Controller class NotesAdminController extends Controller
{ {
protected $noteService;
public function __construct(NoteService $noteService = null)
{
$this->noteService = $noteService ?? new NoteService();
}
/** /**
* Show the form to make a new note. * Show the form to make a new note.
* *

View file

@ -5,7 +5,6 @@ namespace App\Http\Controllers;
use App\Note; use App\Note;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use App\Jobs\SendWebMentions;
use App\Jobs\ProcessWebMention; use App\Jobs\ProcessWebMention;
use Jonnybarnes\IndieWeb\Numbers; use Jonnybarnes\IndieWeb\Numbers;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -64,37 +63,4 @@ class WebMentionsController extends Controller
break; break;
} }
} }
/**
* Send a webmention.
*
* @param \App\Note $note
* @return array An array of successful then failed URLs
*/
public function send(Note $note)
{
//grab the URLs
$urlsInReplyTo = explode(' ', $note->in_reply_to);
$urlsNote = $this->getLinks($note->note);
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
foreach ($urls as $url) {
$this->dispatch(new SendWebMentions($url, $note->longurl));
}
}
/**
* Get the URLs from a note.
*/
private function getLinks($html)
{
$urls = [];
$dom = new \DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$urls[] = ($anchor->hasAttribute('href')) ? $anchor->getAttribute('href') : false;
}
return $urls;
}
} }

View file

@ -2,6 +2,7 @@
namespace App\Jobs; namespace App\Jobs;
use App\Note;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
@ -11,50 +12,61 @@ class SendWebMentions extends Job implements ShouldQueue
{ {
use InteractsWithQueue, SerializesModels; use InteractsWithQueue, SerializesModels;
protected $url; protected $note;
protected $source;
/** /**
* Create a new job instance. * Create the job instance, inject dependencies.
* *
* @param Note $note
* @return void * @return void
*/ */
public function __construct($url, $source) public function __construct(Note $note)
{ {
$this->url = $url; $this->note = $note;
$this->source = $source;
} }
/** /**
* Execute the job. * Execute the job.
* *
* @param \GuzzleHttp\Client $guzzle
* @return void * @return void
*/ */
public function handle(Client $client) public function handle(Client $guzzle)
{ {
$endpoint = $this->discoverWebmentionEndpoint($this->url, $client); //grab the URLs
$urlsInReplyTo = explode(' ', $this->note->in_reply_to);
$urlsNote = $this->getLinks($this->note->note);
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
foreach ($urls as $url) {
$endpoint = $this->discoverWebmentionEndpoint($url, $guzzle);
if ($endpoint) { if ($endpoint) {
$client->post($endpoint, [ $guzzle->post($endpoint, [
'form_params' => [ 'form_params' => [
'source' => $this->source, 'source' => $this->note->longurl,
'target' => $this->url, 'target' => $url,
], ],
]); ]);
} }
} }
}
/** /**
* Discover if a URL has a webmention endpoint. * Discover if a URL has a webmention endpoint.
* *
* @param string The URL * @param string The URL
* @param \GuzzleHttp\Client $client * @param \GuzzleHttp\Client $guzzle
* @return string The webmention endpoint URL * @return string The webmention endpoint URL
*/ */
private function discoverWebmentionEndpoint($url, $client) private function discoverWebmentionEndpoint($url, $guzzle)
{ {
//lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) {
return false;
}
$endpoint = null; $endpoint = null;
$response = $client->get($url); $response = $guzzle->get($url);
//check HTTP Headers for webmention endpoint //check HTTP Headers for webmention endpoint
$links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link')); $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
foreach ($links as $link) { foreach ($links as $link) {
@ -83,4 +95,23 @@ class SendWebMentions extends Job implements ShouldQueue
return false; return false;
} }
/**
* Get the URLs from a note.
*
* @param string $html
* @return array $urls
*/
private function getLinks($html)
{
$urls = [];
$dom = new \DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$urls[] = ($anchor->hasAttribute('href')) ? $anchor->getAttribute('href') : false;
}
return $urls;
}
} }

View file

@ -5,9 +5,9 @@ namespace App\Services;
use App\Note; use App\Note;
use App\Place; use App\Place;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Jobs\SendWebMentions;
use App\Jobs\SyndicateToTwitter; use App\Jobs\SyndicateToTwitter;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Http\Controllers\WebMentionsController;
class NoteService class NoteService
{ {
@ -45,10 +45,7 @@ class NoteService
} }
} }
if ($request->input('webmentions')) { $this->dispatch(new SendWebMentions($note));
$wmc = new WebMentionsController();
$wmc->send($note);
}
if (//micropub request, syndication sent as array if (//micropub request, syndication sent as array
(is_array($request->input('mp-syndicate-to')) (is_array($request->input('mp-syndicate-to'))

View file

@ -1,5 +1,9 @@
# Changelog # Changelog
## Version 0.0.5 (2016-06-23)
- Automatically send webmentions
- Change `mp-syndicate-to` to `syndicate-to`
## Version 0.0.4 (2016-06-21) ## Version 0.0.4 (2016-06-21)
- Move bower components into their own subdir - Move bower components into their own subdir
- Move my js into `resources/`, apply an eslint pre-commit hook - Move my js into `resources/`, apply an eslint pre-commit hook

30
composer.lock generated
View file

@ -59,16 +59,16 @@
}, },
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "3.18.18", "version": "3.18.19",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "68b9d0b7e007782bc0f2633cdf5a4a6bf08aaafc" "reference": "2ffb032afe91b143293f75b48ec7593659c66ddb"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/68b9d0b7e007782bc0f2633cdf5a4a6bf08aaafc", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/2ffb032afe91b143293f75b48ec7593659c66ddb",
"reference": "68b9d0b7e007782bc0f2633cdf5a4a6bf08aaafc", "reference": "2ffb032afe91b143293f75b48ec7593659c66ddb",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -135,7 +135,7 @@
"s3", "s3",
"sdk" "sdk"
], ],
"time": "2016-06-14 20:35:03" "time": "2016-06-21 21:41:45"
}, },
{ {
"name": "barnabywalters/mf-cleaner", "name": "barnabywalters/mf-cleaner",
@ -1470,16 +1470,16 @@
}, },
{ {
"name": "league/flysystem-aws-s3-v3", "name": "league/flysystem-aws-s3-v3",
"version": "1.0.12", "version": "1.0.13",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/thephpleague/flysystem-aws-s3-v3.git", "url": "https://github.com/thephpleague/flysystem-aws-s3-v3.git",
"reference": "d9c27edda5b4b2840c3f602d4ce6fbfeaab10e5a" "reference": "dc56a8faf3aff0841f9eae04b6af94a50657896c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem-aws-s3-v3/zipball/d9c27edda5b4b2840c3f602d4ce6fbfeaab10e5a", "url": "https://api.github.com/repos/thephpleague/flysystem-aws-s3-v3/zipball/dc56a8faf3aff0841f9eae04b6af94a50657896c",
"reference": "d9c27edda5b4b2840c3f602d4ce6fbfeaab10e5a", "reference": "dc56a8faf3aff0841f9eae04b6af94a50657896c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1513,7 +1513,7 @@
} }
], ],
"description": "Flysystem adapter for the AWS S3 SDK v3.x", "description": "Flysystem adapter for the AWS S3 SDK v3.x",
"time": "2016-06-06 11:18:47" "time": "2016-06-21 21:34:35"
}, },
{ {
"name": "league/glide", "name": "league/glide",
@ -2437,16 +2437,16 @@
}, },
{ {
"name": "spatie/laravel-medialibrary", "name": "spatie/laravel-medialibrary",
"version": "4.2.1", "version": "4.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/spatie/laravel-medialibrary.git", "url": "https://github.com/spatie/laravel-medialibrary.git",
"reference": "3499014e3b2de852f1e5f4bd4006e4031dd49daa" "reference": "d5ec391023ea27c78131f2d1823908d45f9e38ae"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-medialibrary/zipball/3499014e3b2de852f1e5f4bd4006e4031dd49daa", "url": "https://api.github.com/repos/spatie/laravel-medialibrary/zipball/d5ec391023ea27c78131f2d1823908d45f9e38ae",
"reference": "3499014e3b2de852f1e5f4bd4006e4031dd49daa", "reference": "d5ec391023ea27c78131f2d1823908d45f9e38ae",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2493,7 +2493,7 @@
"media", "media",
"spatie" "spatie"
], ],
"time": "2016-06-03 11:06:43" "time": "2016-06-23 08:22:21"
}, },
{ {
"name": "spatie/pdf-to-image", "name": "spatie/pdf-to-image",

View file

@ -4,9 +4,8 @@
<legend>New Note</legend> <legend>New Note</legend>
<label for="in-reply-to" accesskey="r">Reply-to: </label><input type="text" name="in-reply-to" id="in-reply-to" placeholder="in-reply-to-1 in-reply-to-2 …" value="{{ old('in-reply-to') }}"> <label for="in-reply-to" accesskey="r">Reply-to: </label><input type="text" name="in-reply-to" id="in-reply-to" placeholder="in-reply-to-1 in-reply-to-2 …" value="{{ old('in-reply-to') }}">
<label for="content" accesskey="n">Note: </label><textarea name="content" id="content" placeholder="Note" autofocus>{{ old('content') }}</textarea> <label for="content" accesskey="n">Note: </label><textarea name="content" id="content" placeholder="Note" autofocus>{{ old('content') }}</textarea>
<label for="webmentions" accesskey="w">Send webmentions: </label><input type="checkbox" name="webmentions" id="webmentions" checked="checked"><br>
@if ($micropub === true) @if ($micropub === true)
<label for="syndication" accesskey="s">Syndication: </label>@if($syndication)<ul class="syndication-targets-list" name="syndication">@foreach($syndication as $target)<li><input type="checkbox" name="mp-syndicate-to[]" id="{{ $target }}" value="{{ $target }}" checked="checked"> <label for="{{ $target }}">{{ $target }}</label></li>@endforeach</ul>@endif <label for="syndication" accesskey="s">Syndication: </label>@if($syndication)<ul class="syndication-targets-list" name="syndication">@foreach($syndication as $target)<li><input type="checkbox" name="syndicate-to[]" id="{{ $target }}" value="{{ $target }}" checked="checked"> <label for="{{ $target }}">{{ $target }}</label></li>@endforeach</ul>@endif
<a href="/refresh-syndication-targets">Refresh Syndication Targets</a><br> <a href="/refresh-syndication-targets">Refresh Syndication Targets</a><br>
@endif @endif
<label for="photo" accesskey="p">Photo: </label><input type="file" accept="image/*" value="Upload" name="photo[]" id="photo" multiple> <label for="photo" accesskey="p">Photo: </label><input type="file" accept="image/*" value="Upload" name="photo[]" id="photo" multiple>

View file

@ -38,7 +38,7 @@ class MicropubTest extends TestCase
public function testMicropubRequestWithValidToken() public function testMicropubRequestWithValidToken()
{ {
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); $this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
$this->see('me=https%3A%2F%2Fjbl5.dev'); $this->see('me=https%3A%2F%2Fjonnybarnes.localhost');
} }
public function testMicropubRequestForSyndication() public function testMicropubRequestForSyndication()
@ -79,8 +79,6 @@ class MicropubTest extends TestCase
public function testMicropubRequestCreateNewPlace() public function testMicropubRequestCreateNewPlace()
{ {
$faker = \Faker\Factory::create();
$note = $faker->text;
$this->call( $this->call(
'POST', 'POST',
$this->appurl . '/api/post', $this->appurl . '/api/post',
@ -101,7 +99,7 @@ class MicropubTest extends TestCase
$signer = new Sha256(); $signer = new Sha256();
$token = (new Builder()) $token = (new Builder())
->set('client_id', 'https://quill.p3k.io') ->set('client_id', 'https://quill.p3k.io')
->set('me', 'https://jbl5.dev') ->set('me', 'https://jonnybarnes.localhost')
->set('scope', 'post') ->set('scope', 'post')
->set('issued_at', time()) ->set('issued_at', time())
->sign($signer, env('APP_KEY')) ->sign($signer, env('APP_KEY'))

33
tests/NotesAdminTest.php Normal file
View file

@ -0,0 +1,33 @@
<?php
namespace App\Tests;
use TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesAdminTest extends TestCase
{
use DatabaseTransactions;
protected $appurl;
protected $notesAdminController;
public function setUp()
{
parent::setUp();
$this->appurl = config('app.url');
$this->notesAdminController = new \App\Http\Controllers\NotesAdminController();
}
public function testCreatedNoteDispatchesSendWebmentionsJob()
{
$this->expectsJobs(\App\Jobs\SendWebMentions::class);
$this->withSession(['loggedin' => true])
->visit($this->appurl . '/admin/note/new')
->type('Mentioning', 'content')
->press('Submit');
}
}