tissue/app/MetadataResolver/ActivityPubResolver.php

84 lines
2.6 KiB
PHP
Raw Normal View History

2019-02-09 04:04:41 +09:00
<?php
namespace App\MetadataResolver;
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;
$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
{
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
return '';
}
2019-02-09 04:04:41 +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
return $title;
} catch (TransferException $e) {
Log::info(self::class . ': Actorの取得に失敗 URL=' . $url);
2019-03-03 00:29:43 +09:00
return '';
}
2019-02-09 04:04:41 +09:00
}
private function html2text(string $html): string
{
$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;
}
}