aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/pages.php
blob: 418436f350f4602f458e496f7e83098ecfb4d5ec (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

use Digitigrade\FormattingProvider\CommonMark;
use Digitigrade\HttpResponseStatus\Forbidden;
use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\HttpResponseStatus\PolicyRejected;
use Digitigrade\HttpResponseStatus\TemporaryRedirect;
use Digitigrade\Model\Note;
use Digitigrade\Model\NotePrivacyScope;
use Digitigrade\Model\UserAccount;
use Digitigrade\Page;
use Digitigrade\PolicyManager;
use Digitigrade\Router;

Router::getInstance()->mount('/pages', function (array $args) {
    // list all pages
    // it would be silly to show this to logged out users honestly
    UserAccount::requireByCurrentSession();
    render_template('pages/list_page');
});

Router::getInstance()->mount('/pages/new', function (array $args) {
    // creating a new page
    $user = UserAccount::requireByCurrentSession();
    if (isset($_POST['title'])) {
        $title = trim($_POST['title']);
        $body = trim($_POST['body']);
        $note = Note::create(
            $user->actor,
            $title,
            get_ui_language(),  // TODO: actual language selector/detection
            null,
            NotePrivacyScope::from($_POST['scope'])
        );
        $note->plainContent .= ' - ' . path_to_uri($note->getLocalUiHref(true));
        $note->setPage(Page::create($title, [
            'text/markdown' => $body,
            'text/html' => CommonMark::getInstance()->renderToHtml($body)
        ]));
        if (!PolicyManager::getInstance()->check($note)) {
            $note->remove();
            throw new PolicyRejected();
        }
        $note->save();
        $note->publish();
        $note->processTimelineAdditions();
        $note->processNotifications();

        throw new TemporaryRedirect($note->getLocalUiHref(true));
    } else {
        render_template('pages/new_page');
    }
});

Router::getInstance()->mount('/@/:handle/page/:id', function (array $args) {
    $note = Note::find($args['id']);
    if ($note == null || $note->deleted) {
        throw new NotFound("i don't know that note");
    }
    // check the current user is allowed to see it
    if ($note->privacy->scope != NotePrivacyScope::PUBLIC ) {
        $user = UserAccount::requireByCurrentSession();
        if (!in_array($user->actor, $note->getRelevantActors())) {
            throw new Forbidden();
        }
    }
    // go back to the note if it's not a page
    if (!$note->hasPage()) {
        throw new TemporaryRedirect($note->getLocalUiHref());
    }
    // change the handle in the url if it's wrong
    if ($args['handle'] != $note->author->getFullHandle()) {
        throw new TemporaryRedirect($note->getLocalUiHref(true));
    }
    render_template('pages/page', ['note' => $note]);
});