designtopack/public/site/plugins/comments/routes/create.php
isUnknown a7d315942a Refonte du système de notifications : passage aux notifications dérivées
Remplace le système de notifications stockées par un système de providers
qui dérivent les notifications des données existantes (commentaires, réponses,
demandes de projet, demandes de rendez-vous, validations de brief).

- Ajout du NotificationCollector et de l'interface NotificationProvider
- Création de 5 providers : Comment, Reply, ProjectRequest, AppointmentRequest, Content
- Métadonnées de notifications stockées directement sur les entités source
- Nouvelles routes mark-as-read et mark-all-read
- Mise à jour du frontend pour le nouveau système
- Route de migration pour les données existantes

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-15 10:31:31 +01:00

71 lines
2 KiB
PHP

<?php
use adrienpayet\comments\Comment;
return [
'pattern' => '(:all)create-comment.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
// kirby()->impersonate('kirby');
$page = page($data->fileParentUri);
$project = $page->parent()->template() == 'project' ? $page->parent() : $page->parent()->parent();
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
foreach ($comments as $existingComment) {
if
(
$existingComment['author']['email'] == $user->email() &&
$existingComment['text'] == $data->text &&
$existingComment['position']['x'] == $data->position->x &&
$existingComment['position']['y'] == $data->position->y
) {
return json_encode(null);
}
}
$commentData = [
'location' => [
'page' => $page,
'project' => $project,
'file' => $file,
],
'position' => [
'x' => $data->position->x,
'y' => $data->position->y,
],
'date' => (string) $data->date,
'text' => $data->text,
'author' => kirby()->user(),
'id' => Str::uuid(),
'type' => 'comment',
'readby' => [], // Pour le système de notifications dérivées
];
if (isset($data->position->pageIndex)) {
$commentData['position']['pageIndex'] = $data->position->pageIndex;
}
$newComment = new Comment($commentData);
$comments[] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments,
]);
echo json_encode(getFileData($newFile));
// Note: Les notifications sont maintenant dérivées des commentaires.
// Plus besoin d'appeler createNotification().
exit;
},
];