From afea6c249e1b1d6b2c5b8d1d53f16fed4a61db1a Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 18 Mar 2021 13:21:33 +0530 Subject: [PATCH] Delete chat fix --- src/WAConnection/4.Events.ts | 6 ++-- src/WAConnection/7.MessagesExtra.ts | 47 +++++++++++++++-------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index e23306c..9c13806 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -1,7 +1,7 @@ import * as QR from 'qrcode-terminal' import { WAConnection as Base } from './3.Connect' -import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate, WAContactUpdate, WAMetric, WAFlag } from './Constants' -import { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils' +import { WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate, WAContactUpdate, WAMetric, WAFlag } from './Constants' +import { whatsappID, unixTimestampSeconds, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils' import KeyedDB from '@adiwajshing/keyed-db' import { Mutex } from './Mutex' @@ -316,7 +316,7 @@ export class WAConnection extends Base { const FUNCTIONS = { 'delete': () => { chat['delete'] = 'true' - this.chats.delete(chat) + this.chats.deleteById(chat.jid) return 'delete' }, 'clear': () => { diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 5426bd8..a1197a3 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -378,13 +378,6 @@ export class WAConnection extends Base { await this.relayWAMessage (waMessage) return waMessage } - /** - * Delete the chat of a given ID - * @deprecated -- use `modifyChat(jid, 'delete')` instead - * */ - deleteChat (jid: string) { - return this.modifyChat(jid, 'delete') - } /** * Clear the chat messages * @param jid the ID of the person/group you are modifiying @@ -447,23 +440,31 @@ export class WAConnection extends Base { const response = await this.setQuery ([['chat', chatAttrs, null]], [ WAMetric.chat, WAFlag.ignore ]) if (chat && response.status === 200) { - if (type === ChatModification.clear) { - if (includeStarred) { - chat.messages.clear () - } else { - chat.messages = chat.messages.filter(m => m.starred) - } + switch(type) { + case ChatModification.clear: + if (includeStarred) { + chat.messages.clear() + } else { + chat.messages = chat.messages.filter(m => m.starred) + } + break + case ChatModification.delete: + this.chats.deleteById(jid) + this.emit('chat-update', { jid, delete: 'true' }) + break + default: + this.chats.update(jid, chat => { + if (type.includes('un')) { + type = type.replace ('un', '') as ChatModification + delete chat[type.replace('un','')] + this.emit ('chat-update', { jid, [type]: false }) + } else { + chat[type] = chatAttrs[type] || 'true' + this.emit ('chat-update', { jid, [type]: chat[type] }) + } + }) + break } - this.chats.update(jid, chat => { - if (type.includes('un')) { - type = type.replace ('un', '') as ChatModification - delete chat[type.replace('un','')] - this.emit ('chat-update', { jid, [type]: false }) - } else { - chat[type] = chatAttrs[type] || 'true' - this.emit ('chat-update', { jid, [type]: chat[type] }) - } - }) } return response }