aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/Actor.php
diff options
context:
space:
mode:
authorwinter2024-12-09 21:31:54 +0000
committerwinter2024-12-09 21:31:54 +0000
commit4c7bde1400820f36caf8b2a5374007384c3018f3 (patch)
tree83ed26ff368117b8e392f7bc28736482077fc2f4 /Digitigrade/Model/Actor.php
parent50edbd3907c26a2d4d616421de9bb51a4d083c1b (diff)
rename to Digitigrade / PawPub
:3
Diffstat (limited to 'Digitigrade/Model/Actor.php')
-rw-r--r--Digitigrade/Model/Actor.php90
1 files changed, 90 insertions, 0 deletions
diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php
new file mode 100644
index 0000000..946265e
--- /dev/null
+++ b/Digitigrade/Model/Actor.php
@@ -0,0 +1,90 @@
+<?php
+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;
+ public \DateTimeImmutable $created;
+ public ?\DateTimeImmutable $modified;
+ public ?string $homepage;
+ public string $handle;
+ public string $displayName;
+ public ?string $avatar;
+ public ?string $bio;
+ public ?string $pronouns;
+ public bool $automated = false;
+ public bool $requestToFollow = false;
+ public ActorEndpoints $endpoints;
+ public array $extensions = [];
+
+ protected function getUpdateWhereClause(\PDO $db): ?string {
+ if (self::findWhere('uri = ?', [$this->uri]) != null)
+ return 'uri = ' . $db->quote($this->uri);
+ if (self::findWhere('id = ?', [$this->id]) != null)
+ return "id = $this->id";
+ return null;
+ }
+
+ public static function findLocalByHandle(string $handle): ?self {
+ 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',
+ 'self' => path_to_uri("/user/$this->handle"),
+ 'created' => $this->created?->format('c'),
+ 'modified' => $this->modified?->format('c'),
+ 'homepage' => path_to_uri("/$this->handle"),
+ 'handle' => $this->handle,
+ 'displayName' => $this->displayName,
+ 'bio' => $this->bio,
+ 'pronouns' => $this->pronouns,
+ 'automated' => $this->automated,
+ 'endpoints' => [
+ 'basicFeed' => path_to_uri("/user/$this->handle/basicFeed"),
+ 'fullFeed' => path_to_uri("/user/$this->handle/fullFeed")
+ ]
+ ];
+ }
+
+ public function hydrate() {
+ $this->endpoints = ActorEndpoints::findWhere('actor_id = ?', [$this->id]);
+ }
+} \ No newline at end of file