From fa1f4bc5812b621870a4c75e97697f9e1709c653 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 25 Apr 2026 14:55:11 -0500 Subject: [PATCH] up --- app/Services/CorreoImapService.php | 45 +++++++++++++++---- .../whatsapp/show-correo-config.blade.php | 8 +++- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/Services/CorreoImapService.php b/app/Services/CorreoImapService.php index 138502f..afb4cbf 100644 --- a/app/Services/CorreoImapService.php +++ b/app/Services/CorreoImapService.php @@ -29,10 +29,16 @@ class CorreoImapService // ────────────────────────────────────────────── /** - * Conecta, autentica y devuelve los últimos $limit correos. - * Cada elemento: [uid, from, subject, date, body] + * Conecta, autentica y devuelve: + * - Los correos de los últimos $minutesBack minutos, con máximo $limit emails. + * - Lo que ocurra primero: llegar al límite de tiempo o al límite de cantidad. + * + * Para tener margen suficiente se descarga un lote mayor ($fetchSize) y + * luego se filtra por fecha en PHP. + * + * Cada elemento: [seq, from, subject, date, body] */ - public function fetchLatest(int $limit = 5): array + public function fetchLatest(int $limit = 5, int $minutesBack = 5): array { $this->connect(); $this->login(); @@ -43,16 +49,39 @@ class CorreoImapService return []; } - // Tomamos los últimos $limit números de secuencia - $from = max(1, $total - $limit + 1); + // Descargar un lote suficiente para cubrir la ventana de tiempo. + // 20 mensajes es razonable; si el buzón tiene pocos, se ajusta. + $fetchSize = min($total, max($limit * 4, 20)); + $from = max(1, $total - $fetchSize + 1); $set = "{$from}:{$total}"; $emails = $this->fetchMessages($set); - $this->logout(); - // Devolver en orden descendente (más reciente primero) - return array_reverse($emails); + // Más reciente primero + $emails = array_reverse($emails); + + $cutoff = time() - ($minutesBack * 60); + $result = []; + + foreach ($emails as $email) { + // Parsear la fecha del correo + $ts = $email['date'] ? @strtotime($email['date']) : false; + + // Si la fecha es válida y el correo es más antiguo que la ventana → parar + if ($ts !== false && $ts < $cutoff) { + break; + } + + $result[] = $email; + + // Límite de cantidad alcanzado → parar + if (count($result) >= $limit) { + break; + } + } + + return $result; } public function getLastError(): string diff --git a/resources/views/livewire/whatsapp/show-correo-config.blade.php b/resources/views/livewire/whatsapp/show-correo-config.blade.php index 6175488..738eb3a 100644 --- a/resources/views/livewire/whatsapp/show-correo-config.blade.php +++ b/resources/views/livewire/whatsapp/show-correo-config.blade.php @@ -142,7 +142,13 @@

Conexión exitosa

- Se encontraron {{ count($emails) }} correo(s) recientes. + @if(count($emails) === 0) + No hay correos en los últimos 5 minutos. + @elseif(count($emails) === 5) + Se muestran los últimos 5 correos (límite alcanzado). + @else + {{ count($emails) }} correo(s) en los últimos 5 minutos. + @endif