From ae37b73d1f15ffe5c4c9a32c5de349cd746ebc48 Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 10 Dec 2024 18:29:57 +0000 Subject: cache webfinger lookups --- Digitigrade/Model/ActorWebfinger.php | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Digitigrade/Model/ActorWebfinger.php (limited to 'Digitigrade/Model/ActorWebfinger.php') 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 @@ +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 -- cgit v1.3