From 4f5301a15f93bc6be4da56c02b43e37b6b4bce37 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 24 Jan 2026 08:48:58 -0500 Subject: [PATCH] up --- api/get_user_messages.php | 21 +++++++++++++++++---- conversations.php | 6 ++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/api/get_user_messages.php b/api/get_user_messages.php index 66825da..b3412f5 100644 --- a/api/get_user_messages.php +++ b/api/get_user_messages.php @@ -61,13 +61,22 @@ try { LEFT JOIN users u ON c.user_id = u.id WHERE c.user_id = :user_id"; + // Soporte para paginación estable: before (timestamp) y before_id (id del mensaje más antiguo del bloque) + $beforeId = isset($_GET['before_id']) ? intval($_GET['before_id']) : null; + $params = ['user_id' => $userId]; if ($before) { - $sql .= " AND c.created_at < :before"; + $sql .= " AND (c.created_at < :before"; $params['before'] = $before; + if ($beforeId) { + // Incluir mensajes con mismo timestamp pero id menor (paginación estable) + $sql .= " OR (c.created_at = :before AND c.id < :before_id)"; + $params['before_id'] = $beforeId; + } + $sql .= ")"; } - $sql .= " ORDER BY c.created_at DESC LIMIT " . $limit; + $sql .= " ORDER BY c.created_at DESC, c.id 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])); @@ -163,6 +172,7 @@ try { // earliest message timestamp (para paginación hacia atrás) $earliest = $conversations[0]['created_at'] ?? null; + $earliestId = $conversations[0]['id'] ?? null; // Si usamos fallback y el earliest coincide o es >= al 'before' solicitado, // decrementamos 1 segundo para evitar que la próxima petición repita el mismo bloque @@ -177,19 +187,22 @@ try { } } + // Recalculate final_count for debug + $finalCount = is_array($conversations) ? count($conversations) : 0; + // 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, - 'final_count' => $finalCount ?? (is_array($conversations) ? count($conversations) : 0), + 'final_count' => $finalCount, 'fallback_used' => $fallbackUsed, 'fallback_count' => $fallbackUsed ? count($conversationsFallback) : 0 ]; } - $out = ['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest]; + $out = ['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest, 'earliest_id' => $earliestId]; if ($debugInfo) $out['debug'] = $debugInfo; echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR); diff --git a/conversations.php b/conversations.php index c53cb50..d436c06 100644 --- a/conversations.php +++ b/conversations.php @@ -2093,6 +2093,7 @@ let url = `get_user_messages.php?user_id=${userId}&limit=${this.messageLimit}`; if (!initial && this.earliestMessage) { url += `&before=${encodeURIComponent(this.earliestMessage)}`; + if (this.earliestMessageId) url += `&before_id=${encodeURIComponent(this.earliestMessageId)}`; } const data = await this.apiCall(url); @@ -2105,6 +2106,7 @@ messages = data.data; hasMore = !!data.has_more; earliest = data.earliest || (messages[0] && messages[0].created_at) || null; + earliestId = data.earliest_id || (messages[0] && messages[0].id) || null; } else if (Array.isArray(data)) { messages = data; } @@ -2116,6 +2118,10 @@ this.conversations = messages.concat(this.conversations); } + // Actualizar paginación (earliest timestamp y id) + if (earliest) this.earliestMessage = earliest; + if (typeof earliestId !== 'undefined' && earliestId !== null) this.earliestMessageId = earliestId; + // Improved dedupe: pick the most "readable" message when duplicates exist try { const best = new Map(); // key => { index, score }