diff options
| author | winter | 2025-03-15 18:35:59 +0000 |
|---|---|---|
| committer | winter | 2025-03-15 18:52:54 +0000 |
| commit | 61b122e88cc6783b4d0cf5142a22002f8e558c60 (patch) | |
| tree | 847b864a93f716157456891d4526077e5f0b93f7 /routes | |
| parent | cb904ef3fb05ab106d5e5e12f6273b8117db4216 (diff) | |
implement pages
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/lookup.php | 6 | ||||
| -rw-r--r-- | routes/note.php | 5 | ||||
| -rw-r--r-- | routes/pages.php | 76 |
3 files changed, 80 insertions, 7 deletions
diff --git a/routes/lookup.php b/routes/lookup.php index e0a790e..62d9e48 100644 --- a/routes/lookup.php +++ b/routes/lookup.php @@ -34,9 +34,7 @@ Router::getInstance()->mount('/lookup', function (array $args) { 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()); + if ($object instanceof Note || $object instanceof Actor) { + throw new TemporaryRedirect($object->getLocalUiHref()); } });
\ No newline at end of file diff --git a/routes/note.php b/routes/note.php index 2612f0f..6dd0025 100644 --- a/routes/note.php +++ b/routes/note.php @@ -2,7 +2,6 @@ use Digitigrade\HttpResponseStatus\Forbidden; use Digitigrade\HttpResponseStatus\NotFound; -use Digitigrade\HttpResponseStatus\PolicyRejected; use Digitigrade\HttpResponseStatus\TemporaryRedirect; use Digitigrade\Model\Instance; use Digitigrade\Model\Note; @@ -22,7 +21,7 @@ Router::getInstance()->mount('/note/:id', function (array $args) { } PolicyManager::getInstance()->checkFederationOrThrow($note, $instance); if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'text/html')) { - throw new TemporaryRedirect('/@/' . $note->author->handle . "/note/$note->id"); + throw new TemporaryRedirect($note->getLocalUiHref()); } // if it's not public we need to check whether the requesting instance is allowed to see it if ($note->privacy->scope != NotePrivacyScope::PUBLIC ) { @@ -49,7 +48,7 @@ Router::getInstance()->mount('/@/:handle/note/:id', function (array $args) { } // 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"); + throw new TemporaryRedirect($note->getLocalUiHref()); } render_template('thread_page', ['note' => $note]); }); diff --git a/routes/pages.php b/routes/pages.php new file mode 100644 index 0000000..03ec4d4 --- /dev/null +++ b/routes/pages.php @@ -0,0 +1,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('note_page', ['note' => $note]); +});
\ No newline at end of file |
