diff --git a/public/site/plugins/comments/index.php b/public/site/plugins/comments/index.php
index 0fcc8bb..703fd41 100644
--- a/public/site/plugins/comments/index.php
+++ b/public/site/plugins/comments/index.php
@@ -12,5 +12,6 @@ Kirby::plugin('adrienpayet/kirby4-comments', [
require(__DIR__ . '/routes/update.php'),
require(__DIR__ . '/routes/delete.php'),
require(__DIR__ . '/routes/reply.php'),
+ require(__DIR__ . '/routes/read.php'),
]
]);
diff --git a/public/site/plugins/comments/routes/delete.php b/public/site/plugins/comments/routes/delete.php
index eba45cb..8fb658a 100644
--- a/public/site/plugins/comments/routes/delete.php
+++ b/public/site/plugins/comments/routes/delete.php
@@ -38,7 +38,7 @@ return [
echo json_encode(getFileData($newFile));
- kirby()->user()->deleteNotification($page, $data->id);
+ kirby()->user()->deleteNotification($page->managers()->toUsers(), $data->id);
exit;
}
diff --git a/public/site/plugins/comments/routes/read.php b/public/site/plugins/comments/routes/read.php
new file mode 100644
index 0000000..2bcd03d
--- /dev/null
+++ b/public/site/plugins/comments/routes/read.php
@@ -0,0 +1,28 @@
+ '(:all)read-comment.json',
+ 'method' => 'POST',
+ 'action' => function () {
+ $json = file_get_contents('php://input');
+ $data = json_decode($json);
+
+ try {
+ $newNotification = kirby()->user()->readNotification($data->notificationId);
+ if ($newNotification) {
+ return [
+ "success" => "Notification read.",
+ "data" => $newNotification
+ ];
+ } else {
+ throw new Exception("Can't find corresponding notification.", 1);
+ }
+ } catch (\Throwable $th) {
+ return [
+ "error" => $th->getMessage(),
+ "notificationId" => $data->notificationId,
+ "userName" => kirby()->user()->name()->value()
+ ];
+ }
+ }
+];
diff --git a/public/site/plugins/comments/src/BaseComment.php b/public/site/plugins/comments/src/BaseComment.php
index b647d4f..7b86fea 100644
--- a/public/site/plugins/comments/src/BaseComment.php
+++ b/public/site/plugins/comments/src/BaseComment.php
@@ -43,6 +43,7 @@ class BaseComment {
$this->date = $date;
$this->id = $id;
$this->type = $type;
+ $this->isRead = false;
$this->position = $position;
}
@@ -78,6 +79,20 @@ class BaseComment {
return $this->type;
}
+ public function isRead() {
+ return $this->isRead;
+ }
+
+ public function read() {
+ $this->isRead = true;
+ return $this->isRead;
+ }
+
+ public function unread() {
+ $this->isRead = false;
+ return $this->isRead;
+ }
+
public function pageIndex() {
$this->position['pageIndex'];
}
@@ -93,6 +108,7 @@ class BaseComment {
'date' => $this->date,
'id' => $this->id,
'type' => $this->type,
+ 'isRead' => $this->isRead
];
}
}
diff --git a/public/site/plugins/notifications/index.php b/public/site/plugins/notifications/index.php
index 7a80fd1..e33db2b 100644
--- a/public/site/plugins/notifications/index.php
+++ b/public/site/plugins/notifications/index.php
@@ -1,11 +1,9 @@
[
- // require(__DIR__ . '/routes/mark-as-read.php'),
- // ],
'userMethods' => [
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
- 'deleteNotification' => require(__DIR__ . '/user-methods/delete.php')
+ 'deleteNotification' => require(__DIR__ . '/user-methods/delete.php'),
+ 'readNotification' => require(__DIR__ . '/user-methods/read.php')
]
]);
diff --git a/public/site/plugins/notifications/user-methods/delete.php b/public/site/plugins/notifications/user-methods/delete.php
index 20b20b9..b9e029a 100644
--- a/public/site/plugins/notifications/user-methods/delete.php
+++ b/public/site/plugins/notifications/user-methods/delete.php
@@ -1,25 +1,22 @@
managers()->toUsers();
-
- foreach ($projectManagers as $user) {
- try {
- $notifications = $user->notifications()->isNotEmpty()
- ? Yaml::decode($user->notifications()->value())
- : [];
-
- foreach ($notifications as $key => $notification) {
- if ($notification['id'] === $notificationId) {
- unset($notifications[$key]);
- }
+return function($notificationId) {
+ $user = kirby()->user();
+ try {
+ $notifications = $user->notifications()->isNotEmpty()
+ ? Yaml::decode($user->notifications()->value())
+ : [];
+
+ foreach ($notifications as $key => $notification) {
+ if ($notification['id'] === $notificationId) {
+ unset($notifications[$key]);
}
-
- $user->update([
- 'notifications' => Yaml::encode(array_values($notifications))
- ]);
- } catch (\Throwable $th) {
- throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
+
+ $user->update([
+ 'notifications' => Yaml::encode(array_values($notifications))
+ ]);
+ } catch (\Throwable $th) {
+ throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
};
diff --git a/public/site/plugins/notifications/user-methods/read.php b/public/site/plugins/notifications/user-methods/read.php
index b039807..1bbc851 100644
--- a/public/site/plugins/notifications/user-methods/read.php
+++ b/public/site/plugins/notifications/user-methods/read.php
@@ -1,16 +1,26 @@
notifications()->value());
+return function($notificationId) {
try {
- $notifications[$group][$notificationId]['isRead'] = true;
+ $notifications = $this->notifications()->isNotEmpty()
+ ? Yaml::decode($this->notifications()->value())
+ : [];
+
+ $newNotification = null;
+ foreach ($notifications as $key => $notification) {
+ if ($notification['id'] === $notificationId) {
+ $notifications[$key]['isRead'] = true;
+ $newNotification = $notifications[$key];
+ }
+ }
+
+ $this->update([
+ 'notifications' => Yaml::encode(array_values($notifications))
+ ]);
+
+ return $newNotification;
} catch (\Throwable $th) {
- //throw $th;
+ throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
-
- $this->update([
- "notifications" => $notifications
- ]);
- return $notifications;
};
\ No newline at end of file
diff --git a/public/site/plugins/notifications/user-methods/send.php b/public/site/plugins/notifications/user-methods/send.php
index 9775fbe..72db18b 100644
--- a/public/site/plugins/notifications/user-methods/send.php
+++ b/public/site/plugins/notifications/user-methods/send.php
@@ -14,11 +14,11 @@
*/
return function ($projectUri, $notificationData) {
- $notificationData['isRead'] = false;
$recipients = page($projectUri)->managers()->toUsers()->not($this);
if (!$recipients) return;
+ $notificationData['isRead'];
foreach ($recipients as $otherUser) {
try {
$notifications = $otherUser->notifications()->isNotEmpty()
diff --git a/src/components/comments/Comment.vue b/src/components/comments/Comment.vue
index 38b5664..0641515 100644
--- a/src/components/comments/Comment.vue
+++ b/src/components/comments/Comment.vue
@@ -3,6 +3,7 @@
:id="`comment-${comment.id}`"
class="comment | flow"
:data-status="status"
+ @click="read()"
>
@@ -15,7 +16,7 @@
{{ formatDate() }}