Compare commits

..

1 Commits

Author SHA1 Message Date
Renato Alcara 45cbdc4233 fix: add biz node injection and quality_control for carousel WhatsApp Web rendering
- Add interactive message detection helpers (getButtonType, isCarouselMessage, etc.)
- Inject biz node with interactive/native_flow for non-carousel interactive messages
- Inject biz + quality_control with decision_id for carousel messages
- Skip bot node for native_flow/carousel/catalog (breaks Web rendering)
- Force device-identity inclusion for carousel messages
- Append deferred biz/bot/quality_control nodes after device-identity and tctoken
- Store tctoken under both LID and PN for reliable lookup

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 21:22:43 -03:00
2 changed files with 6 additions and 102 deletions
+5 -54
View File
@@ -417,39 +417,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
)
}
const resolveSessionJid = async (jid: string) => {
if (isLidUser(jid) || isHostedLidUser(jid)) {
return jid
}
if (isPnUser(jid) || isHostedPnUser(jid)) {
return await signalRepository.lidMapping.getLIDForPN(jid) || jid
}
return jid
}
const canonicalizeSessionRecipients = async (recipientJids: string[]) => {
const uniqueRecipients = [...new Set(recipientJids)]
const canonicalRecipients: string[] = []
for (const jid of uniqueRecipients) {
const canonicalJid = await resolveSessionJid(jid)
if (!canonicalRecipients.includes(canonicalJid)) {
canonicalRecipients.push(canonicalJid)
}
}
if (canonicalRecipients.length !== uniqueRecipients.length || canonicalRecipients.some((jid, index) => jid !== uniqueRecipients[index])) {
logger.debug(
{ before: uniqueRecipients, after: canonicalRecipients },
'[SESSION] Canonicalized recipient addressing for session reuse'
)
}
return canonicalRecipients
}
const assertSessions = async (jids: string[], force?: boolean) => {
let didFetchNewSession = false
const uniqueJids = [...new Set(jids)] // Deduplicate JIDs
@@ -459,32 +426,22 @@ export const makeMessagesSocket = (config: SocketConfig) => {
// Check peerSessionsCache and validate sessions using libsignal loadSession
for (const jid of uniqueJids) {
const canonicalJid = await resolveSessionJid(jid)
const signalIds = [
signalRepository.jidToSignalProtocolAddress(jid),
signalRepository.jidToSignalProtocolAddress(canonicalJid)
]
const cachedSession = signalIds
.map(signalId => peerSessionsCache.get(signalId))
.find(session => session !== undefined)
const signalId = signalRepository.jidToSignalProtocolAddress(jid)
const cachedSession = peerSessionsCache.get(signalId)
if (cachedSession !== undefined) {
if (cachedSession && !force) {
continue // Session exists in cache
}
} else {
const sessionValidation = await signalRepository.validateSession(canonicalJid)
const sessionValidation = await signalRepository.validateSession(jid)
const hasSession = sessionValidation.exists
for (const signalId of signalIds) {
peerSessionsCache.set(signalId, hasSession)
}
peerSessionsCache.set(signalId, hasSession)
if (hasSession && !force) {
continue
}
}
jidsRequiringFetch.push(canonicalJid)
jidsRequiringFetch.push(jid)
}
if (jidsRequiringFetch.length) {
@@ -1011,13 +968,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
allRecipients.push(jid)
}
await assertSessions(allRecipients)
const effectiveMeRecipients = await canonicalizeSessionRecipients(meRecipients)
const effectiveOtherRecipients = await canonicalizeSessionRecipients(otherRecipients)
const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients]
await assertSessions(effectiveAllRecipients)
const [
{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 },
+1 -48
View File
@@ -41,12 +41,10 @@ import type { ILogger } from './logger'
import {
downloadContentFromMessage,
encryptedStream,
extractImageThumb,
generateThumbnail,
getAudioDuration,
getAudioWaveform,
getRawMediaUploadData,
getStream,
type MediaDownloadOptions
} from './messages-media'
import { shouldIncludeReportingToken } from './reporting-utils'
@@ -475,48 +473,6 @@ export const formatNativeFlowButton = (button: NativeButton): NativeFlowButton =
}
}
const recoverCarouselImageMetadata = async (
card: CarouselMessageOptions['cards'][number],
imageMessage: proto.Message.IImageMessage,
mediaOptions: MessageContentGenerationOptions
) => {
if (imageMessage.jpegThumbnail && imageMessage.height && imageMessage.width) {
return
}
try {
const { stream } = await getStream(card.image!, mediaOptions.options)
const thumb = await extractImageThumb(stream)
if (!imageMessage.jpegThumbnail) {
imageMessage.jpegThumbnail = thumb.buffer
}
if (!imageMessage.width && thumb.original.width) {
imageMessage.width = thumb.original.width
}
if (!imageMessage.height && thumb.original.height) {
imageMessage.height = thumb.original.height
}
mediaOptions.logger?.info(
{
cardTitle: card.title,
hasJpegThumbnail: !!imageMessage.jpegThumbnail,
width: imageMessage.width,
height: imageMessage.height
},
'[CAROUSEL] Recovered image thumbnail/dimensions from source media'
)
} catch (error) {
mediaOptions.logger?.warn(
{ cardTitle: card.title, error },
'[CAROUSEL] Failed to recover image thumbnail/dimensions from source media'
)
}
}
/**
* Generates a button message using Native Flow format wrapped in viewOnceMessage
* This is the modern approach for button messages that works on iOS and Android
@@ -692,10 +648,7 @@ export const generateCarouselMessage = async (
if (hasMedia && mediaOptions) {
if (card.image) {
const { imageMessage } = await prepareWAMessageMedia({ image: card.image }, mediaOptions)
if (imageMessage) {
await recoverCarouselImageMetadata(card, imageMessage, mediaOptions)
}
// Validate image fields needed for WhatsApp rendering
if (imageMessage && !imageMessage.jpegThumbnail) {
mediaOptions.logger?.warn(
{ cardTitle: card.title },