designtopack/public/site/plugins/comments/routes/reply.php

50 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2024-10-30 09:56:16 +01:00
use adrienpayet\comments\Reply;
return [
2024-10-29 16:51:31 +01:00
'pattern' => '(:all)reply-comment.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
2024-12-20 12:36:21 +01:00
$page = page($data->fileParentUri);
2025-01-15 14:18:48 +01:00
$file = $page->file($data->fileName);
2024-12-18 18:39:45 +01:00
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
2024-10-30 13:29:26 +01:00
foreach ($comments as &$comment) {
2024-12-19 10:32:48 +01:00
$isParentComment = $comment['id'] === $data->parentId;
if ($isParentComment) {
$replyData = [
"location" => [
"page" => $page,
"file" => $file,
"parent" => $comment
],
"position" => [
2024-12-20 12:36:21 +01:00
"pageIndex" => $data->position->pageIndex ?? null,
2024-12-19 10:32:48 +01:00
],
"date" => (string) $data->date,
"text" => $data->text,
"author" => kirby()->user(),
"id" => Str::uuid(),
"type" => "comment-reply",
"readby" => [], // Pour le système de notifications dérivées
2024-12-19 10:32:48 +01:00
];
$newReply = new Reply($replyData);
2024-10-30 13:29:26 +01:00
$comment['replies'][] = $newReply->toArray();
}
}
$newFile = $file->update([
'comments' => $comments
]);
// Note: Les notifications sont maintenant dérivées des commentaires.
// Plus besoin d'appeler createNotification().
return getFileData($newFile);
}
];