From d803c219b6f90c93d30355931585da2c95ec6301 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 4 Feb 2021 22:38:30 +0530 Subject: [PATCH] Android not receiving message status update fix --- src/WAConnection/4.Events.ts | 60 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 76d2b2d..920ea2f 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -236,29 +236,31 @@ export class WAConnection extends Base { this.logger.debug ({ unhandled: true }, 'received message update for non-present message from ' + jid) } }) - this.on('CB:action,add:relay,received', json => { + // message status updates + const onMessageStatusUpdate = json => { json = json[2][0][1] - const chat = this.chats.get( whatsappID(json.jid) ) - const msg = chat?.messages.get(GET_MESSAGE_ID({ id: json.index, fromMe: json.owner === 'true' })) - if (msg) { - const MAP = { - read: WA_MESSAGE_STATUS_TYPE.READ, - message: WA_MESSAGE_STATUS_TYPE.DELIVERY_ACK, - error: WA_MESSAGE_STATUS_TYPE.ERROR - } - const status = MAP[json.type] - if (typeof status !== 'undefined') { - if (status > msg.status || status === WA_MESSAGE_STATUS_TYPE.ERROR) { - msg.status = status - this.emit('chat-update', { jid: chat.jid, messages: newMessagesDB([ msg ]) }) - } - } else { - this.logger.warn({ update: json }, 'received unknown message status update') - } - } else { - this.logger.debug ({ unhandled: true, update: json }, 'received message status update for non-present message') + const MAP = { + read: WA_MESSAGE_STATUS_TYPE.READ, + message: WA_MESSAGE_STATUS_TYPE.DELIVERY_ACK, + error: WA_MESSAGE_STATUS_TYPE.ERROR } - }) + this.onMessageStatusUpdate( + whatsappID(json.jid), + { id: json.index, fromMe: json.owner === 'true' }, + MAP[json.type] + ) + } + this.on('CB:action,add:relay,received', onMessageStatusUpdate) + this.on('CB:action,,received', onMessageStatusUpdate) + + this.on('CB:Msg,cmd:ack', json => ( + this.onMessageStatusUpdate( + whatsappID(json[1].to), + { id: json[1].id, fromMe: true }, + +json[1].ack + 1 + ) + )) + // If a user's contact has changed this.on ('CB:action,,user', json => { const node = json[2][0] @@ -430,6 +432,22 @@ export class WAConnection extends Base { return chat } } + protected onMessageStatusUpdate(jid: string, key: { id: string, fromMe: boolean }, status: WA_MESSAGE_STATUS_TYPE) { + const chat = this.chats.get( whatsappID(jid) ) + const msg = chat?.messages.get(GET_MESSAGE_ID(key)) + if (msg) { + if (typeof status !== 'undefined') { + if (status > msg.status || status === WA_MESSAGE_STATUS_TYPE.ERROR) { + msg.status = status + this.emit('chat-update', { jid: chat.jid, messages: newMessagesDB([ msg ]) }) + } + } else { + this.logger.warn({ update: status }, 'received unknown message status update') + } + } else { + this.logger.debug ({ unhandled: true, update: status, key }, 'received message status update for non-present message') + } + } protected contactAddOrGet (jid: string) { jid = whatsappID(jid) if (!this.contacts[jid]) this.contacts[jid] = { jid }