feat(history): add userReceipt fallback for LID-PN mapping extraction
Add extractPnFromMessages() function to extract phone numbers from
userReceipt fields when pnJid is missing in LID conversations.
This is a cherry-pick of the functionality from upstream PR #2282
(commit f829b6d7a) integrated with our existing LID-PN extraction logic.
Closes: WhiskeySockets/Baileys#2282
This commit is contained in:
@@ -206,6 +206,36 @@ export function extractLidPnFromMessage(
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts phone number (PN) from message userReceipt fields.
|
||||||
|
*
|
||||||
|
* This is a fallback mechanism for LID chats that don't have a pnJid property.
|
||||||
|
* It looks at outgoing messages (fromMe: true) and extracts the recipient's
|
||||||
|
* phone number from the userReceipt.userJid field.
|
||||||
|
*
|
||||||
|
* @param messages - Array of history sync messages from a conversation
|
||||||
|
* @returns The extracted phone number JID, or undefined if not found
|
||||||
|
*
|
||||||
|
* @see https://github.com/WhiskeySockets/Baileys/pull/2282
|
||||||
|
*/
|
||||||
|
const extractPnFromMessages = (messages: proto.IHistorySyncMsg[]): string | undefined => {
|
||||||
|
for (const msgItem of messages) {
|
||||||
|
const message = msgItem.message
|
||||||
|
// Only extract from outgoing messages (fromMe: true) in 1:1 chats
|
||||||
|
// because userReceipt.userJid is the recipient's JID
|
||||||
|
if (!message?.key?.fromMe || !message.userReceipt?.length) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const userJid = message.userReceipt[0]?.userJid
|
||||||
|
if (userJid && (isPnUser(userJid) || isHostedPnUser(userJid))) {
|
||||||
|
return userJid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes a history sync message and extracts chats, contacts, messages,
|
* Processes a history sync message and extracts chats, contacts, messages,
|
||||||
* and LID-PN mappings.
|
* and LID-PN mappings.
|
||||||
@@ -276,6 +306,16 @@ export const processHistoryMessage = (item: proto.IHistorySync) => {
|
|||||||
)
|
)
|
||||||
if (conversationMapping) {
|
if (conversationMapping) {
|
||||||
addLidPnMapping(conversationMapping)
|
addLidPnMapping(conversationMapping)
|
||||||
|
} else if ((isLidUser(chatId) || isHostedLidUser(chatId)) && !chat.pnJid) {
|
||||||
|
// Source 2b: Fallback - extract PN from userReceipt in messages when pnJid is missing
|
||||||
|
// This handles edge cases where the conversation is LID but pnJid wasn't provided
|
||||||
|
const pnFromReceipt = extractPnFromMessages(chat.messages || [])
|
||||||
|
if (pnFromReceipt) {
|
||||||
|
addLidPnMapping({
|
||||||
|
lid: jidNormalizedUser(chatId),
|
||||||
|
pn: jidNormalizedUser(pnFromReceipt)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contacts.push({
|
contacts.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user