aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/lookup.php
blob: 62d9e487397313f841ea9fe1832f981f4b6010a2 (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
<?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 || $object instanceof Actor) {
        throw new TemporaryRedirect($object->getLocalUiHref());
    }
});