Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 871ad3711e | |||
| 3d8778561a | |||
| 9d6ada6514 | |||
| 5e7c53eff7 | |||
| 0640de9a38 | |||
| 847e213443 | |||
| 7a66f8b3d6 | |||
| 5b42aaa642 | |||
| 5c6f38d885 | |||
| a790bf616d |
+65
-38
@@ -74,8 +74,6 @@ import {
|
|||||||
import { USyncQuery, USyncUser } from '../WAUSync'
|
import { USyncQuery, USyncUser } from '../WAUSync'
|
||||||
import { makeNewsletterSocket } from './newsletter'
|
import { makeNewsletterSocket } from './newsletter'
|
||||||
|
|
||||||
/* eslint-disable max-depth */
|
|
||||||
|
|
||||||
export const makeMessagesSocket = (config: SocketConfig) => {
|
export const makeMessagesSocket = (config: SocketConfig) => {
|
||||||
const {
|
const {
|
||||||
logger,
|
logger,
|
||||||
@@ -601,7 +599,57 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
return patchedMessage
|
return patchedMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
/* eslint-disable max-depth */
|
const encryptPatchedMessageForRecipient = async ({
|
||||||
|
jid,
|
||||||
|
patchedMessage,
|
||||||
|
dsmMessage,
|
||||||
|
meId,
|
||||||
|
meLid,
|
||||||
|
meLidUser,
|
||||||
|
extraAttrs,
|
||||||
|
onPkmsg
|
||||||
|
}: {
|
||||||
|
jid: string
|
||||||
|
patchedMessage: proto.IMessage
|
||||||
|
dsmMessage: proto.IMessage | undefined
|
||||||
|
meId: string
|
||||||
|
meLid: string | undefined
|
||||||
|
meLidUser: string | null | undefined
|
||||||
|
extraAttrs: BinaryNode['attrs'] | undefined
|
||||||
|
onPkmsg: () => void
|
||||||
|
}) => {
|
||||||
|
if (!jid) return null
|
||||||
|
|
||||||
|
try {
|
||||||
|
const msgToEncrypt = resolveDsmMessageForRecipient(jid, patchedMessage, dsmMessage, meId, meLid, meLidUser)
|
||||||
|
const bytes = encodeWAMessage(msgToEncrypt)
|
||||||
|
const mutexKey = jid
|
||||||
|
|
||||||
|
return await encryptionMutex.mutex(mutexKey, async () => {
|
||||||
|
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
||||||
|
|
||||||
|
if (type === 'pkmsg') {
|
||||||
|
onPkmsg()
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
tag: 'to',
|
||||||
|
attrs: { jid },
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
tag: 'enc',
|
||||||
|
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
||||||
|
content: ciphertext
|
||||||
|
}
|
||||||
|
]
|
||||||
|
} as BinaryNode
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ jid, err }, 'Failed to encrypt for recipient')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const createParticipantNodes = async (
|
const createParticipantNodes = async (
|
||||||
recipientJids: string[],
|
recipientJids: string[],
|
||||||
message: proto.IMessage,
|
message: proto.IMessage,
|
||||||
@@ -622,42 +670,22 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
if (!meId) throw new Boom('Not authenticated', { statusCode: 401 })
|
if (!meId) throw new Boom('Not authenticated', { statusCode: 401 })
|
||||||
const meLid = authState.creds.me?.lid
|
const meLid = authState.creds.me?.lid
|
||||||
const meLidUser = meLid ? (jidDecode(meLid)?.user ?? null) : null
|
const meLidUser = meLid ? (jidDecode(meLid)?.user ?? null) : null
|
||||||
|
const onPkmsg = () => {
|
||||||
|
shouldIncludeDeviceIdentity = true
|
||||||
|
}
|
||||||
|
|
||||||
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
||||||
async ({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) => {
|
({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) =>
|
||||||
try {
|
encryptPatchedMessageForRecipient({
|
||||||
if (!jid) return null
|
jid,
|
||||||
|
patchedMessage,
|
||||||
const msgToEncrypt = resolveDsmMessageForRecipient(jid, patchedMessage, dsmMessage, meId, meLid, meLidUser)
|
dsmMessage,
|
||||||
const bytes = encodeWAMessage(msgToEncrypt)
|
meId,
|
||||||
const mutexKey = jid
|
meLid,
|
||||||
|
meLidUser,
|
||||||
const node = await encryptionMutex.mutex(mutexKey, async () => {
|
extraAttrs,
|
||||||
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
onPkmsg
|
||||||
|
})
|
||||||
if (type === 'pkmsg') {
|
|
||||||
shouldIncludeDeviceIdentity = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
tag: 'to',
|
|
||||||
attrs: { jid },
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
tag: 'enc',
|
|
||||||
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
|
||||||
content: ciphertext
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return node
|
|
||||||
} catch (err) {
|
|
||||||
logger.error({ jid, err }, 'Failed to encrypt for recipient')
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[]
|
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[]
|
||||||
@@ -669,7 +697,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
return { nodes, shouldIncludeDeviceIdentity }
|
return { nodes, shouldIncludeDeviceIdentity }
|
||||||
}
|
}
|
||||||
/* eslint-enable max-depth */
|
|
||||||
|
|
||||||
// Interactive message detection and binary node injection
|
// Interactive message detection and binary node injection
|
||||||
|
|
||||||
|
|||||||
+54
-36
@@ -635,6 +635,59 @@ export const generateCarouselMessage = async (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const recoverCarouselImageMetadata = async (
|
||||||
|
imageMessage: proto.Message.IImageMessage | null | undefined,
|
||||||
|
cardImage: WAMediaUpload,
|
||||||
|
cardTitle: string | undefined,
|
||||||
|
uploadOptions: MessageContentGenerationOptions
|
||||||
|
) => {
|
||||||
|
if (!imageMessage) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const missingThumbnail = !imageMessage.jpegThumbnail
|
||||||
|
const missingWidth = !imageMessage.width
|
||||||
|
const missingHeight = !imageMessage.height
|
||||||
|
if (!missingThumbnail && !missingWidth && !missingHeight) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { stream } = await getStream(cardImage, uploadOptions.options)
|
||||||
|
const { buffer, original } = await extractImageThumb(stream)
|
||||||
|
|
||||||
|
if (missingThumbnail) {
|
||||||
|
imageMessage.jpegThumbnail = buffer.toString('base64')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (missingWidth && original.width) {
|
||||||
|
imageMessage.width = original.width
|
||||||
|
}
|
||||||
|
|
||||||
|
if (missingHeight && original.height) {
|
||||||
|
imageMessage.height = original.height
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadOptions.logger?.info(
|
||||||
|
{
|
||||||
|
cardTitle,
|
||||||
|
recoveredThumbnail: !!imageMessage.jpegThumbnail,
|
||||||
|
width: imageMessage.width,
|
||||||
|
height: imageMessage.height
|
||||||
|
},
|
||||||
|
'[CAROUSEL] Recovered image metadata from source media'
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
uploadOptions.logger?.warn(
|
||||||
|
{
|
||||||
|
cardTitle,
|
||||||
|
trace: error instanceof Error ? error.stack : String(error)
|
||||||
|
},
|
||||||
|
'[CAROUSEL] Failed source-media thumbnail fallback'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Map cards to the carousel format (processing media)
|
// Map cards to the carousel format (processing media)
|
||||||
const carouselCards = await Promise.all(
|
const carouselCards = await Promise.all(
|
||||||
cards.map(async card => {
|
cards.map(async card => {
|
||||||
@@ -653,42 +706,7 @@ export const generateCarouselMessage = async (
|
|||||||
|
|
||||||
// Mirror the working Pastorini-style result: every carousel image card should
|
// Mirror the working Pastorini-style result: every carousel image card should
|
||||||
// carry a jpegThumbnail and dimensions before it reaches the Web live renderer.
|
// carry a jpegThumbnail and dimensions before it reaches the Web live renderer.
|
||||||
if (imageMessage && (!imageMessage.jpegThumbnail || !imageMessage.height || !imageMessage.width)) {
|
await recoverCarouselImageMetadata(imageMessage, card.image, card.title, mediaOptions)
|
||||||
try {
|
|
||||||
const { stream } = await getStream(card.image, mediaOptions.options)
|
|
||||||
const { buffer, original } = await extractImageThumb(stream)
|
|
||||||
|
|
||||||
if (!imageMessage.jpegThumbnail) {
|
|
||||||
imageMessage.jpegThumbnail = buffer.toString('base64')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!imageMessage.width && original.width) {
|
|
||||||
imageMessage.width = original.width
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!imageMessage.height && original.height) {
|
|
||||||
imageMessage.height = original.height
|
|
||||||
}
|
|
||||||
|
|
||||||
mediaOptions.logger?.info(
|
|
||||||
{
|
|
||||||
cardTitle: card.title,
|
|
||||||
recoveredThumbnail: !!imageMessage.jpegThumbnail,
|
|
||||||
width: imageMessage.width,
|
|
||||||
height: imageMessage.height
|
|
||||||
},
|
|
||||||
'[CAROUSEL] Recovered image metadata from source media'
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
mediaOptions.logger?.warn(
|
|
||||||
{
|
|
||||||
cardTitle: card.title,
|
|
||||||
trace: error instanceof Error ? error.stack : String(error)
|
|
||||||
},
|
|
||||||
'[CAROUSEL] Failed source-media thumbnail fallback'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate image fields needed for WhatsApp rendering
|
// Validate image fields needed for WhatsApp rendering
|
||||||
if (imageMessage && !imageMessage.jpegThumbnail) {
|
if (imageMessage && !imageMessage.jpegThumbnail) {
|
||||||
|
|||||||
Reference in New Issue
Block a user