From 995140bb954bf5aa3a88435e41f06847acba6992 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Tue, 21 Jun 2016 15:53:05 +0100 Subject: [PATCH] Automatically send webmentions, move the sending logic into the job --- .../Controllers/WebMentionsController.php | 34 ----------- app/Jobs/SendWebMentions.php | 59 +++++++++++++------ app/Services/NoteService.php | 6 +- 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/WebMentionsController.php b/app/Http/Controllers/WebMentionsController.php index ccffd451..fc254058 100644 --- a/app/Http/Controllers/WebMentionsController.php +++ b/app/Http/Controllers/WebMentionsController.php @@ -5,7 +5,6 @@ namespace App\Http\Controllers; use App\Note; use Illuminate\Http\Request; use Illuminate\Http\Response; -use App\Jobs\SendWebMentions; use App\Jobs\ProcessWebMention; use Jonnybarnes\IndieWeb\Numbers; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -64,37 +63,4 @@ class WebMentionsController extends Controller 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; - } } diff --git a/app/Jobs/SendWebMentions.php b/app/Jobs/SendWebMentions.php index 796fc97c..9f9d70a0 100644 --- a/app/Jobs/SendWebMentions.php +++ b/app/Jobs/SendWebMentions.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Note; use GuzzleHttp\Client; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; @@ -11,18 +12,20 @@ class SendWebMentions extends Job implements ShouldQueue { use InteractsWithQueue, SerializesModels; - protected $url; - protected $source; + protected $note; + protected $guzzle; /** - * Create a new job instance. + * Create the job instance, inject dependencies. * + * @param User $user + * @param Note $note * @return void */ - public function __construct($url, $source) + public function __construct(Note $note, Client $guzzle) { - $this->url = $url; - $this->source = $source; + $this->note = $note; + $this->guzzle = $guzzle ?? new Client(); } /** @@ -30,16 +33,22 @@ class SendWebMentions extends Job implements ShouldQueue * * @return void */ - public function handle(Client $client) + public function handle(Note $note) { - $endpoint = $this->discoverWebmentionEndpoint($this->url, $client); - if ($endpoint) { - $client->post($endpoint, [ - 'form_params' => [ - 'source' => $this->source, - 'target' => $this->url, - ], - ]); + //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); + if ($endpoint) { + $this->guzzle->post($endpoint, [ + 'form_params' => [ + 'source' => $this->note->longurl, + 'target' => $url + ] + ]) + } } } @@ -50,11 +59,11 @@ class SendWebMentions extends Job implements ShouldQueue * @param \GuzzleHttp\Client $client * @return string The webmention endpoint URL */ - private function discoverWebmentionEndpoint($url, $client) + private function discoverWebmentionEndpoint($url) { $endpoint = null; - $response = $client->get($url); + $response = $this->guzzle->get($url); //check HTTP Headers for webmention endpoint $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link')); foreach ($links as $link) { @@ -83,4 +92,20 @@ class SendWebMentions extends Job implements ShouldQueue return false; } + + /** + * 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; + } } diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 0b680090..c9279533 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -5,6 +5,7 @@ namespace App\Services; use App\Note; use App\Place; use Illuminate\Http\Request; +use App\Jobs\SendWebmentions; use App\Jobs\SyndicateToTwitter; use Illuminate\Foundation\Bus\DispatchesJobs; use App\Http\Controllers\WebMentionsController; @@ -45,10 +46,7 @@ class NoteService } } - if ($request->input('webmentions')) { - $wmc = new WebMentionsController(); - $wmc->send($note); - } + $this->dispatch(new SendWebmentions($note)); if (//micropub request, syndication sent as array (is_array($request->input('mp-syndicate-to'))