diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 2cb3591..df9a103 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -47,7 +47,7 @@ export class WAConnection extends Base { } else { chat.messages = oldChat.messages if (oldChat.t !== chat.t || oldChat.modify_tag !== chat.modify_tag) { - const changes = shallowChanges (oldChat, chat) + const changes = shallowChanges (oldChat, chat, { lookForDeletedKeys: true }) delete chat.metadata // remove group metadata as that may have changed; TODO, write better mechanism for this delete changes.messages @@ -150,7 +150,7 @@ export class WAConnection extends Base { contact.jid = whatsappID (contact.jid) const presentContact = contacts[contact.jid] if (presentContact) { - const changes = shallowChanges(presentContact, contact) + const changes = shallowChanges(presentContact, contact, { lookForDeletedKeys: false }) if (changes && Object.keys(changes).length > 0) { updatedContacts.push({ ...changes, jid: contact.jid }) } diff --git a/src/WAConnection/Utils.ts b/src/WAConnection/Utils.ts index 04a89e8..32d4e4a 100644 --- a/src/WAConnection/Utils.ts +++ b/src/WAConnection/Utils.ts @@ -45,16 +45,18 @@ export const newMessagesDB = (messages: WAMessage[] = []) => { return db } -export function shallowChanges (old: T, current: T): Partial { +export function shallowChanges (old: T, current: T, {lookForDeletedKeys}: {lookForDeletedKeys: boolean}): Partial { let changes: Partial = {} for (let key in current) { if (old[key] !== current[key]) { changes[key] = current[key] || null } } - for (let key in old) { - if (!changes[key] && old[key] !== current[key]) { - changes[key] = current[key] || null + if (lookForDeletedKeys) { + for (let key in old) { + if (!changes[key] && old[key] !== current[key]) { + changes[key] = current[key] || null + } } } return changes