diff options
| author | winter | 2025-01-17 19:07:47 +0000 |
|---|---|---|
| committer | winter | 2025-01-17 19:07:47 +0000 |
| commit | febd52ce20185486ae69fcb100df9c53f3f7e77d (patch) | |
| tree | 9a65efdb7376e4f42bfdf1eabf638d29644c2c17 | |
| parent | 8d7727e6d1a8f69dbe43390a3ef2f7a7880c42bc (diff) | |
add lookup (by uri) route
| -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 |
