mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor!: cleaner message history sync
This is a breaking change, 1. three events (chats.set, contacts.set, messages.set) are now just one `messaging-history.set` event 2. no need to debounce for app state sync 3. added a new "conditional" chat update to allow for correct app state sync despite not having the chat available on hand
This commit is contained in:
@@ -70,28 +70,30 @@ export default (
|
||||
ev.on('connection.update', update => {
|
||||
Object.assign(state, update)
|
||||
})
|
||||
ev.on('chats.set', ({ chats: newChats, isLatest }) => {
|
||||
|
||||
ev.on('messaging-history.set', ({
|
||||
chats: newChats,
|
||||
contacts: newContacts,
|
||||
messages: newMessages,
|
||||
isLatest
|
||||
}) => {
|
||||
if(isLatest) {
|
||||
chats.clear()
|
||||
|
||||
for(const id in messages) {
|
||||
delete messages[id]
|
||||
}
|
||||
}
|
||||
|
||||
const chatsAdded = chats.insertIfAbsent(...newChats).length
|
||||
logger.debug({ chatsAdded }, 'synced chats')
|
||||
})
|
||||
ev.on('contacts.set', ({ contacts: newContacts }) => {
|
||||
|
||||
const oldContacts = contactsUpsert(newContacts)
|
||||
for(const jid of oldContacts) {
|
||||
delete contacts[jid]
|
||||
}
|
||||
|
||||
logger.debug({ deletedContacts: oldContacts.size, newContacts }, 'synced contacts')
|
||||
})
|
||||
ev.on('messages.set', ({ messages: newMessages, isLatest }) => {
|
||||
if(isLatest) {
|
||||
for(const id in messages) {
|
||||
delete messages[id]
|
||||
}
|
||||
}
|
||||
|
||||
for(const msg of newMessages) {
|
||||
const jid = msg.key.remoteJid!
|
||||
@@ -101,6 +103,7 @@ export default (
|
||||
|
||||
logger.debug({ messages: newMessages.length }, 'synced messages')
|
||||
})
|
||||
|
||||
ev.on('contacts.update', updates => {
|
||||
for(const update of updates) {
|
||||
if(contacts[update.id!]) {
|
||||
|
||||
Reference in New Issue
Block a user