diff options
| author | winter | 2024-12-09 21:13:16 +0000 |
|---|---|---|
| committer | winter | 2024-12-09 21:13:16 +0000 |
| commit | 1ab1c4d9c6a28e9929052bd00f15866eb1a5a200 (patch) | |
| tree | 1396a368e7e05eb3c701d69deb9311d7361dd2ba /WpfTest/Model/Actor.php | |
| parent | bda28640d6e066ae338c6f31407df274ed09f026 (diff) | |
implement webfinger lookups
Diffstat (limited to 'WpfTest/Model/Actor.php')
| -rw-r--r-- | WpfTest/Model/Actor.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/WpfTest/Model/Actor.php b/WpfTest/Model/Actor.php index 0b0e729..a745a96 100644 --- a/WpfTest/Model/Actor.php +++ b/WpfTest/Model/Actor.php @@ -4,6 +4,8 @@ namespace WpfTest\Model; use WpfTest\RemoteFetchable; class Actor extends FetchableModel implements \JsonSerializable { + public const REL_URI = "https://pawpub.entities.org.uk/rel/actor.html"; + public ?int $id; public string $uri; public bool $isLocal = false; @@ -32,6 +34,37 @@ class Actor extends FetchableModel implements \JsonSerializable { return self::findWhere('is_local = true AND handle = ?', [$handle]); } + public static function findByWebfinger(string $acct, bool $autoSave = true, bool $forceRefetch = false): ?self { + // normalise uri + if (!str_starts_with($acct, 'acct:')) { + if (str_starts_with($acct, '@')) { + $acct = substr($acct, 1); + } + $acct = "acct:$acct"; + } + // fetch it + $domain = explode('@', $acct, 2)[1]; + $data = @file_get_contents("https://$domain/.well-known/webfinger?resource=$acct", + context: stream_context_create([ + 'http' => ['header' => 'Accept: application/jrd+json, application/json'] + ]) + ); + if ($data === false) + return null; + $data = json_decode($data); + if ($data === null) + return null; + // find the right link + if (!isset($data->links)) + return null; + $matching = array_filter($data->links, function (\stdClass $linkObj) { + return $linkObj->rel == self::REL_URI; + }); + if (count($matching) == 0) + return null; + return self::findByUri($matching[0]->href, $autoSave, $forceRefetch); + } + public function jsonSerialize(): array { return [ 'type' => 'actor', |
