Should now be able to send webmentions to webmention.rocks

This commit is contained in:
Jonny Barnes 2016-09-05 23:39:54 +01:00
parent a9f089098c
commit 94ec2ec254
3 changed files with 38 additions and 19 deletions

View file

@ -20,9 +20,10 @@ class SendWebMentions extends Job implements ShouldQueue
* @param Note $note * @param Note $note
* @return void * @return void
*/ */
public function __construct(Note $note) public function __construct(Note $note, Client $guzzle = null)
{ {
$this->note = $note; $this->note = $note;
$this->guzzle = $guzzle ?? new Client();
} }
/** /**
@ -31,16 +32,16 @@ class SendWebMentions extends Job implements ShouldQueue
* @param \GuzzleHttp\Client $guzzle * @param \GuzzleHttp\Client $guzzle
* @return void * @return void
*/ */
public function handle(Client $guzzle) public function handle()
{ {
//grab the URLs //grab the URLs
$urlsInReplyTo = explode(' ', $this->note->in_reply_to); $urlsInReplyTo = explode(' ', $this->note->in_reply_to);
$urlsNote = $this->getLinks($this->note->note); $urlsNote = $this->getLinks($this->note->note);
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
foreach ($urls as $url) { foreach ($urls as $url) {
$endpoint = $this->discoverWebmentionEndpoint($url, $guzzle); $endpoint = $this->discoverWebmentionEndpoint($url, $this->guzzle);
if ($endpoint) { if ($endpoint) {
$guzzle->post($endpoint, [ $this->guzzle->post($endpoint, [
'form_params' => [ 'form_params' => [
'source' => $this->note->longurl, 'source' => $this->note->longurl,
'target' => $url, 'target' => $url,
@ -73,8 +74,8 @@ class SendWebMentions extends Job implements ShouldQueue
//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) {
if ($link['rel'] == 'webmention') { if (mb_stristr($link['rel'], 'webmention')) {
return trim($link[0], '<>'); return $this->resolveUri($link[0], $url);
} }
} }
@ -89,11 +90,7 @@ class SendWebMentions extends Job implements ShouldQueue
$endpoint = $rels[0]['http://webmention.org/'][0]; $endpoint = $rels[0]['http://webmention.org/'][0];
} }
if ($endpoint) { if ($endpoint) {
if (filter_var($endpoint, FILTER_VALIDATE_URL)) { return $this->resolveUri($endpoint, $url);
return $endpoint;
}
//it must be a relative url, so resolve with php-mf2
return $mf2->resolveUrl($endpoint);
} }
return false; return false;
@ -105,7 +102,7 @@ class SendWebMentions extends Job implements ShouldQueue
* @param string $html * @param string $html
* @return array $urls * @return array $urls
*/ */
private function getLinks($html) public function getLinks($html)
{ {
$urls = []; $urls = [];
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -117,4 +114,23 @@ class SendWebMentions extends Job implements ShouldQueue
return $urls; 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
);
}
} }

View file

@ -3,6 +3,8 @@
## Version {next} ## Version {next}
- Adding jsonb column to store webmentions mf2. - 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 * 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) ## Version 0.0.8.5 (2016-07-18)
- Set the size of the textarea in a form better - Set the size of the textarea in a form better

15
composer.lock generated
View file

@ -182,12 +182,12 @@
"version": "0.16", "version": "0.16",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Bosnadev/Database.git", "url": "https://github.com/bosnadev/database.git",
"reference": "c2748d118415d30ce69b792448689285d01ffdb9" "reference": "c2748d118415d30ce69b792448689285d01ffdb9"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Bosnadev/Database/zipball/c2748d118415d30ce69b792448689285d01ffdb9", "url": "https://api.github.com/repos/bosnadev/database/zipball/c2748d118415d30ce69b792448689285d01ffdb9",
"reference": "c2748d118415d30ce69b792448689285d01ffdb9", "reference": "c2748d118415d30ce69b792448689285d01ffdb9",
"shasum": "" "shasum": ""
}, },
@ -2154,16 +2154,16 @@
}, },
{ {
"name": "psr/http-message", "name": "psr/http-message",
"version": "1.0", "version": "1.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/php-fig/http-message.git", "url": "https://github.com/php-fig/http-message.git",
"reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2191,6 +2191,7 @@
} }
], ],
"description": "Common interface for HTTP messages", "description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [ "keywords": [
"http", "http",
"http-message", "http-message",
@ -2199,7 +2200,7 @@
"request", "request",
"response" "response"
], ],
"time": "2015-05-04 20:22:00" "time": "2016-08-06 14:39:51"
}, },
{ {
"name": "psr/log", "name": "psr/log",