Start work on better webmention support

This commit is contained in:
Jonny Barnes 2016-07-29 09:55:27 +01:00
parent 75cb5cf454
commit 5585483800
2 changed files with 16 additions and 22 deletions

View file

@ -28,7 +28,7 @@ class WebMentionsController extends Controller
} }
//next check the $target is valid //next check the $target is valid
$path = parse_url($request->input('target'))['path']; $path = parse_url($request->input('target'), PHP_URL_PATH);
$pathParts = explode('/', $path); $pathParts = explode('/', $path);
switch ($pathParts[1]) { switch ($pathParts[1]) {
@ -36,9 +36,8 @@ class WebMentionsController extends Controller
//we have a note //we have a note
$noteId = $pathParts[2]; $noteId = $pathParts[2];
$numbers = new Numbers(); $numbers = new Numbers();
$realId = $numbers->b60tonum($noteId);
try { try {
$note = Note::findOrFail($realId); $note = Note::findOrFail($numbers->b60tonum($noteId));
$this->dispatch(new ProcessWebMention($note, $request->input('source'))); $this->dispatch(new ProcessWebMention($note, $request->input('source')));
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
return new Response('This note doesnt exist.', 400); return new Response('This note doesnt exist.', 400);

View file

@ -2,8 +2,8 @@
namespace App\Jobs; namespace App\Jobs;
use Mf2;
use App\Note; use App\Note;
use Mf2\parse;
use HTMLPurifier; use HTMLPurifier;
use App\WebMention; use App\WebMention;
use GuzzleHttp\Client; use GuzzleHttp\Client;
@ -11,6 +11,7 @@ use HTMLPurifier_Config;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Jonnybarnes\WebmentionsParser\Parser; use Jonnybarnes\WebmentionsParser\Parser;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
class ProcessWebMention extends Job implements ShouldQueue class ProcessWebMention extends Job implements ShouldQueue
@ -44,7 +45,11 @@ class ProcessWebMention extends Job implements ShouldQueue
$sourceURL = parse_url($this->source); $sourceURL = parse_url($this->source);
$baseURL = $sourceURL['scheme'] . '://' . $sourceURL['host']; $baseURL = $sourceURL['scheme'] . '://' . $sourceURL['host'];
$remoteContent = $this->getRemoteContent($this->source); $remoteContent = $this->getRemoteContent($this->source);
$microformats = $this->parseHTML($remoteContent, $baseURL); if ($remoteContent === null) {
return false;
}
$mf2Parser = new Mf2\Parser($remoteContent, $baseURL, true);
$microformats = $mf2Parser->parse();
$count = WebMention::where('source', '=', $this->source)->count(); $count = WebMention::where('source', '=', $this->source)->count();
if ($count > 0) { if ($count > 0) {
//we already have a webmention from this source //we already have a webmention from this source
@ -141,13 +146,17 @@ class ProcessWebMention extends Job implements ShouldQueue
* Retreive the remote content from a URL, and caches the result. * Retreive the remote content from a URL, and caches the result.
* *
* @param string The URL to retreive content from * @param string The URL to retreive content from
* @return string The HTML from the URL * @return string|null The HTML from the URL (or null if error)
*/ */
private function getRemoteContent($url) private function getRemoteContent($url)
{ {
$client = new Client(); $client = new Client();
$response = $client->get($url); try {
$response = $client->request('GET', $url);
} catch(RequestException $e) {
return;
}
$html = (string) $response->getBody(); $html = (string) $response->getBody();
$path = storage_path() . '/HTML/' . $this->createFilenameFromURL($url); $path = storage_path() . '/HTML/' . $this->createFilenameFromURL($url);
$this->fileForceContents($path, $html); $this->fileForceContents($path, $html);
@ -189,20 +198,6 @@ class ProcessWebMention extends Job implements ShouldQueue
file_put_contents("$dir/$name", $contents); file_put_contents("$dir/$name", $contents);
} }
/**
* A wrapper function for php-mf2s parse method.
*
* @param string The HTML to parse
* @param string The base URL to resolve relative URLs in the HTML against
* @return array The porcessed microformats
*/
private function parseHTML($html, $baseurl)
{
$microformats = \Mf2\parse((string) $html, $baseurl);
return $microformats;
}
/** /**
* Save a profile image to the local cache. * Save a profile image to the local cache.
* *