aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Digitigrade/Model/Actor.php26
-rw-r--r--Digitigrade/Model/ActorWebfinger.php58
-rw-r--r--migrations/20241210_181118_create_actor_webfinger.php13
3 files changed, 74 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
diff --git a/migrations/20241210_181118_create_actor_webfinger.php b/migrations/20241210_181118_create_actor_webfinger.php
new file mode 100644
index 0000000..6b6c497
--- /dev/null
+++ b/migrations/20241210_181118_create_actor_webfinger.php
@@ -0,0 +1,13 @@
+<?php
+
+use \Digitigrade\Db\Migrator;
+
+Migrator::getInstance()->register(20241210_181118, function (PDO $db) {
+ // webfinger lookup cache for actors
+ $db->exec(<<<END
+ CREATE TABLE actor_webfinger (
+ acct text primary key,
+ actor bigint not null references actor
+ );
+ END);
+});