delete comment / reply working

This commit is contained in:
isUnknown 2024-10-30 16:32:13 +01:00
parent 58c08690f8
commit ac48b4166a
5 changed files with 131 additions and 59 deletions

View file

@ -19,53 +19,15 @@ Comments:
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
replies:
-
page:
uri: projects/miss-dior-blooming-bouquet
title: Miss Dior Blooming Bouquet
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
replies: [ ]
text: Test de réponse
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T12:26:44+01:00
id: m2vskcko
type: comment
isRead: false
parentId: m2vsk6jn
-
page:
uri: projects/miss-dior-blooming-bouquet
title: Miss Dior Blooming Bouquet
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
replies: [ ]
text: deuxième réponse
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T12:27:42+01:00
id: m2vslkxg
type: comment
isRead: false
parentId: m2vsk6jn
text: Un premier commentaire
replies: [ ]
text: Un certain commentaire
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T12:26:37+01:00
id: m2vsk6jn
date: 2024-10-30T16:26:22+01:00
id: m2w14iph
type: comment
isRead: false
position:

View file

@ -7,14 +7,50 @@ return [
$json = file_get_contents('php://input');
$data = json_decode($json);
$page = page($data->pageUri);
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$page = page($data->page->uri);
$file = $page->file($data->file->uuid);
$isReply = $data->parentId ?? false;
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
unset($comments[$data->id] );
foreach ($comments as $key => &$comment) {
if ($isReply) {
if ($comment['id'] === $data->parentId) {
foreach ($comment['replies'] as $replyKey => $reply) {
if ($reply['id'] === $data->id) {
unset($comment['replies'][$replyKey]);
$comment['replies'] = array_values($comment['replies']);
}
}
}
} else {
if ($comment['id'] === $data->id) {
unset($comments[$key]);
}
}
}
$comments = array_values($comments); // Réindexe les commentaires
foreach (kirby()->users() as $user) {
try {
$notifications = $user->notifications()->isNotEmpty()
? Yaml::decode($user->notifications()->value())
: [];
foreach ($notifications as $key => $notification) {
if ($notification['id'] === $data->id) {
unset($notifications[$key]);
}
}
$user->update([
'notifications' => $notifications
]);
} catch (\Throwable $th) {
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
}
$newFile = $file->update([
'comments' => $comments
@ -22,4 +58,4 @@ return [
return getFileData($newFile);
}
];
];