feat: absorb existing message updates into upsert

This commit is contained in:
Adhiraj Singh
2022-07-22 15:33:04 +05:30
parent 8f9e46fadd
commit 665e461e83

View File

@@ -168,14 +168,18 @@ function append<E extends BufferableEvent>(
case 'chats.update': case 'chats.update':
for(const update of eventData as Partial<Chat>[]) { for(const update of eventData as Partial<Chat>[]) {
const chatId = update.id! const chatId = update.id!
// if there is an existing upsert, merge the update into it
const upsert = data.chatUpserts[chatId] const upsert = data.chatUpserts[chatId]
if(upsert) { if(upsert) {
concatChats(upsert, update) concatChats(upsert, update)
} else { } else {
// merge the update into the existing update
const chatUpdate = data.chatUpdates[chatId] || { } const chatUpdate = data.chatUpdates[chatId] || { }
data.chatUpdates[chatId] = concatChats(chatUpdate, update) data.chatUpdates[chatId] = concatChats(chatUpdate, update)
} }
// if the chat has been updated
// ignore any existing chat delete
if(data.chatDeletes.has(chatId)) { if(data.chatDeletes.has(chatId)) {
data.chatDeletes.delete(chatId) data.chatDeletes.delete(chatId)
} }
@@ -185,6 +189,7 @@ function append<E extends BufferableEvent>(
case 'chats.delete': case 'chats.delete':
for(const chatId of eventData as string[]) { for(const chatId of eventData as string[]) {
data.chatDeletes.add(chatId) data.chatDeletes.add(chatId)
// remove any prior updates & upserts
if(data.chatUpdates[chatId]) { if(data.chatUpdates[chatId]) {
delete data.chatUpdates[chatId] delete data.chatUpdates[chatId]
} }
@@ -212,10 +217,12 @@ function append<E extends BufferableEvent>(
const contactUpdates = eventData as BaileysEventMap<any>['contacts.update'] const contactUpdates = eventData as BaileysEventMap<any>['contacts.update']
for(const update of contactUpdates) { for(const update of contactUpdates) {
const id = update.id! const id = update.id!
// merge into prior upsert
const upsert = data.contactUpserts[update.id!] const upsert = data.contactUpserts[update.id!]
if(upsert) { if(upsert) {
Object.assign(upsert, update) Object.assign(upsert, update)
} else { } else {
// merge into prior update
const contactUpdate = data.contactUpdates[id] || { } const contactUpdate = data.contactUpdates[id] || { }
data.contactUpdates[id] = Object.assign(contactUpdate, update) data.contactUpdates[id] = Object.assign(contactUpdate, update)
} }
@@ -231,6 +238,12 @@ function append<E extends BufferableEvent>(
message.messageTimestamp = existing.message.messageTimestamp message.messageTimestamp = existing.message.messageTimestamp
} }
if(data.messageUpdates[key]) {
logger.debug('absorbed prior message update in message upsert')
Object.assign(message, data.messageUpdates[key].update)
delete data.messageUpdates[key]
}
data.messageUpserts[key] = { data.messageUpserts[key] = {
message, message,
type: type === 'notify' || existing?.type === 'notify' type: type === 'notify' || existing?.type === 'notify'