comments / notifications : improve abstraction

This commit is contained in:
isUnknown 2024-11-18 12:00:19 +01:00
parent f467012241
commit 32c026acfe
11 changed files with 92 additions and 65 deletions

View file

@ -1,22 +1,23 @@
<?php
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]);
return function($projectManagers, $notificationId) {
foreach ($projectManagers as $projectManager) {
try {
$notifications = $projectManager->notifications()->isNotEmpty()
? Yaml::decode($projectManager->notifications()->value())
: [];
foreach ($notifications as $key => $notification) {
if ($notification['id'] === $notificationId) {
unset($notifications[$key]);
}
}
$projectManager->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);
}
};