Merge pull request #303 from unarist/fix/ap-actor

MastodonのプロフィールURLをいい感じに表示する
This commit is contained in:
shibafu 2019-12-10 23:43:52 +09:00 committed by GitHub
commit 00345eedca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 8 deletions

View File

@ -37,6 +37,10 @@ class ActivityPubResolver implements Resolver, Parser
$activityOrObject = json_decode($json, true);
$object = $activityOrObject['object'] ?? $activityOrObject;
if ($object['type'] !== 'Note') {
throw new UnsupportedContentException('Unsupported object type: ' . $object['type']);
}
$metadata = new Metadata();
$metadata->title = isset($object['attributedTo']) ? $this->getTitleFromActor($object['attributedTo']) : '';

View File

@ -49,16 +49,19 @@ class MetadataResolver implements Resolver
{
foreach ($this->rules as $pattern => $class) {
if (preg_match($pattern, $url) === 1) {
/** @var Resolver $resolver */
$resolver = app($class);
try {
/** @var Resolver $resolver */
$resolver = app($class);
return $resolver->resolve($url);
return $resolver->resolve($url);
} catch (UnsupportedContentException $e) {
}
}
}
$result = $this->resolveWithAcceptHeader($url);
if ($result !== null) {
return $result;
try {
return $this->resolveWithAcceptHeader($url);
} catch (UnsupportedContentException $e) {
}
if (isset($this->defaultResolver)) {
@ -71,7 +74,7 @@ class MetadataResolver implements Resolver
throw new \UnexpectedValueException('URL not matched.');
}
public function resolveWithAcceptHeader(string $url): ?Metadata
public function resolveWithAcceptHeader(string $url): Metadata
{
try {
// Rails等はAcceptに */* が入っていると、ブラウザの適当なAcceptヘッダだと判断して全部無視してしまう。
@ -116,6 +119,6 @@ class MetadataResolver implements Resolver
// 5xx は変なAcceptが原因かもしれないので無視してフォールバック
}
return null;
throw new UnsupportedContentException();
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\MetadataResolver;
use Exception;
/**
* このResolverやParserが対応していないサイトであったことを表わします。
*/
class UnsupportedContentException extends Exception
{
}