This commit is contained in:
Lizandro Guarnizo
2026-04-25 15:04:50 -05:00
parent 08be3f254d
commit 0e5ff957eb
4 changed files with 36 additions and 13 deletions
+11 -4
View File
@@ -37,6 +37,7 @@ class CorreoImapService
* luego se filtra por fecha en PHP.
*
* Cada elemento: [seq, from, subject, date, body]
* El campo 'fuera_de_ventana' indica si el resultado vino del fallback (sin filtro de tiempo).
*/
public function fetchLatest(int $limit = 5, int $minutesBack = 5): array
{
@@ -50,7 +51,6 @@ class CorreoImapService
}
// 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}";
@@ -65,22 +65,29 @@ class CorreoImapService
$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
// Correo 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;
}
}
// ── Fallback: si la ventana no devolvió nada, mostrar los últimos $limit sin filtro
if (empty($result)) {
$result = array_slice($emails, 0, $limit);
foreach ($result as &$e) {
$e['fuera_de_ventana'] = true;
}
unset($e);
}
return $result;
}