From 2dfe5adbe11997e4ea1813c673a413d65515b350 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 15 Nov 2021 09:08:24 +0530 Subject: [PATCH] feat: push failed decryption messages as "CIPHERTEXT" --- src/Socket/messages-recv.ts | 66 +++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 1112caa..49dcfe5 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -342,14 +342,28 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { // recv a message ws.on('CB:message', async(stanza: BinaryNode) => { const dec = await decodeMessageStanza(stanza, authState) - const fullMessages: proto.IWebMessageInfo[] = [] - for(const msg of dec.successes) { - const { attrs } = stanza - const isGroup = !!stanza.attrs.participant - const sender = (attrs.participant || attrs.from)?.toString() - const isMe = areJidsSameUser(sender, authState.creds.me!.id) + if(dec.successes.length) { + ev.emit('auth-state.update', authState) + } - // send delivery receipt + const fullMessages: proto.IWebMessageInfo[] = [] + + const { attrs } = stanza + const isGroup = !!stanza.attrs.participant + const sender = (attrs.participant || attrs.from)?.toString() + const isMe = areJidsSameUser(sender, authState.creds.me!.id) + + const remoteJid = jidNormalizedUser(dec.chatId) + + const key: WAMessageKey = { + remoteJid, + fromMe: isMe, + id: dec.msgId, + participant: dec.participant + } + + for(const msg of dec.successes) { + // send message receipt let recpAttrs: { [_: string]: any } if(isMe) { recpAttrs = { @@ -383,16 +397,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { await sendDeliveryReceipt(dec.chatId, dec.participant, [dec.msgId]) logger.debug({ msgId: dec.msgId }, 'sent delivery receipt') - const remoteJid = jidNormalizedUser(dec.chatId) - const message = msg.deviceSentMessage?.message || msg fullMessages.push({ - key: { - remoteJid, - fromMe: isMe, - id: dec.msgId, - participant: dec.participant - }, + key, message, status: isMe ? proto.WebMessageInfo.WebMessageInfoStatus.SERVER_ACK : null, messageTimestamp: dec.timestamp, @@ -400,19 +407,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { participant: dec.participant }) } - - if(dec.successes.length) { - ev.emit('auth-state.update', authState) - if(fullMessages.length) { - ev.emit( - 'messages.upsert', - { - messages: fullMessages.map(m => proto.WebMessageInfo.fromObject(m)), - type: stanza.attrs.offline ? 'append' : 'notify' - } - ) - } - } for(const { error } of dec.failures) { logger.error( @@ -420,6 +414,22 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { 'failure in decrypting message' ) await sendRetryRequest(stanza) + + fullMessages.push({ + key, + messageStubType: WAMessageStubType.CIPHERTEXT, + messageStubParameters: [error.message] + }) + } + + if(fullMessages.length) { + ev.emit( + 'messages.upsert', + { + messages: fullMessages.map(m => proto.WebMessageInfo.fromObject(m)), + type: stanza.attrs.offline ? 'append' : 'notify' + } + ) } })