read comment notification working

This commit is contained in:
isUnknown 2025-01-10 17:40:45 +01:00
parent 9222069ef5
commit 246d21f85a
17 changed files with 86 additions and 111 deletions

View file

@ -40,6 +40,5 @@ fields:
disabled: true
date:
type: hidden
isread:
type: toggle
disabled: true
readby:
type: users

View file

@ -1,6 +1,3 @@
title: Admin
description: Possède tous les droits et les accès, peut accéder à lensemble des Clients et des Projets et assigner des Projets à des Utilisateurs.
home: /panel/pages/projects
fields:
notifications: fields/notifications

View file

@ -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

View file

@ -19,4 +19,3 @@ fields:
type: pages
query: page('projects').children
width: 3/4
notifications: fields/notifications

View file

@ -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()

View file

@ -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;

View file

@ -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()
]);
}
}
];

View file

@ -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;

View file

@ -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)
]);
}
}