diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/lookup.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/routes/lookup.php b/routes/lookup.php new file mode 100644 index 0000000..e0a790e --- /dev/null +++ b/routes/lookup.php @@ -0,0 +1,42 @@ +<?php + +use Digitigrade\HttpResponseStatus\BadRequest; +use Digitigrade\HttpResponseStatus\NotFound; +use Digitigrade\HttpResponseStatus\TemporaryRedirect; +use Digitigrade\Model\Actor; +use Digitigrade\Model\Note; +use Digitigrade\Router; + +Router::getInstance()->mount('/lookup', function (array $args) { + if (!isset($_GET['uri'])) { + throw new BadRequest('please provide a `uri` query parameter'); + } + $uri = $_GET['uri']; + if (!str_starts_with($uri, 'https://')) { + throw new BadRequest('dodgy looking uri'); + } + + // try looking it up as a note + try { + $object = Note::findByUri($uri); + } catch (\Throwable $ignored) { + } + + // then as an actor + if (!isset($object)) { + try { + $object = Actor::findByUri($uri); + } catch (\Throwable $ignored) { + } + } + + if (!isset($object)) { + throw new NotFound("i can't find any object with that uri, sorry!"); + } + + if ($object instanceof Note) { + throw new TemporaryRedirect('/@/' . $object->author->getFullHandle() . "/note/$object->id"); + } elseif ($object instanceof Actor) { + throw new TemporaryRedirect('/@/' . $object->getFullHandle()); + } +});
\ No newline at end of file |
