Update conversations.php
This commit is contained in:
+29
-2
@@ -2143,6 +2143,8 @@
|
|||||||
// no reemplazar ni limpiar la conversación actual; solo indicar que no hay más.
|
// no reemplazar ni limpiar la conversación actual; solo indicar que no hay más.
|
||||||
if (!initial && Array.isArray(messages) && messages.length === 0) {
|
if (!initial && Array.isArray(messages) && messages.length === 0) {
|
||||||
try { if (topLoader && topLoader.parentNode) topLoader.remove(); } catch(e) {}
|
try { if (topLoader && topLoader.parentNode) topLoader.remove(); } catch(e) {}
|
||||||
|
// Asegurar que no quedemos en estado de loading
|
||||||
|
this.loadingMessages = false;
|
||||||
this.hasMoreMessages = false;
|
this.hasMoreMessages = false;
|
||||||
// show hint and preserve current view
|
// show hint and preserve current view
|
||||||
this.showNoMoreMessagesHint();
|
this.showNoMoreMessagesHint();
|
||||||
@@ -2150,12 +2152,24 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si es carga inicial y el servidor devolvió vacío, pero ya teníamos mensajes, preservarlos.
|
||||||
|
if (initial && Array.isArray(messages) && messages.length === 0 && Array.isArray(this.conversations) && this.conversations.length > 0) {
|
||||||
|
try { if (topLoader && topLoader.parentNode) topLoader.remove(); } catch(e) {}
|
||||||
|
this.loadingMessages = false;
|
||||||
|
if (typeof showAlert === 'function') showAlert('No se pudieron recargar mensajes; manteniendo historial actual', 'warning');
|
||||||
|
console.warn('Initial load returned empty but existing view contains messages - preserving current conversation');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (initial) {
|
if (initial) {
|
||||||
|
// Reemplazar sólo en carga inicial válida (puede estar vacía si no hay nada en DB)
|
||||||
this.conversations = messages;
|
this.conversations = messages;
|
||||||
} else {
|
} else {
|
||||||
|
// Prepend sólo si hay mensajes nuevos
|
||||||
|
if (Array.isArray(messages) && messages.length > 0) {
|
||||||
// Evitar introducir duplicados que ya existen en la vista actual
|
// Evitar introducir duplicados que ya existen en la vista actual
|
||||||
try {
|
try {
|
||||||
const existingKeys = new Set(this.conversations.map(m => this.messageKey(m)));
|
const existingKeys = new Set((this.conversations || []).map(m => this.messageKey(m)));
|
||||||
const beforeLen = messages.length;
|
const beforeLen = messages.length;
|
||||||
messages = messages.filter(m => {
|
messages = messages.filter(m => {
|
||||||
const k = this.messageKey(m);
|
const k = this.messageKey(m);
|
||||||
@@ -2173,7 +2187,10 @@
|
|||||||
} catch (e) { console.warn('duplicate filtering failed', e); }
|
} catch (e) { console.warn('duplicate filtering failed', e); }
|
||||||
|
|
||||||
// Prepend mensajes antiguos
|
// Prepend mensajes antiguos
|
||||||
this.conversations = messages.concat(this.conversations);
|
this.conversations = messages.concat(this.conversations || []);
|
||||||
|
} else {
|
||||||
|
// No hay mensajes nuevos para añadir; nada que hacer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualizar paginación (earliest timestamp y id)
|
// Actualizar paginación (earliest timestamp y id)
|
||||||
@@ -2182,6 +2199,9 @@
|
|||||||
|
|
||||||
// Improved dedupe: pick the most "readable" message when duplicates exist
|
// Improved dedupe: pick the most "readable" message when duplicates exist
|
||||||
try {
|
try {
|
||||||
|
if (!this.conversations || this.conversations.length === 0) {
|
||||||
|
// Nothing to dedupe
|
||||||
|
} else {
|
||||||
const best = new Map(); // key => { index, score }
|
const best = new Map(); // key => { index, score }
|
||||||
const toRemove = [];
|
const toRemove = [];
|
||||||
|
|
||||||
@@ -2246,6 +2266,7 @@
|
|||||||
console.debug('Dedupe removed message at index', idx, 'removed=', removed && (removed.message_id || removed.id || removed.content || removed.media_url) );
|
console.debug('Dedupe removed message at index', idx, 'removed=', removed && (removed.message_id || removed.id || removed.content || removed.media_url) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Dedupe failed', e);
|
console.warn('Dedupe failed', e);
|
||||||
}
|
}
|
||||||
@@ -2263,7 +2284,13 @@
|
|||||||
// Renderizar incrementalmente para evitar parpadeos y preservar reproducción de media
|
// Renderizar incrementalmente para evitar parpadeos y preservar reproducción de media
|
||||||
// Si cargamos paginación (initial=false), pasar la cantidad de mensajes recién obtenidos para insertarlos al inicio
|
// Si cargamos paginación (initial=false), pasar la cantidad de mensajes recién obtenidos para insertarlos al inicio
|
||||||
const newlyFetched = (!initial && Array.isArray(messages)) ? messages.length : 0;
|
const newlyFetched = (!initial && Array.isArray(messages)) ? messages.length : 0;
|
||||||
|
if (this.conversations && this.conversations.length > 0) {
|
||||||
this.renderMessagesIncremental(initial, newlyFetched);
|
this.renderMessagesIncremental(initial, newlyFetched);
|
||||||
|
} else if (initial) {
|
||||||
|
// Si es carga inicial y no tenemos mensajes, mostrar una vista vacía
|
||||||
|
const container = document.getElementById('chat-conversations');
|
||||||
|
if (container) container.innerHTML = `<div class="no-conversation"><i class="fab fa-whatsapp"></i><h4>No hay mensajes</h4><p>Envía un mensaje para iniciar la conversación.</p></div>`;
|
||||||
|
}
|
||||||
|
|
||||||
// Replace media rendering uses window.renderMediaMessage inside element creation/update
|
// Replace media rendering uses window.renderMediaMessage inside element creation/update
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user