fix(whatsapp): normalize device-qualified contact ids
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import crypto from 'node:crypto'
|
||||
|
||||
export function canonicalWhatsAppJid(jid, identity = {}) {
|
||||
const raw = String(jid || '').trim().toLowerCase()
|
||||
const normalizeDevice = (value) => String(value || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/:\d+(?=@s\.whatsapp\.net$)/, '')
|
||||
const raw = normalizeDevice(jid)
|
||||
if (!raw || raw.endsWith('@g.us') || raw.endsWith('@newsletter')) return raw
|
||||
const canonical = String(identity?.canonicalJid || '').trim().toLowerCase()
|
||||
const canonical = normalizeDevice(identity?.canonicalJid)
|
||||
if (canonical.endsWith('@s.whatsapp.net')) return canonical
|
||||
const phone = String(identity?.phone || '').replace(/\D/g, '')
|
||||
const phone = String(identity?.phone || '').split(':')[0].replace(/\D/g, '')
|
||||
return phone ? `${phone}@s.whatsapp.net` : raw
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ test('WhatsApp LID resolves to the same canonical phone identity', () => {
|
||||
assert.equal(canonicalWhatsAppJid('987654321@lid', { phone: '+1 555 123 4567' }), phoneJid)
|
||||
assert.equal(canonicalWhatsAppJid('987654321@lid', { canonicalJid: phoneJid }), phoneJid)
|
||||
assert.equal(whatsappCustomerStableId(canonicalWhatsAppJid('987654321@lid', { phone: '+1 555 123 4567' })), whatsappCustomerStableId(phoneJid))
|
||||
assert.equal(canonicalWhatsAppJid('15551234567:4@s.whatsapp.net'), phoneJid)
|
||||
assert.equal(canonicalWhatsAppJid('987654321@lid', { canonicalJid: '15551234567:0@s.whatsapp.net' }), phoneJid)
|
||||
})
|
||||
|
||||
test('WhatsApp customer id remains stable after gateway reauthorization', () => {
|
||||
|
||||
@@ -805,6 +805,7 @@ async function connectAccount(accountId) {
|
||||
id: jidNormalizedUser(sock.user.id),
|
||||
name: sock.user.name || account.name || account.phoneNumber,
|
||||
} : account.user
|
||||
delete account.lastError
|
||||
delete account.qrDataUrl
|
||||
delete account.pairingCode
|
||||
const session = sessions.get(accountId)
|
||||
@@ -1595,6 +1596,7 @@ app.get('/accounts/:id/chats/:jid/identity', async (req, res) => {
|
||||
phoneJid = jid
|
||||
}
|
||||
}
|
||||
phoneJid = jidNormalizedUser(phoneJid)
|
||||
const contact = store.contacts.get(jid) || store.contacts.get(phoneJid) || {}
|
||||
const group = jid.endsWith('@g.us')
|
||||
? await sock.groupMetadata(jid).catch(() => store.groups.get(jid))
|
||||
|
||||
Reference in New Issue
Block a user