From e1aadc5546c6bf8ee8c299e4b88b254bcc0cdeaa Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Mon, 10 Mar 2025 14:51:13 +0200 Subject: [PATCH] feat: add per-jid patching This does not work in groups --- src/Socket/messages-send.ts | 23 ++++++++++++++++------- src/Types/Socket.ts | 6 ++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 2677542..673ea24 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -280,22 +280,26 @@ export const makeMessagesSocket = (config: SocketConfig) => { message: proto.IMessage, extraAttrs?: BinaryNode['attrs'] ) => { - const patched = await patchMessageBeforeSending(message, jids) - const bytes = encodeWAMessage(patched) + let patched = await patchMessageBeforeSending(message, jids) + if(!Array.isArray(patched)) { + patched = [{ recipientJid: jids[0], ...patched }] + } let shouldIncludeDeviceIdentity = false const nodes = await Promise.all( - jids.map( - async jid => { + patched.map( + async patchedMessageWithJid => { + const { recipientJid: jid, ...patchedMessage } = patchedMessageWithJid + const bytes = encodeWAMessage(patchedMessage) const { type, ciphertext } = await signalRepository - .encryptMessage({ jid, data: bytes }) + .encryptMessage({ jid: jid!, data: bytes }) if(type === 'pkmsg') { shouldIncludeDeviceIdentity = true } const node: BinaryNode = { tag: 'to', - attrs: { jid }, + attrs: { jid: jid! }, content: [{ tag: 'enc', attrs: { @@ -401,7 +405,12 @@ export const makeMessagesSocket = (config: SocketConfig) => { devices.push(...additionalDevices) } - const patched = await patchMessageBeforeSending(message, devices.map(d => jidEncode(d.user, isLid ? 'lid' : 's.whatsapp.net', d.device))) + const patched = await patchMessageBeforeSending(message) + + if(Array.isArray(patched)) { + throw new Boom('Per-jid patching is not supported in groups') + } + const bytes = encodeWAMessage(patched) const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage( diff --git a/src/Types/Socket.ts b/src/Types/Socket.ts index 79fd83d..61dfc5f 100644 --- a/src/Types/Socket.ts +++ b/src/Types/Socket.ts @@ -23,6 +23,8 @@ export type CacheStore = { flushAll(): void } +export type PatchedMessageWithRecipientJID = proto.IMessage & {recipientJid?: string} + export type SocketConfig = { /** the WS url to connect to WA */ waWebSocketUrl: string | URL @@ -104,8 +106,8 @@ export type SocketConfig = { * */ patchMessageBeforeSending: ( msg: proto.IMessage, - recipientJids: string[], - ) => Promise | proto.IMessage + recipientJids?: string[], + ) => Promise | PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID /** verify app state MACs */ appStateMacVerification: {