Ajout de collectLight() pour optimiser le chargement du listing des projets

Problème : projects.json.php causait un dépassement mémoire en collectant
toutes les notifications complètes (avec author, location, text, etc.) pour
tous les projets.

Solution : Nouvelle méthode collectLight() qui ne retourne que les données
minimales nécessaires au frontend pour afficher les indicateurs :
- id, type, isRead, date
- location.project.uri (pour le filtrage)

Les détails complets sont toujours chargés dans project.json.php individuel.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
isUnknown 2026-01-15 11:18:59 +01:00
parent 0250dc1487
commit 0a980603a4
2 changed files with 63 additions and 7 deletions

View file

@ -57,6 +57,49 @@ class NotificationCollector
return $all;
}
/**
* Collecte uniquement les données minimales des notifications (version allégée pour listing).
* Retourne seulement id, type, isRead, date pour économiser la mémoire.
*
* @param Page $project Le projet à scanner
* @param User $user L'utilisateur courant
* @return array Liste triée par date décroissante
*/
public function collectLight(Page $project, User $user): array
{
$all = [];
foreach ($this->providers as $provider) {
try {
$notifications = $provider->collect($project, $user);
// Ne garder que les champs essentiels
foreach ($notifications as $notification) {
$all[] = [
'id' => $notification['id'] ?? null,
'type' => $notification['type'] ?? null,
'isRead' => $notification['isRead'] ?? false,
'date' => $notification['date'] ?? null,
// Garder location.project.uri pour le frontend
'location' => [
'project' => $notification['location']['project'] ?? []
]
];
}
} catch (\Throwable $e) {
error_log("NotificationCollector: Error in {$provider->getType()}: " . $e->getMessage());
}
}
// Trier par date décroissante
usort($all, function ($a, $b) {
$dateA = strtotime($a['date'] ?? '0');
$dateB = strtotime($b['date'] ?? '0');
return $dateB - $dateA;
});
return $all;
}
/**
* Marque une notification comme lue en déléguant au bon provider.
*

View file

@ -7,10 +7,19 @@ if (!$kirby->user()) {
]);
}
function getProjectData($project)
function getProjectData($project, $user, $collector)
{
// Les notifications ne sont plus collectées ici pour éviter les problèmes de mémoire.
// Elles seront collectées uniquement quand on affiche un projet individuel.
// Utiliser collectLight() pour économiser la mémoire (seulement id, type, isRead, date)
$notifications = [];
if ($collector) {
try {
$notifications = $collector->collectLight($project, $user);
} catch (\Throwable $e) {
error_log("Error collecting light notifications for project {$project->uri()}: " . $e->getMessage());
$notifications = [];
}
}
$data = [
'title' => $project->title()->value(),
'url' => $project->url(),
@ -20,7 +29,7 @@ function getProjectData($project)
'status' => $project->status(),
'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '',
'steps' => $project->getSteps(),
'notifications' => [], // Sera collecté à la demande dans project.json.php
'notifications' => $notifications,
'uuid' => (string) $project->uuid(),
'slug' => (string) $project->slug(),
'isDTLEnabled' => $project->isDTLEnabled()->isTrue(),
@ -34,10 +43,14 @@ function getProjectData($project)
return $data;
}
// Récupérer le collector de notifications
$notificationCollector = $kirby->option('adrienpayet.pdc-notifications.collector');
$currentUser = $kirby->user();
try {
$children = $kirby->user()->role() == 'admin'
? $page->childrenAndDrafts()->map(fn($project) => getProjectData($project))->values()
: $kirby->user()->projects()->toPages()->map(fn($project) => getProjectData($project))->values();
$children = $currentUser->role() == 'admin'
? $page->childrenAndDrafts()->map(fn($project) => getProjectData($project, $currentUser, $notificationCollector))->values()
: $currentUser->projects()->toPages()->map(fn($project) => getProjectData($project, $currentUser, $notificationCollector))->values();
} catch (\Throwable $th) {
throw new Exception($th->getMessage() . ' line ' . $th->getLine() . ' in file ' . $th->getFile(), 1);
$children = [];