diff options
| author | winter | 2024-12-10 18:29:57 +0000 |
|---|---|---|
| committer | winter | 2024-12-10 18:29:57 +0000 |
| commit | ae37b73d1f15ffe5c4c9a32c5de349cd746ebc48 (patch) | |
| tree | 525c6f5f2e8996be383e4ff62c574cb35239c8fb /Digitigrade | |
| parent | 4c7bde1400820f36caf8b2a5374007384c3018f3 (diff) | |
cache webfinger lookups
Diffstat (limited to 'Digitigrade')
| -rw-r--r-- | Digitigrade/Model/Actor.php | 26 | ||||
| -rw-r--r-- | Digitigrade/Model/ActorWebfinger.php | 58 |
2 files changed, 61 insertions, 23 deletions
diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php index 946265e..3c416ee 100644 --- a/Digitigrade/Model/Actor.php +++ b/Digitigrade/Model/Actor.php @@ -4,8 +4,6 @@ namespace Digitigrade\Model; use Digitigrade\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; @@ -42,27 +40,9 @@ class Actor extends FetchableModel implements \JsonSerializable { } $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); + return ActorWebfinger::findByAcct($acct, $autoSave, $forceRefetch)->actor; } public function jsonSerialize(): array { @@ -87,4 +67,4 @@ class Actor extends FetchableModel implements \JsonSerializable { public function hydrate() { $this->endpoints = ActorEndpoints::findWhere('actor_id = ?', [$this->id]); } -}
\ No newline at end of file +} diff --git a/Digitigrade/Model/ActorWebfinger.php b/Digitigrade/Model/ActorWebfinger.php new file mode 100644 index 0000000..4169874 --- /dev/null +++ b/Digitigrade/Model/ActorWebfinger.php @@ -0,0 +1,58 @@ +<?php +namespace Digitigrade\Model; + +use Digitigrade\Model; + +class ActorWebfinger extends Model { + public const REL_URI = "https://pawpub.entities.org.uk/rel/actor.html"; + + public string $acct; + public Actor $actor; + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (self::findWhere('acct = ?', [$this->acct]) != null) { + return 'acct = ' . $db->quote($this->acct); + } + return null; + } + + public static function findByAcct(string $acct, bool $autoSave = true, bool $forceRefetch = false): ?self { + if (!$forceRefetch) { + $entry = self::findWhere('acct = ?', [$acct]); + if ($entry != null) + return $entry; + } + + // fetch from remote + $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; + $actor = Actor::findByUri($matching[0]->href, $autoSave, $forceRefetch); + + // save and return + $entry = new self(); + $entry->acct = $acct; + $entry->actor = $actor; + + if ($autoSave) + $entry->save(); + + return $entry; + } +}
\ No newline at end of file |
