diff --git a/app/MetadataResolver/ActivityPubResolver.php b/app/MetadataResolver/ActivityPubResolver.php index 7cf3e00..c0580c8 100644 --- a/app/MetadataResolver/ActivityPubResolver.php +++ b/app/MetadataResolver/ActivityPubResolver.php @@ -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']) : ''; diff --git a/app/MetadataResolver/MetadataResolver.php b/app/MetadataResolver/MetadataResolver.php index bb2ec08..41869b8 100644 --- a/app/MetadataResolver/MetadataResolver.php +++ b/app/MetadataResolver/MetadataResolver.php @@ -48,16 +48,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)) { @@ -70,7 +73,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ヘッダだと判断して全部無視してしまう。 @@ -115,6 +118,6 @@ class MetadataResolver implements Resolver // 5xx は変なAcceptが原因かもしれない(?)ので無視してフォールバック } - return null; + throw new UnsupportedContentException(); } } diff --git a/app/MetadataResolver/UnsupportedContentException.php b/app/MetadataResolver/UnsupportedContentException.php new file mode 100644 index 0000000..6ca6ed3 --- /dev/null +++ b/app/MetadataResolver/UnsupportedContentException.php @@ -0,0 +1,12 @@ +