feat: add per-jid patching

This does not work in groups
This commit is contained in:
Rajeh Taher
2025-03-10 14:51:13 +02:00
parent 40b8cc5383
commit e1aadc5546
2 changed files with 20 additions and 9 deletions

View File

@@ -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(

View File

@@ -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> | proto.IMessage
recipientJids?: string[],
) => Promise<PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID> | PatchedMessageWithRecipientJID[] | PatchedMessageWithRecipientJID
/** verify app state MACs */
appStateMacVerification: {