diff --git a/app/Jobs/SendWebMentions.php b/app/Jobs/SendWebMentions.php index 4cfbaafc..4caf6c7d 100644 --- a/app/Jobs/SendWebMentions.php +++ b/app/Jobs/SendWebMentions.php @@ -20,9 +20,10 @@ class SendWebMentions extends Job implements ShouldQueue * @param Note $note * @return void */ - public function __construct(Note $note) + public function __construct(Note $note, Client $guzzle = null) { $this->note = $note; + $this->guzzle = $guzzle ?? new Client(); } /** @@ -31,16 +32,16 @@ class SendWebMentions extends Job implements ShouldQueue * @param \GuzzleHttp\Client $guzzle * @return void */ - public function handle(Client $guzzle) + public function handle() { //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); + $endpoint = $this->discoverWebmentionEndpoint($url, $this->guzzle); if ($endpoint) { - $guzzle->post($endpoint, [ + $this->guzzle->post($endpoint, [ 'form_params' => [ 'source' => $this->note->longurl, 'target' => $url, @@ -73,8 +74,8 @@ class SendWebMentions extends Job implements ShouldQueue //check HTTP Headers for webmention endpoint $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link')); foreach ($links as $link) { - if ($link['rel'] == 'webmention') { - return trim($link[0], '<>'); + if (mb_stristr($link['rel'], 'webmention')) { + return $this->resolveUri($link[0], $url); } } @@ -89,11 +90,7 @@ class SendWebMentions extends Job implements ShouldQueue $endpoint = $rels[0]['http://webmention.org/'][0]; } if ($endpoint) { - if (filter_var($endpoint, FILTER_VALIDATE_URL)) { - return $endpoint; - } - //it must be a relative url, so resolve with php-mf2 - return $mf2->resolveUrl($endpoint); + return $this->resolveUri($endpoint, $url); } return false; @@ -105,7 +102,7 @@ class SendWebMentions extends Job implements ShouldQueue * @param string $html * @return array $urls */ - private function getLinks($html) + public function getLinks($html) { $urls = []; $dom = new \DOMDocument(); @@ -117,4 +114,23 @@ class SendWebMentions extends Job implements ShouldQueue return $urls; } + + /** + * Resolve a URI if necessary. + * + * @param string $url + * @param string $base + * @return string + */ + public function resolveUri(string $url, string $base): string + { + $endpoint = \GuzzleHttp\Psr7\uri_for($url); + if ($endpoint->getScheme() !== null) { + return (string) $endpoint; + } + return (string) \GuzzleHttp\Psr7\Uri::resolve( + \GuzzleHttp\Psr7\uri_for($base), + $endpoint + ); + } } diff --git a/changelog.md b/changelog.md index 4b8f3b2f..788e181e 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ ## Version {next} - Adding jsonb column to store webmentions’ mf2. * As of L5.2 this needs a custom command to drop NOT NULL from content, L5.3 should allow a fix for this + - Refactor receiving webmention code + - Refactor sending webmention code to pass webmention.rocks ## Version 0.0.8.5 (2016-07-18) - Set the size of the textarea in a form better diff --git a/composer.lock b/composer.lock index 92da6d7a..f7578a1f 100644 --- a/composer.lock +++ b/composer.lock @@ -182,12 +182,12 @@ "version": "0.16", "source": { "type": "git", - "url": "https://github.com/Bosnadev/Database.git", + "url": "https://github.com/bosnadev/database.git", "reference": "c2748d118415d30ce69b792448689285d01ffdb9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Bosnadev/Database/zipball/c2748d118415d30ce69b792448689285d01ffdb9", + "url": "https://api.github.com/repos/bosnadev/database/zipball/c2748d118415d30ce69b792448689285d01ffdb9", "reference": "c2748d118415d30ce69b792448689285d01ffdb9", "shasum": "" }, @@ -2154,16 +2154,16 @@ }, { "name": "psr/http-message", - "version": "1.0", + "version": "1.0.1", "source": { "type": "git", "url": "https://github.com/php-fig/http-message.git", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", "shasum": "" }, "require": { @@ -2191,6 +2191,7 @@ } ], "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", "keywords": [ "http", "http-message", @@ -2199,7 +2200,7 @@ "request", "response" ], - "time": "2015-05-04 20:22:00" + "time": "2016-08-06 14:39:51" }, { "name": "psr/log",