diff --git a/api/get_user_messages.php b/api/get_user_messages.php index f6da0b1..65bf771 100644 --- a/api/get_user_messages.php +++ b/api/get_user_messages.php @@ -100,9 +100,10 @@ try { $conversations = array_reverse($conversations); // Indicar si hay más mensajes anteriores (si la consulta original devolvió exactamente $limit, podría haber más) - $hasMore = count($conversations) >= 1 && count($conversations) == $limit ? true : false; - - // Si no hay mensajes en conversations, intentar con conversations + $initialCount = is_array($conversations) ? count($conversations) : 0; + $hasMore = $initialCount >= 1 && $initialCount == $limit ? true : false; + + // Si no hay mensajes en conversations, intentar con conversaciones (compatibilidad antigua) if (empty($conversations)) { $conversations = $db->fetchAll( "SELECT @@ -163,7 +164,22 @@ 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], JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR); + // Añadir debug information cuando se solicita (solo con ?debug=1) + $debugInfo = null; + if (!empty($_GET['debug']) && $_GET['debug'] == '1') { + $debugInfo = [ + 'original_sql' => substr($sql, 0, 2000), + 'params' => $params, + 'initial_count' => $initialCount, + 'fallback_used' => isset($conversationsFallback) && !empty($conversationsFallback), + 'fallback_count' => isset($conversationsFallback) ? count($conversationsFallback) : 0 + ]; + } + + $out = ['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest]; + if ($debugInfo) $out['debug'] = $debugInfo; + + echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR); } catch (Exception $e) { // Log detallado con stack trace para diagnóstico