Update get_user_messages.php

This commit is contained in:
Lizandro Guarnizo
2026-01-24 08:24:35 -05:00
parent 1a2cb998d6
commit 2e14f21377
+16 -2
View File
@@ -28,6 +28,16 @@ try {
$limit = max(1, min(200, $limit));
$before = !empty($_GET['before']) ? $_GET['before'] : null; // expect timestamp string
// Validar formato de 'before' si se proporcionó (evitar SQL/parse errors por input malformado)
if (!is_null($before)) {
// Aceptar formato 'YYYY-MM-DD HH:MM:SS' (espacio entre fecha y hora)
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $before)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'before timestamp inválido']);
exit;
}
}
$db = Database::getInstance();
// Construir consulta: obtener mensajes ordenados DESC (más recientes primero) y limitar
@@ -59,11 +69,15 @@ try {
$sql .= " ORDER BY created_at DESC LIMIT " . $limit;
// Log params/consulta para diagnóstico si algo falla (no sensible)
error_log('get_user_messages.php - params: ' . json_encode(['user_id' => $userId, 'limit' => $limit, 'before' => $before]));
error_log('get_user_messages.php - executing SQL: ' . $sql . ' params: ' . json_encode($params));
$conversations = $db->fetchAll($sql, $params);
// Si no hay mensajes, devolver array vacío
if (empty($conversations)) {
echo json_encode(['success' => true, 'data' => [], 'has_more' => false]);
echo json_encode(['success' => true, 'data' => [], 'has_more' => false], JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR);
exit;
}
@@ -134,7 +148,7 @@ try {
// earliest message timestamp (para paginación hacia atrás)
$earliest = $conversations[0]['created_at'] ?? null;
echo json_encode(['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest]);
echo json_encode(['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest], JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR);
} catch (Exception $e) {
// Log detallado con stack trace para diagnóstico