diff options
| author | winter | 2025-03-16 00:53:52 +0000 |
|---|---|---|
| committer | winter | 2025-03-16 00:53:52 +0000 |
| commit | 145b5db9474e32ca3430fe35eaa7498d7f26ff95 (patch) | |
| tree | df1da516a05cd382e19a815d4e7e6bee314e3007 /Digitigrade/Model | |
| parent | a0a86c3a98a6af158ecd2f1923feb752c82907db (diff) | |
implement poking and such
Diffstat (limited to 'Digitigrade/Model')
| -rw-r--r-- | Digitigrade/Model/Actor.php | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/Digitigrade/Model/Actor.php b/Digitigrade/Model/Actor.php index 0541e1b..18aba15 100644 --- a/Digitigrade/Model/Actor.php +++ b/Digitigrade/Model/Actor.php @@ -3,12 +3,16 @@ namespace Digitigrade\Model; use Digitigrade\Db; use Digitigrade\Notification\PendingFollowActionedNotif; +use Digitigrade\Notification\PokeNotif; use Digitigrade\Notification\UnblockNotif; use Digitigrade\Notification\UnfollowNotif; +use Digitigrade\PokeVerb; use Digitigrade\RpcException; use Digitigrade\RpcReceiver; class Actor extends PushableModel implements RpcReceiver { + public const EXTENSION_POKE = 'https://pawpub.entities.org.uk/extension/poke'; + public ?int $id; public string $uri; public bool $isLocal = false; @@ -135,15 +139,15 @@ class Actor extends PushableModel implements RpcReceiver { return self::findByUri($uri); } - public function rpcCall(string $method, array $args) { - // $args = [Actor $actingAs] + public function rpcCall(string $method, array $args, string $customEndpoint = null) { + // $args = [Actor $actingAs, ?string $requestBody] if (!$this->isLocal) { // trigger the same action on the remote server - $endpoint = $this->endpoints->{$method} ?? null; + $endpoint = $customEndpoint ?? $this->endpoints->{$method} ?? null; if (!isset($endpoint)) { throw new RpcException("Actor `$this->uri` has no $method endpoint"); } - $result = send_request_authenticated($endpoint, method: 'POST', actingAs: $args[0]); + $result = send_request_authenticated($endpoint, method: 'POST', actingAs: $args[0], body: $args[1] ?? null); if (!str_contains($result->headers[0], ' 20')) { throw new RpcException('remote call did not indicate success: ' . $result->headers[0]); } @@ -188,6 +192,16 @@ class Actor extends PushableModel implements RpcReceiver { (new UnblockNotif($args[0], $this))->processNotifications(); return; + case 'poke': + $obj = json_decode($args[1] ?? ''); + (new PokeNotif( + $args[0], + PokeVerb::tryFrom($obj?->verb ?? '') ?? PokeVerb::POKE, + $this, + $obj?->urgent ?? false + ))->processNotifications(); + return; + default: throw new \RuntimeException("behaviour for actor rpc method $method not (yet?) implemented"); } @@ -329,6 +343,25 @@ class Actor extends PushableModel implements RpcReceiver { return '/@/' . $this->getFullHandle(); } + private function getPokeEndpoint(): ?string { + return $this->extensions[self::EXTENSION_POKE] ?? null; + } + + public function poke(self $target, PokeVerb $verb = PokeVerb::POKE, bool $urgent = false) { + $target->rpcCall('poke', [$this, json_encode([ + 'verb' => $verb->value, + 'urgent' => $urgent + ])], $target->getPokeEndpoint()); + } + + public function isPokeable(): bool { + return isset($this->extensions[self::EXTENSION_POKE]); + } + + public function setPokeable(bool $pokeable) { + $this->extensions[self::EXTENSION_POKE] = $pokeable ? path_to_uri("/actor/$this->id/poke") : null; + } + public function jsonSerialize(): array { if ($this->deleted) { return [ @@ -359,7 +392,8 @@ class Actor extends PushableModel implements RpcReceiver { 'rejectedFollow' => path_to_uri("/actor/$this->id/rejectedFollow"), 'block' => path_to_uri("/actor/$this->id/block"), 'unblock' => path_to_uri("/actor/$this->id/unblock"), - ] + ], + 'extensions' => $this->extensions ]; } } |
