Feature: History sync, getMessage y handlers de chats
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 51s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 51s
This commit is contained in:
@@ -155,7 +155,22 @@ class BaileysManager extends EventEmitter {
|
||||
logger: pino({ level: 'debug' }),
|
||||
generateHighQualityLinkPreview: true,
|
||||
syncFullHistory: false,
|
||||
markOnlineOnConnect: false
|
||||
markOnlineOnConnect: false,
|
||||
// getMessage for resending/decrypting messages
|
||||
getMessage: async (key) => {
|
||||
try {
|
||||
const result = await query<{ raw_message: any }>(
|
||||
`SELECT raw_message FROM messages WHERE instance_id = $1 AND message_id = $2`,
|
||||
[instanceId, key.id]
|
||||
)
|
||||
if (result.rows.length > 0) {
|
||||
return result.rows[0].raw_message?.message
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[BaileysManager] getMessage error:', err)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
console.log(`[BaileysManager] WASocket created successfully`)
|
||||
|
||||
@@ -303,6 +318,87 @@ class BaileysManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Chat upsert - new chats
|
||||
socket.ev.on('chats.upsert', async (chats) => {
|
||||
console.log(`[BaileysManager] chats.upsert: ${chats.length} chats`)
|
||||
for (const chat of chats) {
|
||||
try {
|
||||
await query(
|
||||
`INSERT INTO chats (instance_id, jid, name, is_group, unread_count)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (instance_id, jid) DO UPDATE SET
|
||||
name = COALESCE(EXCLUDED.name, chats.name),
|
||||
updated_at = NOW()`,
|
||||
[instanceId, chat.id, chat.name || chat.id?.split('@')[0], chat.id?.includes('@g.us'), chat.unreadCount || 0]
|
||||
)
|
||||
} catch (err) {
|
||||
console.error(`[BaileysManager] Error upserting chat:`, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Chat update - changes to existing chats
|
||||
socket.ev.on('chats.update', async (updates) => {
|
||||
for (const update of updates) {
|
||||
try {
|
||||
const fields: string[] = []
|
||||
const values: any[] = [instanceId, update.id]
|
||||
let paramIdx = 3
|
||||
|
||||
if (update.unreadCount !== undefined) {
|
||||
fields.push(`unread_count = $${paramIdx++}`)
|
||||
values.push(update.unreadCount)
|
||||
}
|
||||
if (update.name) {
|
||||
fields.push(`name = $${paramIdx++}`)
|
||||
values.push(update.name)
|
||||
}
|
||||
|
||||
if (fields.length > 0) {
|
||||
await query(
|
||||
`UPDATE chats SET ${fields.join(', ')}, updated_at = NOW() WHERE instance_id = $1 AND jid = $2`,
|
||||
values
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[BaileysManager] Error updating chat:`, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// History sync - save chats and messages from history
|
||||
socket.ev.on('messaging-history.set', async ({ chats, contacts, messages, syncType }) => {
|
||||
console.log(`[BaileysManager] History sync received: ${chats?.length || 0} chats, ${contacts?.length || 0} contacts, ${messages?.length || 0} messages, type: ${syncType}`)
|
||||
|
||||
// Save chats
|
||||
if (chats?.length) {
|
||||
for (const chat of chats) {
|
||||
try {
|
||||
await query(
|
||||
`INSERT INTO chats (instance_id, jid, name, is_group, unread_count)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (instance_id, jid) DO UPDATE SET
|
||||
name = COALESCE(EXCLUDED.name, chats.name),
|
||||
unread_count = EXCLUDED.unread_count,
|
||||
updated_at = NOW()`,
|
||||
[instanceId, chat.id, chat.name || chat.id?.split('@')[0], chat.id?.includes('@g.us'), chat.unreadCount || 0]
|
||||
)
|
||||
} catch (err) {
|
||||
console.error(`[BaileysManager] Error saving chat:`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save messages
|
||||
if (messages?.length) {
|
||||
for (const msg of messages) {
|
||||
await this.saveMessage(instanceId, msg, false)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[BaileysManager] History sync processed`)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user