@@ -35,7 +35,12 @@ function getFutureProofInnerMessage(message: proto.IMessage): proto.IMessage | u
3535 return undefined ;
3636}
3737
38- function buildMessageChain ( message : proto . IMessage | undefined ) : proto . IMessage [ ] {
38+ type WhatsAppInboundMessageProjection = readonly proto . IMessage [ ] ;
39+ type WhatsAppInboundMessageSource = proto . IMessage | WhatsAppInboundMessageProjection | undefined ;
40+
41+ export function projectWhatsAppInboundMessage (
42+ message : proto . IMessage | undefined ,
43+ ) : WhatsAppInboundMessageProjection {
3944 const chain : proto . IMessage [ ] = [ ] ;
4045 let current = normalizeMessageContent ( message ) ;
4146 while ( current && chain . length < 4 ) {
@@ -45,11 +50,25 @@ function buildMessageChain(message: proto.IMessage | undefined): proto.IMessage[
4550 return chain ;
4651}
4752
53+ function isWhatsAppInboundMessageProjection (
54+ message : WhatsAppInboundMessageSource ,
55+ ) : message is WhatsAppInboundMessageProjection {
56+ return Array . isArray ( message ) ;
57+ }
58+
59+ function resolveWhatsAppInboundMessageProjection (
60+ message : WhatsAppInboundMessageSource ,
61+ ) : WhatsAppInboundMessageProjection {
62+ return isWhatsAppInboundMessageProjection ( message )
63+ ? message
64+ : projectWhatsAppInboundMessage ( message ) ;
65+ }
66+
4867export function findMessageSection < K extends keyof proto . IMessage > (
4968 rawMessage : proto . IMessage | undefined ,
5069 sectionNames : readonly K [ ] ,
5170) : { name : K ; value : Record < string , unknown > } | undefined {
52- const chain = buildMessageChain ( rawMessage ) ;
71+ const chain = projectWhatsAppInboundMessage ( rawMessage ) ;
5372 for ( const name of sectionNames ) {
5473 for ( const message of chain ) {
5574 const value = message [ name ] ;
@@ -61,9 +80,8 @@ export function findMessageSection<K extends keyof proto.IMessage>(
6180 return undefined ;
6281}
6382
64- function unwrapMessage ( message : proto . IMessage | undefined ) : proto . IMessage | undefined {
65- const chain = buildMessageChain ( message ) ;
66- return chain . at ( - 1 ) ;
83+ function unwrapMessage ( message : WhatsAppInboundMessageSource ) : proto . IMessage | undefined {
84+ return resolveWhatsAppInboundMessageProjection ( message ) . at ( - 1 ) ;
6785}
6886
6987function extractContextInfoFromMessage ( message : proto . IMessage ) : proto . IContextInfo | undefined {
@@ -117,9 +135,9 @@ function extractContextInfoFromMessage(message: proto.IMessage): proto.IContextI
117135}
118136
119137export function extractContextInfo (
120- message : proto . IMessage | undefined ,
138+ message : WhatsAppInboundMessageSource ,
121139) : proto . IContextInfo | undefined {
122- for ( const candidate of buildMessageChain ( message ) ) {
140+ for ( const candidate of resolveWhatsAppInboundMessageProjection ( message ) ) {
123141 const contextInfo = extractContextInfoFromMessage ( candidate ) ;
124142 if ( contextInfo ) {
125143 return contextInfo ;
@@ -128,9 +146,9 @@ export function extractContextInfo(
128146 return undefined ;
129147}
130148
131- export function extractMentionedJids ( rawMessage : proto . IMessage | undefined ) : string [ ] | undefined {
149+ export function extractMentionedJids ( message : WhatsAppInboundMessageSource ) : string [ ] | undefined {
132150 // Context ownership already follows Baileys envelopes without entering quoted messages.
133- const mentionedJids = extractContextInfo ( rawMessage ) ?. mentionedJid ?. filter ( Boolean ) ;
151+ const mentionedJids = extractContextInfo ( message ) ?. mentionedJid ?. filter ( Boolean ) ;
134152 if ( ! mentionedJids ?. length ) {
135153 return undefined ;
136154 }
@@ -157,8 +175,9 @@ function extractNativeFlowResponseText(
157175 }
158176}
159177
160- export function extractText ( rawMessage : proto . IMessage | undefined ) : string | undefined {
161- const message = unwrapMessage ( rawMessage ) ;
178+ export function extractText ( source : WhatsAppInboundMessageSource ) : string | undefined {
179+ const projection = resolveWhatsAppInboundMessageProjection ( source ) ;
180+ const message = unwrapMessage ( projection ) ;
162181 if ( ! message ) {
163182 return undefined ;
164183 }
@@ -215,7 +234,7 @@ export function extractText(rawMessage: proto.IMessage | undefined): string | un
215234 }
216235 }
217236 const contactPlaceholder =
218- extractContactPlaceholder ( message ) ??
237+ extractContactPlaceholder ( projection ) ??
219238 ( extracted && extracted !== message
220239 ? extractContactPlaceholder ( extracted as proto . IMessage | undefined )
221240 : undefined ) ;
@@ -225,14 +244,14 @@ export function extractText(rawMessage: proto.IMessage | undefined): string | un
225244 return undefined ;
226245}
227246
228- export function extractExternalAdReplyContext ( rawMessage : proto . IMessage | undefined ) :
247+ export function extractExternalAdReplyContext ( source : WhatsAppInboundMessageSource ) :
229248 | {
230249 title ?: string ;
231250 sourceUrl ?: string ;
232251 body ?: string ;
233252 }
234253 | undefined {
235- const message = unwrapMessage ( rawMessage ) ;
254+ const message = unwrapMessage ( source ) ;
236255 const adReply =
237256 message ?. imageMessage ?. contextInfo ?. externalAdReply ??
238257 message ?. videoMessage ?. contextInfo ?. externalAdReply ;
@@ -246,9 +265,9 @@ export function extractExternalAdReplyContext(rawMessage: proto.IMessage | undef
246265}
247266
248267export function extractMediaKind (
249- rawMessage : proto . IMessage | undefined ,
268+ source : WhatsAppInboundMessageSource ,
250269) : NonNullable < ChannelInboundMediaInput [ "kind" ] > | undefined {
251- const message = unwrapMessage ( rawMessage ) ;
270+ const message = unwrapMessage ( source ) ;
252271 if ( ! message ) {
253272 return undefined ;
254273 }
@@ -271,8 +290,8 @@ export function extractMediaKind(
271290 return undefined ;
272291}
273292
274- function extractContactPlaceholder ( rawMessage : proto . IMessage | undefined ) : string | undefined {
275- const contactContext = extractContactContext ( rawMessage ) ;
293+ function extractContactPlaceholder ( source : WhatsAppInboundMessageSource ) : string | undefined {
294+ const contactContext = extractContactContext ( source ) ;
276295 if ( ! contactContext ) {
277296 return undefined ;
278297 }
@@ -284,9 +303,9 @@ function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): stri
284303}
285304
286305export function extractContactContext (
287- rawMessage : proto . IMessage | undefined ,
306+ source : WhatsAppInboundMessageSource ,
288307) : WhatsAppStructuredContactContext | undefined {
289- const message = unwrapMessage ( rawMessage ) ;
308+ const message = unwrapMessage ( source ) ;
290309 if ( ! message ) {
291310 return undefined ;
292311 }
@@ -326,9 +345,9 @@ function describeContact(input: { displayName?: string | null; vcard?: string |
326345}
327346
328347export function extractLocationData (
329- rawMessage : proto . IMessage | undefined ,
348+ source : WhatsAppInboundMessageSource ,
330349) : NormalizedLocation | null {
331- const message = unwrapMessage ( rawMessage ) ;
350+ const message = unwrapMessage ( source ) ;
332351 if ( ! message ) {
333352 return null ;
334353 }
@@ -380,13 +399,18 @@ export function extractLocationData(
380399}
381400
382401export function describeReplyContext (
383- rawMessage : proto . IMessage | undefined ,
402+ source : WhatsAppInboundMessageSource ,
384403) : WhatsAppReplyContext | null {
385- const message = unwrapMessage ( rawMessage ) ;
404+ const projection = resolveWhatsAppInboundMessageProjection ( source ) ;
405+ const message = unwrapMessage ( projection ) ;
386406 if ( ! message ) {
387407 return null ;
388408 }
389- const contextInfo = extractContextInfo ( message ) ;
409+ const contextProjection =
410+ projection . length === 1 && projection [ 0 ] === message
411+ ? projection
412+ : projectWhatsAppInboundMessage ( message ) ;
413+ const contextInfo = extractContextInfo ( contextProjection ) ;
390414 const quoted = normalizeMessageContent ( contextInfo ?. quotedMessage as proto . IMessage | undefined ) ;
391415 if ( ! quoted && ! contextInfo ?. stanzaId ) {
392416 return null ;
@@ -404,11 +428,12 @@ export function describeReplyContext(
404428 sender,
405429 } ;
406430 }
407- const location = extractLocationData ( quoted ) ;
431+ const quotedProjection = projectWhatsAppInboundMessage ( quoted ) ;
432+ const location = extractLocationData ( quotedProjection ) ;
408433 const locationText = location ? formatLocationText ( location ) : undefined ;
409- const text = extractText ( quoted ) ;
434+ const text = extractText ( quotedProjection ) ;
410435 const body = [ text , locationText ] . filter ( Boolean ) . join ( "\n" ) . trim ( ) ;
411- const mediaKind = extractMediaKind ( quoted ) ;
436+ const mediaKind = extractMediaKind ( quotedProjection ) ;
412437 const media = mediaKind
413438 ? { kind : mediaKind , contentType : resolveInboundMediaMimetype ( quoted ) }
414439 : undefined ;
@@ -450,22 +475,23 @@ function hasInteractiveResponseContent(message: proto.IMessage | undefined): boo
450475 * `messages.upsert` stream as real messages but should not trigger pairing
451476 * access-control side effects.
452477 */
453- export function hasInboundUserContent ( rawMessage : proto . IMessage | undefined ) : boolean {
454- if ( ! rawMessage ) {
478+ export function hasInboundUserContent ( source : WhatsAppInboundMessageSource ) : boolean {
479+ const projection = resolveWhatsAppInboundMessageProjection ( source ) ;
480+ if ( projection . length === 0 ) {
455481 return false ;
456482 }
457- if ( extractText ( rawMessage ) ) {
483+ if ( extractText ( projection ) ) {
458484 return true ;
459485 }
460- if ( extractMediaKind ( rawMessage ) ) {
486+ if ( extractMediaKind ( projection ) ) {
461487 return true ;
462488 }
463- if ( extractLocationData ( rawMessage ) ) {
489+ if ( extractLocationData ( projection ) ) {
464490 return true ;
465491 }
466492 // Walk wrappers (ephemeral, viewOnce, etc.) — interactive responses
467493 // can arrive nested.
468- for ( const candidate of buildMessageChain ( rawMessage ) ) {
494+ for ( const candidate of projection ) {
469495 if ( hasInteractiveResponseContent ( candidate ) ) {
470496 return true ;
471497 }
0 commit comments