From c3860d4a386ebe32b729041f3817b613ca308599 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Tue, 29 Oct 2024 11:35:48 +0100 Subject: [PATCH] add notifications plugin + created reply comment route --- public/site/config/config.php | 1 - public/site/plugins/comments/routes/reply.php | 42 +++++++++++++++++++ .../site/plugins/comments/routes/update.php | 4 +- public/site/plugins/notifications/index.php | 7 ++++ .../notifications/routes/mark-as-read.php} | 2 +- src/stores/api.js | 2 +- 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 public/site/plugins/comments/routes/reply.php create mode 100644 public/site/plugins/notifications/index.php rename public/site/{config/routes/read-notification.php => plugins/notifications/routes/mark-as-read.php} (87%) diff --git a/public/site/config/config.php b/public/site/config/config.php index e4d6a71..e15869d 100644 --- a/public/site/config/config.php +++ b/public/site/config/config.php @@ -26,7 +26,6 @@ return [ require(__DIR__ . '/routes/save-file.php'), require(__DIR__ . '/routes/remove-file.php'), require(__DIR__ . '/routes/upload-pdf.php'), - require(__DIR__ . '/routes/read-notification.php'), ], 'hooks' => [ 'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'), diff --git a/public/site/plugins/comments/routes/reply.php b/public/site/plugins/comments/routes/reply.php new file mode 100644 index 0000000..f279327 --- /dev/null +++ b/public/site/plugins/comments/routes/reply.php @@ -0,0 +1,42 @@ + '(:all)create-comment.json', + 'method' => 'POST', + 'action' => function () { + $json = file_get_contents('php://input'); + $data = json_decode($json); + + $page = page($data->pageUri); + $file = $page->file($data->fileName); + $user = kirby()->user($data->userUuid); + + + $comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value()); + + $newComment = [ + 'pageUri' => $data->pageUri, + 'fileUuid' => (string) $file->uuid(), + 'filePageTarget' => $data->targetPage, + 'text' => $data->text, + 'user' => [ + 'name' => (string) $user->name(), + 'email' => (string) $user->email(), + 'uuid' => (string) $user->uuid(), + 'role' => (string) $user->role() + ], + 'date' => (string) $data->date, + 'id' => $data->id, + ]; + + $comments[$data->parentId]['replies'][$data->id] = $newComment; + + $newFile = $file->update([ + 'comments' => $comments + ]); + + $user->sendNotification('comments', $newComment); + + return getFileData($newFile); + } +]; \ No newline at end of file diff --git a/public/site/plugins/comments/routes/update.php b/public/site/plugins/comments/routes/update.php index db611e5..6972193 100644 --- a/public/site/plugins/comments/routes/update.php +++ b/public/site/plugins/comments/routes/update.php @@ -1,7 +1,7 @@ '(:all)create-comment.json', + 'pattern' => '(:all)update-comment.json', 'method' => 'POST', 'action' => function () { $json = file_get_contents('php://input'); @@ -21,7 +21,7 @@ return [ 'comments' => $comments ]); - // $user->sendNotification('comments', $comments[$data->id]); + $user->sendNotification('comments', $comments[$data->id]); return getFileData($newFile); } diff --git a/public/site/plugins/notifications/index.php b/public/site/plugins/notifications/index.php new file mode 100644 index 0000000..acfa800 --- /dev/null +++ b/public/site/plugins/notifications/index.php @@ -0,0 +1,7 @@ + [ + require(__DIR__ . '/routes/mark-as-read.php'), + ] +]); diff --git a/public/site/config/routes/read-notification.php b/public/site/plugins/notifications/routes/mark-as-read.php similarity index 87% rename from public/site/config/routes/read-notification.php rename to public/site/plugins/notifications/routes/mark-as-read.php index 7f7125a..15efc47 100644 --- a/public/site/config/routes/read-notification.php +++ b/public/site/plugins/notifications/routes/mark-as-read.php @@ -1,7 +1,7 @@ '(:all)read-notification.json', + 'pattern' => '(:all)mark-as-read.json', 'method' => 'POST', 'action' => function () { $json = file_get_contents('php://input'); diff --git a/src/stores/api.js b/src/stores/api.js index 6a71bd4..4de0fe5 100644 --- a/src/stores/api.js +++ b/src/stores/api.js @@ -154,7 +154,7 @@ export const useApiStore = defineStore("api", () => { }), }; try { - const response = await fetch("/read-notification.json", headers); + const response = await fetch("/mark-as-read.json", headers); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }