Move HTML filtering into an observer

This commit is contained in:
Jonny Barnes 2016-09-17 19:58:00 +01:00
parent 23d7d97ed0
commit a3d6767ccb

View file

@ -4,10 +4,8 @@ namespace App\Jobs;
use Mf2; use Mf2;
use App\Note; use App\Note;
use HTMLPurifier;
use App\WebMention; use App\WebMention;
use GuzzleHttp\Client; use GuzzleHttp\Client;
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;
@ -65,7 +63,6 @@ class ProcessWebMention extends Job implements ShouldQueue
return; return;
} }
//webmenion is still a reply, so update content //webmenion is still a reply, so update content
$microformats = $this->filterHTML($microformats);
$this->dispatch(new SaveProfileImage($microformats)); $this->dispatch(new SaveProfileImage($microformats));
$webmention->mf2 = json_encode($microformats); $webmention->mf2 = json_encode($microformats);
$webmention->save(); $webmention->save();
@ -94,7 +91,6 @@ class ProcessWebMention extends Job implements ShouldQueue
$webmention = new WebMention(); $webmention = new WebMention();
$type = $parser->getMentionType($microformats); //throw error here? $type = $parser->getMentionType($microformats); //throw error here?
$this->dispatch(new SaveProfileImage($microformats)); $this->dispatch(new SaveProfileImage($microformats));
$microformats = $this->filterHTML($microformats);
$webmention->source = $this->source; $webmention->source = $this->source;
$webmention->target = $this->note->longurl; $webmention->target = $this->note->longurl;
$webmention->commentable_id = $this->note->id; $webmention->commentable_id = $this->note->id;
@ -146,36 +142,4 @@ class ProcessWebMention extends Job implements ShouldQueue
return $url; return $url;
} }
/**
* Filter the HTML in a reply webmention.
*
* @param array The unfiltered microformats
* @return array The filtered microformats
*/
private function filterHTML($microformats)
{
if (isset($microformats['items'][0]['properties']['content'][0]['html'])) {
$microformats['items'][0]['properties']['content'][0]['html_purified'] = $this->useHTMLPurifier(
$microformats['items'][0]['properties']['content'][0]['html']
);
}
return $microformats;
}
/**
* Set up and use HTMLPurifer on some HTML.
*
* @param string The HTML to be processed
* @return string The processed HTML
*/
private function useHTMLPurifier($html)
{
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier');
$purifier = new HTMLPurifier($config);
return $purifier->purify($html);
}
} }