diff options
| author | winter | 2024-12-17 18:39:18 +0000 |
|---|---|---|
| committer | winter | 2024-12-17 18:39:18 +0000 |
| commit | 085762ec174eae1c519ec14db632f5ba197ed3c7 (patch) | |
| tree | 9ad49bc839b5f72ded47c119ff01a09dfa0b934a /routes/push.php | |
| parent | bc2cd99b108b8755a648c3915714e7c0e0771548 (diff) | |
hopefully implement push!
again i can't really test this yet because i don't have two https instances that can talk to each other.
soon i will set that up and test all these recent commits
Diffstat (limited to 'routes/push.php')
| -rw-r--r-- | routes/push.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/routes/push.php b/routes/push.php new file mode 100644 index 0000000..6712fb4 --- /dev/null +++ b/routes/push.php @@ -0,0 +1,50 @@ +<?php + +use Digitigrade\HttpResponseStatus\BadRequest; +use Digitigrade\HttpResponseStatus\Forbidden; +use Digitigrade\HttpResponseStatus\Unauthorized; +use Digitigrade\Model\Actor; +use Digitigrade\Model\Instance; +use Digitigrade\Model\Note; +use Digitigrade\Router; + +Router::getInstance()->mount('/push', function (array $args) { + // receive incoming pushes + $instance = Instance::findByRequestHeaders(); + if ($instance == null) { + throw new Unauthorized('please identify yourself with a valid Authorization header!'); + } + if (!$instance->auth->inboundPushEnabled) { + throw new Forbidden('you are not permitted to push as i have not subscribed to you'); + } + + $body = file_get_contents('php://input'); + $obj = json_decode($body); + if ($obj === false) { + throw new BadRequest("request body doesn't look like valid json"); + } + if (!isset($obj->type, $obj->self)) { + throw new BadRequest('the object needs to have `type` and `self` properties'); + } + if (!str_starts_with($obj->self, 'https://')) { + throw new BadRequest('dodgy looking `self` uri!'); + } + if (hostname_from_uri($obj->self) != $instance->domain) { + throw new Forbidden('you may not push objects belonging to a different instance'); + } + + switch ($obj->type) { + case 'actor': + Actor::importFromReceivedObject($obj); + break; + case 'note': + Note::importFromReceivedObject($obj); + break; + case 'interaction': + case 'extension': + case 'tombstone': + throw new \RuntimeException('object type not yet implemented :('); + default: + throw new BadRequest('invalid object type'); + } +});
\ No newline at end of file |
