diff --git a/README.md b/README.md index cb3e195..630a9cf 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,9 @@ Location: https://telegraph.p3k.io/webmention/xxxxxxxx After Telegraph processes your request, you will receive a post to the callback URL. The initial callback you receive will be one of the following status codes: * `not_supported` - no webmention or pingback endpoint found -* `webmention_queued` - webmention was queued for processing -* `webmention_success` - webmention was successfully processed (for webmention endpoints which process synchronously) +* `webmention_accepted` - the webmention request was accepted * `webmention_error` - the webmention endpoint returned an error code -* `pingback_success` - pingback was received (pingback does not differentiate between when a request is queued or processed immediately) +* `pingback_accepted` - pingback was accepted (pingback does not differentiate between when a request is queued or processed immediately) * `pingback_error` - the pingback endpoint returned an error code Typically, webmention endpoints defer processing until later, so normally the first callback received will indicate that the webmention was queued. This callback will normally be sent relatively quickly after you make the initial request, typically within a few seconds. diff --git a/lib/Telegraph/Webmention.php b/lib/Telegraph/Webmention.php index a49666c..303a3b5 100644 --- a/lib/Telegraph/Webmention.php +++ b/lib/Telegraph/Webmention.php @@ -5,11 +5,15 @@ use IndieWeb\MentionClient; class Webmention { - private static function saveStatus($webmentionID, $code, $raw=null) { + private static function saveStatus($webmentionID, $http_code, $code, $raw=null) { $status = ORM::for_table('webmention_status')->create(); $status->webmention_id = $webmentionID; $status->created_at = date('Y-m-d H:i:s'); + if($http_code) + $status->http_code = $http_code; $status->status = $code; + if($raw) + $status->raw_response = $raw; $status->save(); } @@ -32,7 +36,7 @@ class Webmention { // If no pingback endpoint was found, we can't do anything else if(!$pingbackEndpoint) { - self::saveStatus($id, 'not_supported'); + self::saveStatus($id, null, 'not_supported'); return; } @@ -40,13 +44,24 @@ class Webmention { $webmention->save(); $success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target); - self::saveStatus($id, $success ? 'pingback_success' : 'pingback_error'); + self::saveStatus($id, null, ($success ? 'pingback_accepted' : 'pingback_error')); return; } + // There is a webmention endpoint, send the webmention now + $webmention->webmention_endpoint = $endpoint; + $webmention->save(); + $response = $client->sendWebmentionToEndpoint($endpoint, $webmention->source, $webmention->target); + if(in_array($response['code'], [200,201,202])) { + $status = 'webmention_accepted'; + } else { + $status = 'webmention_error'; + } + + self::saveStatus($webmention->id, $response['code'], $status, $response['body']); } } diff --git a/tests/ProcessTest.php b/tests/ProcessTest.php index 3c4246c..95466a9 100644 --- a/tests/ProcessTest.php +++ b/tests/ProcessTest.php @@ -71,7 +71,7 @@ class ProcessTest extends PHPUnit_Framework_TestCase { 'target' => 'http://target.example.com/pingback-success' ]); $status = $this->webmentionStatus($webmention->id); - $this->assertEquals($status->status, 'pingback_success'); + $this->assertEquals($status->status, 'pingback_accepted'); } public function testPingbackFailed() { @@ -85,4 +85,19 @@ class ProcessTest extends PHPUnit_Framework_TestCase { $this->assertEquals($status->status, 'pingback_error'); } + public function testWebmentionTakesPriorityOverPingback() { + $this->_createExampleAccount(); + $webmention = $this->webmention([ + 'token' => 'a', + 'source' => 'http://source.example.com/webmention-and-pingback', + 'target' => 'http://target.example.com/webmention-success' + ]); + $status = $this->webmentionStatus($webmention->id); + $this->assertEquals($status->status, 'webmention_accepted'); + } + + public function testWebmentionFailed() { + + } + } diff --git a/tests/data/source.example.com/webmention-and-pingback b/tests/data/source.example.com/webmention-and-pingback new file mode 100644 index 0000000..c97b298 --- /dev/null +++ b/tests/data/source.example.com/webmention-and-pingback @@ -0,0 +1,16 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + +

+ target +

+ + diff --git a/tests/data/target.example.com/webmention-success b/tests/data/target.example.com/webmention-success new file mode 100644 index 0000000..d3a17f4 --- /dev/null +++ b/tests/data/target.example.com/webmention-success @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + + +

This has a webmention endpoint that will return success

+ + diff --git a/tests/data/webmention.example.com/error b/tests/data/webmention.example.com/error new file mode 100644 index 0000000..e5caf37 --- /dev/null +++ b/tests/data/webmention.example.com/error @@ -0,0 +1,7 @@ +HTTP/1.1 400 Bad Request +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/plain; charset=utf-8 +Connection: keep-alive + +The webmention was not accepted diff --git a/tests/data/webmention.example.com/success b/tests/data/webmention.example.com/success new file mode 100644 index 0000000..a8e52fd --- /dev/null +++ b/tests/data/webmention.example.com/success @@ -0,0 +1,7 @@ +HTTP/1.1 202 Accepted +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/plain; charset=utf-8 +Connection: keep-alive + +Webmention was accepted