aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/lookup.php
blob: e0a790ee6fe71996f36626f116a36f5de2695856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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());
    }
});