Fix: fetchMessageHistory con evento SSE y logs de debug
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m9s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m9s
- Agregar logs detallados en /api/debug/history/fetch para diagnosticar - Emitir evento SSE 'history.synced' cuando llegan mensajes del historial - Frontend ahora escucha el evento SSE en vez de timeout fijo de 3 segundos - Agregar script y documentación para referencia de Baileys message history
This commit is contained in:
@@ -8,6 +8,7 @@ export interface RealtimeEvents {
|
||||
'message.status': (data: { instanceId: string; messageId: string; status: string }) => void
|
||||
'instance.status': (data: { instanceId: string; status: string; phoneNumber?: string }) => void
|
||||
'instance.qr': (data: { instanceId: string; qr: string; qrDataUrl: string }) => void
|
||||
'history.synced': (data: { instanceId: string; chatsCount: number; contactsCount: number; messagesCount: number; syncType: any }) => void
|
||||
}
|
||||
|
||||
export const useRealtime = () => {
|
||||
@@ -49,11 +50,12 @@ export const useRealtime = () => {
|
||||
// Listen for specific event types
|
||||
const eventTypes = [
|
||||
'message.received',
|
||||
'message.sent',
|
||||
'message.sent',
|
||||
'message.status',
|
||||
'instance.status',
|
||||
'instance.qr',
|
||||
'instance.pairing'
|
||||
'instance.pairing',
|
||||
'history.synced'
|
||||
]
|
||||
|
||||
eventTypes.forEach(eventType => {
|
||||
|
||||
@@ -395,10 +395,10 @@ const fetchWhatsAppHistory = async () => {
|
||||
if (!selectedInstance.value?.value || !selectedChat.value) return
|
||||
|
||||
loadingWhatsAppHistory.value = true
|
||||
try {
|
||||
const instanceId = selectedInstance.value.value
|
||||
const chatId = selectedChat.value.id
|
||||
const instanceId = selectedInstance.value.value
|
||||
const chatId = selectedChat.value.id
|
||||
|
||||
try {
|
||||
// Get the oldest message from local DB
|
||||
const oldest = await $fetch<{
|
||||
hasMessages: boolean
|
||||
@@ -406,6 +406,23 @@ const fetchWhatsAppHistory = async () => {
|
||||
timestamp: number | null
|
||||
}>(`/api/messages/${instanceId}/${chatId}/oldest`)
|
||||
|
||||
// Create a promise that resolves when history.synced event arrives
|
||||
const historyPromise = new Promise<{ messagesCount: number; timeout?: boolean }>((resolve) => {
|
||||
// Set up listener for history.synced event
|
||||
const cleanup = on('history.synced', (data) => {
|
||||
if (data.instanceId === instanceId) {
|
||||
cleanup() // Remove listener
|
||||
resolve({ messagesCount: data.messagesCount })
|
||||
}
|
||||
})
|
||||
|
||||
// Timeout de seguridad (15 segundos)
|
||||
setTimeout(() => {
|
||||
cleanup() // Remove listener
|
||||
resolve({ messagesCount: 0, timeout: true })
|
||||
}, 15000)
|
||||
})
|
||||
|
||||
// Request history from WhatsApp
|
||||
if (!oldest.hasMessages) {
|
||||
// No messages in DB, request without reference
|
||||
@@ -429,10 +446,18 @@ const fetchWhatsAppHistory = async () => {
|
||||
})
|
||||
}
|
||||
|
||||
// Wait for history sync to complete and reload
|
||||
await new Promise(r => setTimeout(r, 3000))
|
||||
await reloadMessages()
|
||||
hasMoreMessages.value = true
|
||||
// Wait for history sync event or timeout
|
||||
const result = await historyPromise
|
||||
|
||||
if (result.timeout) {
|
||||
console.log('[History] Timeout waiting for history sync - WhatsApp may not have more messages')
|
||||
} else if (result.messagesCount > 0) {
|
||||
console.log(`[History] Received ${result.messagesCount} messages from history sync`)
|
||||
await reloadMessages()
|
||||
hasMoreMessages.value = true
|
||||
} else {
|
||||
console.log('[History] No messages received from history sync')
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error fetching WhatsApp history:', e)
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user