diff options
Diffstat (limited to 'Digitigrade/Model/ActorWebfinger.php')
| -rw-r--r-- | Digitigrade/Model/ActorWebfinger.php | 58 |
1 files changed, 58 insertions, 0 deletions
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 |
