aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/note.php
blob: 8a6c9dac7d14526faa89114f34a389ebb8a33276 (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
<?php

use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\HttpResponseStatus\TemporaryRedirect;
use Digitigrade\Model\Note;
use Digitigrade\Router;

Router::getInstance()->mount('/note/:id', function (array $args) {
    $note = Note::find($args['id']);
    if ($note == null || $note->deleted) {
        throw new NotFound("i don't know that note");
    }
    if (!$note->author->isLocal) {
        throw new NotFound("i don't want to tell you about non local notes sorry");
    }
    if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'text/html')) {
        throw new TemporaryRedirect('/@/' . $note->author->handle . "/note/$note->id");
    }
    json_response($note);
});

Router::getInstance()->mount('/@/:handle/note/:id', function (array $args) {
    $note = Note::find($args['id']);
    if ($note == null || $note->deleted) {
        throw new NotFound("i don't know that note");
    }
    // change the handle in the url if it's wrong
    if ($args['handle'] != $note->author->getFullHandle()) {
        throw new TemporaryRedirect('/@/' . $note->author->getFullHandle() . "/note/$note->id");
    }
    render_template('thread_page', ['note' => $note]);
});