diff --git a/server/src/whatsapp-identity.js b/server/src/whatsapp-identity.js index 60fbc20..21f5935 100644 --- a/server/src/whatsapp-identity.js +++ b/server/src/whatsapp-identity.js @@ -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 } diff --git a/server/test/whatsapp-identity.test.js b/server/test/whatsapp-identity.test.js index d5352c6..fe56857 100644 --- a/server/test/whatsapp-identity.test.js +++ b/server/test/whatsapp-identity.test.js @@ -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', () => { diff --git a/whatsapp-gateway/src/index.js b/whatsapp-gateway/src/index.js index 408544c..93f926a 100644 --- a/whatsapp-gateway/src/index.js +++ b/whatsapp-gateway/src/index.js @@ -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))