diff options
| author | winter | 2025-01-28 16:59:43 +0000 |
|---|---|---|
| committer | winter | 2025-01-28 16:59:43 +0000 |
| commit | 09a42d23cd067dc5c2a7d4f03e8f074a9317f6c8 (patch) | |
| tree | c01ea939a0c72c2a4ed5b6479d7739f3dbaaf01f /Digitigrade/Model/Actor.php | |
| parent | 4c66a437d3125ef1a03bac96b80c82e9da060a81 (diff) | |
implement extensions on existing objects
Diffstat (limited to 'Digitigrade/Model/Actor.php')
| -rw-r--r-- | Digitigrade/Model/Actor.php | 36 |
1 files changed, 32 insertions, 4 deletions
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 @@ <?php namespace Digitigrade\Model; +use Digitigrade\Db; use Digitigrade\Notification\PendingFollowActionedNotif; use Digitigrade\Notification\UnblockNotif; use Digitigrade\Notification\UnfollowNotif; @@ -76,6 +77,37 @@ class Actor extends PushableModel implements RpcReceiver { return null; } + protected function hydrate() { + $this->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]); - } } |
