diff options
| author | winter | 2025-03-15 21:26:02 +0000 |
|---|---|---|
| committer | winter | 2025-03-15 21:26:02 +0000 |
| commit | 8843a8cb197e71b1ca42f1ea56be5f806ad0f5b4 (patch) | |
| tree | 20ca690e9bd3e4996421eb4d43c0b4fbdaf0ff95 /routes | |
| parent | d8e440b1998e5c3e33445f2ec020d261fc7fa714 (diff) | |
implement note editing
and improve display of edited note timestamps
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/note_fragment.php | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/routes/note_fragment.php b/routes/note_fragment.php index 94ebc07..9cc9143 100644 --- a/routes/note_fragment.php +++ b/routes/note_fragment.php @@ -14,16 +14,24 @@ use Digitigrade\Model\StorageObject; use Digitigrade\Model\UserAccount; use Digitigrade\PolicyManager; use Digitigrade\Router; -use Digitigrade\StorageProvider; use Digitigrade\StorageProvider\FilesystemStorage; Router::getInstance()->mount('/fragment/note/:id', function (array $args) { + $note = Note::find($args['id']); if ($_SERVER['REQUEST_METHOD'] != 'DELETE') { - throw new RuntimeException('method not implemented'); + // just render the note + // but 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(); + } + } + render_template('note/note', ['note' => Note::find($args['id'])]); + return; } // assuming we're deleting the note $user = UserAccount::requireByCurrentSession(); - $note = Note::find($args['id']); if ($note->author != $user->actor && !$user->isAdmin) { throw new Forbidden('you may not delete that note!'); } @@ -108,6 +116,34 @@ Router::getInstance()->mount('/fragment/note/:id/reply', function (array $args) render_template('note/reply_form', ['note' => $note]); }); +Router::getInstance()->mount('/fragment/note/:id/edit', function (array $args) { + // editing an existing note + $user = UserAccount::requireByCurrentSession(); + $note = Note::find($args['id']); + if ($note->author != $user->actor && !$user->isAdmin) { + throw new Forbidden('you may not edit that note!'); + } + if (!$note->author->isLocal) { + throw new BadRequest("can't edit a non-local note"); + } + + if (isset($_POST['content']) && strlen(trim($_POST['content'])) > 0) { + $note->summary = trim($_POST['summary'] ?? '') ?: null; + $content = trim($_POST['content']); + $note->plainContent = CommonMark::getInstance()->renderToPlainText($content); + $note->formattedContent = [ + 'text/markdown' => $content, + 'text/html' => CommonMark::getInstance()->renderToHtml($content) + ]; + $note->modified = new DateTimeImmutable(); + $note->save(); + $note->publish(); + render_template('note/note', ['note' => $note]); + } else { + render_template('note/editable', ['note' => $note]); + } +}); + Router::getInstance()->mount('/fragment/note', function (array $args) { // posting a new note $author = UserAccount::requireByCurrentSession(); |
