up
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user