2017-09-19 16:07:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-09-19 16:07:32 +01:00
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2017-12-19 16:00:42 +00:00
|
|
|
use App\Models\Like;
|
2017-09-19 16:07:32 +01:00
|
|
|
use GuzzleHttp\Client;
|
2019-10-27 16:15:14 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2017-09-19 16:07:32 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Arr;
|
2017-09-19 16:07:32 +01:00
|
|
|
use Jonnybarnes\WebmentionsParser\Authorship;
|
|
|
|
|
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
|
|
|
|
|
|
|
|
|
|
class ProcessLike implements ShouldQueue
|
|
|
|
|
{
|
2019-10-27 16:15:14 +00:00
|
|
|
use Dispatchable;
|
|
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
|
|
|
|
use SerializesModels;
|
2017-09-19 16:07:32 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function __construct(
|
|
|
|
|
protected Like $like
|
2024-07-13 10:55:19 +01:00
|
|
|
) {}
|
2017-09-19 16:07:32 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
2019-10-27 16:15:14 +00:00
|
|
|
* @throws GuzzleException
|
2017-09-19 16:07:32 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function handle(Client $client, Authorship $authorship): int
|
2017-09-19 16:07:32 +01:00
|
|
|
{
|
|
|
|
|
$response = $client->request('GET', $this->like->url);
|
|
|
|
|
$mf2 = \Mf2\parse((string) $response->getBody(), $this->like->url);
|
2019-02-01 18:49:35 +00:00
|
|
|
if (Arr::has($mf2, 'items.0.properties.content')) {
|
2017-09-19 16:07:32 +01:00
|
|
|
$this->like->content = $mf2['items'][0]['properties']['content'][0]['html'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$author = $authorship->findAuthor($mf2);
|
|
|
|
|
if (is_array($author)) {
|
2019-02-01 18:49:35 +00:00
|
|
|
$this->like->author_name = Arr::get($author, 'properties.name.0');
|
|
|
|
|
$this->like->author_url = Arr::get($author, 'properties.url.0');
|
2017-09-19 16:07:32 +01:00
|
|
|
}
|
|
|
|
|
if (is_string($author) && $author !== '') {
|
|
|
|
|
$this->like->author_name = $author;
|
|
|
|
|
}
|
|
|
|
|
} catch (AuthorshipParserException $exception) {
|
2018-01-12 21:19:42 +00:00
|
|
|
return 1;
|
2017-09-19 16:07:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->like->save();
|
2018-01-15 14:02:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2017-09-19 16:07:32 +01:00
|
|
|
}
|
|
|
|
|
}
|