diff --git a/public/site/plugins/notifications/src/NotificationCollector.php b/public/site/plugins/notifications/src/NotificationCollector.php index 113b041..83e47d1 100644 --- a/public/site/plugins/notifications/src/NotificationCollector.php +++ b/public/site/plugins/notifications/src/NotificationCollector.php @@ -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. * diff --git a/public/site/templates/projects.json.php b/public/site/templates/projects.json.php index ff1d6a4..222e86a 100644 --- a/public/site/templates/projects.json.php +++ b/public/site/templates/projects.json.php @@ -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 = [];