2019-02-09 04:04:41 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-11 01:43:29 +09:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-03-03 00:29:43 +09:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2019-02-09 04:04:41 +09:00
|
|
|
|
|
|
|
class ActivityPubResolver implements Resolver, Parser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \GuzzleHttp\Client
|
|
|
|
*/
|
|
|
|
private $activityClient;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->activityClient = new \GuzzleHttp\Client([
|
|
|
|
'headers' => [
|
|
|
|
'Accept' => 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
|
|
|
$res = $this->activityClient->get($url);
|
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
return $this->parse($res->getBody());
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parse(string $json): Metadata
|
|
|
|
{
|
|
|
|
$activityOrObject = json_decode($json, true);
|
|
|
|
$object = $activityOrObject['object'] ?? $activityOrObject;
|
|
|
|
|
2019-11-24 21:17:53 +09:00
|
|
|
if ($object['type'] !== 'Note') {
|
|
|
|
throw new UnsupportedContentException('Unsupported object type: ' . $object['type']);
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:04:41 +09:00
|
|
|
$metadata = new Metadata();
|
|
|
|
|
|
|
|
$metadata->title = isset($object['attributedTo']) ? $this->getTitleFromActor($object['attributedTo']) : '';
|
2019-03-03 00:29:43 +09:00
|
|
|
$metadata->description .= isset($object['summary']) ? $object['summary'] . ' | ' : '';
|
2019-02-11 03:07:01 +09:00
|
|
|
$metadata->description .= isset($object['content']) ? $this->html2text($object['content']) : '';
|
2019-02-09 04:04:41 +09:00
|
|
|
$metadata->image = $object['attachment'][0]['url'] ?? '';
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTitleFromActor(string $url): string
|
|
|
|
{
|
2019-02-11 01:43:29 +09:00
|
|
|
try {
|
|
|
|
$res = $this->activityClient->get($url);
|
|
|
|
if ($res->getStatusCode() !== 200) {
|
|
|
|
Log::info(self::class . ': Actorの取得に失敗 URL=' . $url);
|
2019-03-03 00:29:43 +09:00
|
|
|
|
2019-02-11 01:43:29 +09:00
|
|
|
return '';
|
|
|
|
}
|
2019-02-09 04:04:41 +09:00
|
|
|
|
2019-02-11 01:43:29 +09:00
|
|
|
$actor = json_decode($res->getBody(), true);
|
|
|
|
$title = $actor['name'] ?? '';
|
|
|
|
if (isset($actor['preferredUsername'])) {
|
|
|
|
$title .= ' (@' . $actor['preferredUsername'] . '@' . parse_url($actor['id'], PHP_URL_HOST) . ')';
|
|
|
|
}
|
2019-02-09 04:04:41 +09:00
|
|
|
|
2019-02-11 01:43:29 +09:00
|
|
|
return $title;
|
|
|
|
} catch (TransferException $e) {
|
|
|
|
Log::info(self::class . ': Actorの取得に失敗 URL=' . $url);
|
2019-03-03 00:29:43 +09:00
|
|
|
|
2019-02-11 01:43:29 +09:00
|
|
|
return '';
|
|
|
|
}
|
2019-02-09 04:04:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
private function html2text(string $html): string
|
|
|
|
{
|
2019-04-14 17:40:21 +09:00
|
|
|
if (empty($html)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:04:41 +09:00
|
|
|
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
|
|
|
|
$html = preg_replace('~<br\s*/?\s*>|</p>\s*<p[^>]*>~i', "\n", $html);
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
2019-03-03 00:29:43 +09:00
|
|
|
|
2019-02-09 04:04:41 +09:00
|
|
|
return $dom->textContent;
|
|
|
|
}
|
|
|
|
}
|