From 7d5c28cf72e7744f35ee96c2505e4983c20bba06 Mon Sep 17 00:00:00 2001 From: Matheus Filype <67132916+Santosl2@users.noreply.github.com> Date: Fri, 26 Sep 2025 09:48:04 -0300 Subject: [PATCH] fix: No session found to decrypt message and recover lost messages due to this reason (#1822) * fix: improve message resend logic by adding checks for message IDs * Revert "fix: improve message resend logic by adding checks for message IDs" This reverts commit c03f9d8e6fc6cbfbb9d1f8f67c169700e704213d. * fix: handle device identity inclusion for retry resends in message sending * feat: lint fix --------- Co-authored-by: Rajeh Taher --- src/Socket/messages-send.ts | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 05aa6dca..66fcd5ac 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -569,7 +569,8 @@ export const makeMessagesSocket = (config: SocketConfig) => { ) => { const meId = authState.creds.me!.id const meLid = authState.creds.me?.lid - let shouldIncludeDeviceIdentity = false + const isRetryResend = Boolean(participant?.jid) + let shouldIncludeDeviceIdentity = isRetryResend const statusJid = 'status@broadcast' const { user, server } = jidDecode(jid)! @@ -722,7 +723,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { for (const device of devices) { const deviceJid = device.wireJid const hasKey = !!senderKeyMap[deviceJid] - if (!hasKey || !!participant) { + if (!hasKey && !isRetryResend) { senderKeyRecipients.push(deviceJid) senderKeyMap[deviceJid] = true } @@ -752,13 +753,30 @@ export const makeMessagesSocket = (config: SocketConfig) => { participants.push(...result.nodes) } - binaryNodeContent.push({ - tag: 'enc', - attrs: { v: '2', type: 'skmsg', ...extraAttrs }, - content: ciphertext - }) + if (isRetryResend) { + const { type, ciphertext: encryptedContent } = await signalRepository.encryptMessage({ + data: bytes, + jid: participant?.jid! + }) - await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } }) + binaryNodeContent.push({ + tag: 'enc', + attrs: { + v: '2', + type, + count: participant!.count.toString() + }, + content: encryptedContent + }) + } else { + binaryNodeContent.push({ + tag: 'enc', + attrs: { v: '2', type: 'skmsg', ...extraAttrs }, + content: ciphertext + }) + + await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } }) + } } else { const { user: ownUser } = jidDecode(ownId)!