aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/note_fragment.php
diff options
context:
space:
mode:
Diffstat (limited to 'routes/note_fragment.php')
-rw-r--r--routes/note_fragment.php42
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();