2024-10-23 11:32:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-10-30 09:49:16 +01:00
|
|
|
use adrienpayet\comments\Comment;
|
|
|
|
|
|
2024-10-23 11:32:51 +02:00
|
|
|
return [
|
2024-10-29 11:12:57 +01:00
|
|
|
'pattern' => '(:all)create-comment.json',
|
2025-02-09 10:57:25 +01:00
|
|
|
'method' => 'POST',
|
2024-10-23 11:32:51 +02:00
|
|
|
'action' => function () {
|
|
|
|
|
$json = file_get_contents('php://input');
|
|
|
|
|
$data = json_decode($json);
|
|
|
|
|
|
2025-02-09 10:57:25 +01:00
|
|
|
// kirby()->impersonate('kirby');
|
|
|
|
|
|
2024-12-19 17:11:46 +01:00
|
|
|
$page = page($data->fileParentUri);
|
2025-02-09 10:57:25 +01:00
|
|
|
$project = $page->parent()->template() == 'project' ? $page->parent() : $page->parent()->parent();
|
2024-10-23 11:32:51 +02:00
|
|
|
$file = $page->file($data->fileName);
|
2025-02-09 10:57:25 +01:00
|
|
|
$user = kirby()->user($data->userUuid);
|
|
|
|
|
|
2024-10-23 11:32:51 +02:00
|
|
|
|
2024-10-28 16:52:59 +01:00
|
|
|
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
2024-10-23 11:32:51 +02:00
|
|
|
|
2025-02-11 18:45:23 +01:00
|
|
|
foreach ($comments as $existingComment) {
|
2025-02-19 09:38:01 +01:00
|
|
|
if
|
|
|
|
|
(
|
|
|
|
|
$existingComment['author']['email'] == $user->email() &&
|
|
|
|
|
$existingComment['text'] == $data->text &&
|
|
|
|
|
$existingComment['position']['x'] == $data->position->x &&
|
|
|
|
|
$existingComment['position']['y'] == $data->position->y
|
|
|
|
|
) {
|
2025-02-11 18:45:23 +01:00
|
|
|
return json_encode(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 16:26:55 +01:00
|
|
|
$commentData = [
|
2025-02-09 10:57:25 +01:00
|
|
|
'location' => [
|
|
|
|
|
'page' => $page,
|
|
|
|
|
'project' => $project,
|
|
|
|
|
'file' => $file,
|
2024-12-18 16:26:55 +01:00
|
|
|
],
|
2025-02-09 10:57:25 +01:00
|
|
|
'position' => [
|
|
|
|
|
'x' => $data->position->x,
|
|
|
|
|
'y' => $data->position->y,
|
2024-10-29 11:12:57 +01:00
|
|
|
],
|
2025-02-09 10:57:25 +01:00
|
|
|
'date' => (string) $data->date,
|
|
|
|
|
'text' => $data->text,
|
|
|
|
|
'author' => kirby()->user(),
|
|
|
|
|
'id' => Str::uuid(),
|
|
|
|
|
'type' => 'comment',
|
2026-01-15 10:31:31 +01:00
|
|
|
'readby' => [], // Pour le système de notifications dérivées
|
2024-10-23 11:32:51 +02:00
|
|
|
];
|
2024-10-23 15:49:09 +02:00
|
|
|
|
2024-12-19 17:11:46 +01:00
|
|
|
if (isset($data->position->pageIndex)) {
|
2025-02-09 10:57:25 +01:00
|
|
|
$commentData['position']['pageIndex'] = $data->position->pageIndex;
|
2024-12-19 16:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-18 16:26:55 +01:00
|
|
|
$newComment = new Comment($commentData);
|
2024-10-30 09:49:16 +01:00
|
|
|
|
2025-02-09 10:57:25 +01:00
|
|
|
$comments[] = $newComment->toArray();
|
|
|
|
|
|
2024-10-23 11:32:51 +02:00
|
|
|
$newFile = $file->update([
|
2025-02-09 10:57:25 +01:00
|
|
|
'comments' => $comments,
|
|
|
|
|
]);
|
2024-10-23 11:32:51 +02:00
|
|
|
|
2024-11-17 12:01:20 +01:00
|
|
|
echo json_encode(getFileData($newFile));
|
2025-02-09 10:57:25 +01:00
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
// Note: Les notifications sont maintenant dérivées des commentaires.
|
|
|
|
|
// Plus besoin d'appeler createNotification().
|
2024-11-17 12:01:20 +01:00
|
|
|
|
|
|
|
|
exit;
|
2025-02-09 10:57:25 +01:00
|
|
|
},
|
|
|
|
|
];
|