From 246d21f85a87079446b772c9ab548cb6eb87f516 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Fri, 10 Jan 2025 17:40:45 +0100 Subject: [PATCH] read comment notification working --- .../site/blueprints/fields/notifications.yml | 5 +- public/site/blueprints/users/admin.yml | 3 - public/site/blueprints/users/client.yml | 5 -- public/site/blueprints/users/pochet.yml | 1 - public/site/controllers/site.php | 5 +- .../site/plugins/comments/routes/create.php | 2 +- .../plugins/notifications/routes/read.php | 16 +++- .../notifications/src/Notification.php | 4 +- .../notifications/src/NotificationsPage.php | 31 +++++++- src/components/Menu.vue | 8 +- src/components/comments/Comment.vue | 4 +- src/components/notifications/Comment.vue | 1 - src/components/notifications/Content.vue | 1 - src/components/notifications/Reply.vue | 1 - src/stores/api.js | 76 +------------------ src/stores/user.js | 33 ++++++-- src/views/Notifications.vue | 1 + 17 files changed, 86 insertions(+), 111 deletions(-) diff --git a/public/site/blueprints/fields/notifications.yml b/public/site/blueprints/fields/notifications.yml index 5d38804..5455610 100644 --- a/public/site/blueprints/fields/notifications.yml +++ b/public/site/blueprints/fields/notifications.yml @@ -40,6 +40,5 @@ fields: disabled: true date: type: hidden - isread: - type: toggle - disabled: true + readby: + type: users diff --git a/public/site/blueprints/users/admin.yml b/public/site/blueprints/users/admin.yml index 3fdce7c..6f51344 100644 --- a/public/site/blueprints/users/admin.yml +++ b/public/site/blueprints/users/admin.yml @@ -1,6 +1,3 @@ title: Admin description: Possède tous les droits et les accès, peut accéder à l’ensemble des Clients et des Projets et assigner des Projets à des Utilisateurs. home: /panel/pages/projects - -fields: - notifications: fields/notifications diff --git a/public/site/blueprints/users/client.yml b/public/site/blueprints/users/client.yml index 166f7fd..cf2109b 100644 --- a/public/site/blueprints/users/client.yml +++ b/public/site/blueprints/users/client.yml @@ -5,12 +5,7 @@ permissions: panel: false fields: - client: - type: pages - max: 1 - query: page('clients').children projects: label: Projets type: pages query: page('projects').children - notifications: fields/notifications diff --git a/public/site/blueprints/users/pochet.yml b/public/site/blueprints/users/pochet.yml index 3464dea..bd66784 100644 --- a/public/site/blueprints/users/pochet.yml +++ b/public/site/blueprints/users/pochet.yml @@ -19,4 +19,3 @@ fields: type: pages query: page('projects').children width: 3/4 - notifications: fields/notifications diff --git a/public/site/controllers/site.php b/public/site/controllers/site.php index 5877617..73ff66a 100644 --- a/public/site/controllers/site.php +++ b/public/site/controllers/site.php @@ -10,11 +10,10 @@ return function ($page, $kirby, $site) { $userData = [ "role" => (string) $kirby->user()->role(), - "uuid" => (string) $kirby->user()->uuid(), - "notifications" => Yaml::decode($kirby->user()->notifications()->value()), + "uuid" => (string) $kirby->user()->uuid() ]; - if ($kirby->user()->role() == 'client' && $kirby->user()->client()->isNotEmpty()) { + if ($kirby->user()->client()->exists() && $kirby->user()->client()->isNotEmpty()) { $userData['client'] = [ "name" => (string) $kirby->user()->client()->toPage()->title(), "uuid" => (string) $kirby->user()->client()->toPage()->uuid() diff --git a/public/site/plugins/comments/routes/create.php b/public/site/plugins/comments/routes/create.php index 536a6d4..96cf559 100644 --- a/public/site/plugins/comments/routes/create.php +++ b/public/site/plugins/comments/routes/create.php @@ -52,7 +52,7 @@ return [ try { $project->createNotification($commentData); } catch (\Throwable $th) { - throw new Exception($th->getMessage() . ". Line " . $th->getLine() . " in file " . $th->getFile(), 1); + throw new Exception($th->getMessage() . ". line " . $th->getLine() . " in file " . $th->getFile(), 1); } exit; diff --git a/public/site/plugins/notifications/routes/read.php b/public/site/plugins/notifications/routes/read.php index 6d4e44e..213ca8f 100644 --- a/public/site/plugins/notifications/routes/read.php +++ b/public/site/plugins/notifications/routes/read.php @@ -6,8 +6,18 @@ return [ 'action' => function () { $json = file_get_contents('php://input'); $data = json_decode($json); - - $newNotifications = kirby()->user()->readNotification($data->notificationId); - return $newNotifications; + + try { + $project = page($data->projectUuid); + $project->readNotification($data->notificationId); + return json_encode([ + "status" => "success" + ]); + } catch (\Throwable $th) { + return json_encode([ + "status" => "error", + "message" => $th->getMessage() . ' line ' . $th->getLine() . " in file " . $th->getFile() + ]); + } } ]; \ No newline at end of file diff --git a/public/site/plugins/notifications/src/Notification.php b/public/site/plugins/notifications/src/Notification.php index 9d0dfbc..41c7a58 100644 --- a/public/site/plugins/notifications/src/Notification.php +++ b/public/site/plugins/notifications/src/Notification.php @@ -12,7 +12,7 @@ class Notification protected Author $author; protected string $date; protected string $id; - protected string $isread = "false"; + protected array $readby = []; protected ?Position $position = null; @@ -33,7 +33,7 @@ class Notification "author" => $this->author->toArray(), "date" => $this->date, "id" => $this->id, - "isread" => $this->isread, + "readby" => $this->readby, ]; return $array; diff --git a/public/site/plugins/notifications/src/NotificationsPage.php b/public/site/plugins/notifications/src/NotificationsPage.php index 3494d5a..c8aed33 100644 --- a/public/site/plugins/notifications/src/NotificationsPage.php +++ b/public/site/plugins/notifications/src/NotificationsPage.php @@ -31,7 +31,36 @@ class NotificationsPage extends Page { } $this->update([ - 'notifications' => Yaml::encode(array_values($notifications)) + 'notifications' => $notifications ]); } + + public function readNotification($notificationId) { + $notifications = $this->notifications()->isNotEmpty() + ? Yaml::decode($this->notifications()->value()) + : []; + + foreach ($notifications as $key => &$notification) { + if ($notification['id'] !== $notificationId) { + continue; + } + + if (!isset($notification["readby"])) { + $notification["readby"] = []; + } + + $userUuid = (string) kirby()->user()->uuid(); + if (in_array($userUuid, $notification["readby"])) { + return; + } + + $notification["readby"][] = $userUuid; + break; + } + + $this->update([ + 'notifications' => Yaml::encode($notifications) + ]); + } + } \ No newline at end of file diff --git a/src/components/Menu.vue b/src/components/Menu.vue index c41b9b4..7ead19c 100644 --- a/src/components/Menu.vue +++ b/src/components/Menu.vue @@ -115,15 +115,15 @@ import { useProjectStore } from "../stores/project"; const route = useRoute(); const isExpanded = ref(true); -const { user } = storeToRefs(useUserStore()); +const { user, notifications } = storeToRefs(useUserStore()); const { currentProjects, archivedProjects } = storeToRefs(useProjectsStore()); const { isEmptyBrief } = useProjectStore(); const { page } = storeToRefs(usePageStore()); const unreadNotificationsCount = computed(() => { if (!user.value) return undefined; - const count = user.value.notifications.filter( - (notification) => notification.isread != "true" + const count = notifications.value.filter( + (notification) => !notification.isRead ).length; if (count === 0) return undefined; return count; @@ -168,7 +168,7 @@ function isCurrent(navItem) { function hasUnreadNotification(project) { if (!user.value) return false; - return user.value.notifications.some((notification) => { + return notifications.value.some((notification) => { return ( notification.isread != "true" && project.uri.includes(notification.location.project.uri) diff --git a/src/components/comments/Comment.vue b/src/components/comments/Comment.vue index a50b0bf..b9ba749 100644 --- a/src/components/comments/Comment.vue +++ b/src/components/comments/Comment.vue @@ -109,7 +109,7 @@ const getStatus = computed(() => { const correspondingNotification = userStore.notifications.find( (notification) => notification.id === comment.id ); - if (correspondingNotification && correspondingNotification.isread != "true") { + if (correspondingNotification && !correspondingNotification.isRead) { return "unread"; } return undefined; @@ -143,7 +143,7 @@ function handleClick() { async function read() { if (getStatus.value !== "unread") return; try { - const newNotification = await api.readNotification(comment.id); + const newNotification = await api.readNotification(comment); console.log(newNotification); userStore.readNotification(comment.id); } catch (error) { diff --git a/src/components/notifications/Comment.vue b/src/components/notifications/Comment.vue index db27dae..1b874f5 100644 --- a/src/components/notifications/Comment.vue +++ b/src/components/notifications/Comment.vue @@ -1,7 +1,6 @@