From 09a42d23cd067dc5c2a7d4f03e8f074a9317f6c8 Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 28 Jan 2025 16:59:43 +0000 Subject: implement extensions on existing objects --- Digitigrade/Model/Actor.php | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'Digitigrade/Model/Actor.php') diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php index 62d1b39..2b9a867 100644 --- a/Digitigrade/Model/Actor.php +++ b/Digitigrade/Model/Actor.php @@ -1,6 +1,7 @@ endpoints = ActorEndpoints::findWhere('actor_id = ?', [$this->id]); + $this->extensions = $this->findExtensions(); + } + + protected function dehydrate() { + $this->saveExtensions(); + } + + private function findExtensions() { + $pdo = Db::getInstance()->getPdo(); + $stmt = $pdo->prepare('SELECT uri, data FROM actor_extension WHERE actor_id = ?'); + $stmt->execute([$this->id]); + $items = []; + while ($row = $stmt->fetch()) { + $items[$row['uri']] = json_decode($row['data']); + } + return $items; + } + + private function saveExtensions() { + $pdo = Db::getInstance()->getPdo(); + foreach ($this->extensions as $uri => $obj) { + $stmt = $pdo->prepare( + 'INSERT INTO actor_extension(actor_id, uri, data) VALUES (?, ?, ?) ' . + 'ON CONFLICT (actor_id, uri) DO UPDATE SET data = EXCLUDED.data' + ); + $stmt->execute([$this->id, $uri, json_encode($obj)]); + } + } + public static function findLocalByHandle(string $handle): ?self { return self::findWhere('is_local = true AND handle = ?', [$handle]); } @@ -323,8 +355,4 @@ class Actor extends PushableModel implements RpcReceiver { ] ]; } - - public function hydrate() { - $this->endpoints = ActorEndpoints::findWhere('actor_id = ?', [$this->id]); - } } -- cgit v1.3