read comment working

This commit is contained in:
isUnknown 2024-11-18 09:36:15 +01:00
parent 583daa1759
commit 39fc379541
12 changed files with 125 additions and 78 deletions

View file

@ -1,11 +1,9 @@
<?php
Kirby::plugin('adrienpayet/pdc-notifications', [
// 'routes' => [
// 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')
]
]);

View file

@ -1,25 +1,22 @@
<?php
return function($project, $notificationId) {
$projectManagers = $project->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);
}
};

View file

@ -1,16 +1,26 @@
<?php
return function ($group, $notificationId) {
$notifications = Yaml::decode($this->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;
};

View file

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