From 66b82ac10a89c1a740a05332e2017d99c329b9ff Mon Sep 17 00:00:00 2001 From: Leandro Rocha Date: Wed, 31 Jan 2024 10:50:04 -0300 Subject: [PATCH 1/4] If contact stored with name, name added in return --- src/whatsapp/services/whatsapp.service.ts | 53 +++++++++++++++-------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 15cf7176c..baaba15d9 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -3130,7 +3130,7 @@ export class WAStartupService { const jids: { groups: { number: string; jid: string }[]; broadcast: { number: string; jid: string }[]; - users: { number: string; jid: string }[]; + users: { number: string; jid: string; contact?: string; name?: string }[]; } = { groups: [], broadcast: [], @@ -3145,7 +3145,7 @@ export class WAStartupService { } else if (jid === 'status@broadcast') { jids.broadcast.push({ number, jid }); } else { - jids.users.push({ number, jid }); + jids.users.push({ number, jid, contact: jid }); } }); @@ -3172,22 +3172,39 @@ export class WAStartupService { const verify = await this.client.onWhatsApp( ...jids.users.map(({ jid }) => (!jid.startsWith('+') ? `+${jid}` : jid)), ); - const users: OnWhatsAppDto[] = jids.users.map((user) => { - const MAX_SIMILARITY_THRESHOLD = 0.01; - const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28; - const jid = isBrWithDigit ? user.jid.slice(0, 4) + user.jid.slice(5) : user.jid; - - const numberVerified = verify.find((v) => { - const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length); - const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length); - return mainJidSimilarity <= MAX_SIMILARITY_THRESHOLD || jidSimilarity <= MAX_SIMILARITY_THRESHOLD; - }); - return { - exists: !!numberVerified?.exists, - jid: numberVerified?.jid || user.jid, - number: user.number, - }; - }); + const users: OnWhatsAppDto[] = await Promise.all( + jids.users.map(async (user) => { + const MAX_SIMILARITY_THRESHOLD = 0.01; + const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28; + const jid = isBrWithDigit ? user.jid.slice(0, 4) + user.jid.slice(5) : user.jid; + + const query: ContactQuery = { + where: { + owner: this.instance.name, + id: user.contact, + }, + }; + const contacts: ContactRaw[] = await this.repository.contact.find(query); + let firstContactFound; + if (contacts.length > 0) { + firstContactFound = contacts[0].pushName; + console.log(contacts[0]); + } + + const numberVerified = verify.find((v) => { + const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length); + const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length); + return mainJidSimilarity <= MAX_SIMILARITY_THRESHOLD || jidSimilarity <= MAX_SIMILARITY_THRESHOLD; + }); + return { + exists: !!numberVerified?.exists, + jid: numberVerified?.jid || user.jid, + name: firstContactFound, + number: user.number, + }; + }), + ); + onWhatsapp.push(...users); return onWhatsapp; From 3da73b821d2f71bdfad932000943d19fbed94f06 Mon Sep 17 00:00:00 2001 From: Leandro Santos Rocha Date: Wed, 31 Jan 2024 10:55:37 -0300 Subject: [PATCH 2/4] Removed console.log --- src/whatsapp/services/whatsapp.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index baaba15d9..77d5570df 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -3188,7 +3188,6 @@ export class WAStartupService { let firstContactFound; if (contacts.length > 0) { firstContactFound = contacts[0].pushName; - console.log(contacts[0]); } const numberVerified = verify.find((v) => { From dfb1ee0c568aa12511c8ec818fdb7f82ed4007f3 Mon Sep 17 00:00:00 2001 From: Leandro Santos Rocha Date: Wed, 31 Jan 2024 11:37:19 -0300 Subject: [PATCH 3/4] Adjust to use the same jid --- src/whatsapp/services/whatsapp.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 77d5570df..4858e4334 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -3130,7 +3130,7 @@ export class WAStartupService { const jids: { groups: { number: string; jid: string }[]; broadcast: { number: string; jid: string }[]; - users: { number: string; jid: string; contact?: string; name?: string }[]; + users: { number: string; jid: string; name?: string }[]; } = { groups: [], broadcast: [], @@ -3145,7 +3145,7 @@ export class WAStartupService { } else if (jid === 'status@broadcast') { jids.broadcast.push({ number, jid }); } else { - jids.users.push({ number, jid, contact: jid }); + jids.users.push({ number, jid }); } }); @@ -3181,7 +3181,7 @@ export class WAStartupService { const query: ContactQuery = { where: { owner: this.instance.name, - id: user.contact, + id: user.jid.startsWith('+') ? user.jid.substring(1) : user.jid;, }, }; const contacts: ContactRaw[] = await this.repository.contact.find(query); From 7439d2401de1a75cfbf5ea94376974bbe9e853cd Mon Sep 17 00:00:00 2001 From: Leandro Santos Rocha Date: Wed, 31 Jan 2024 11:39:04 -0300 Subject: [PATCH 4/4] Fix error ; --- src/whatsapp/services/whatsapp.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 4858e4334..71ca431eb 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -3181,7 +3181,7 @@ export class WAStartupService { const query: ContactQuery = { where: { owner: this.instance.name, - id: user.jid.startsWith('+') ? user.jid.substring(1) : user.jid;, + id: user.jid.startsWith('+') ? user.jid.substring(1) : user.jid, }, }; const contacts: ContactRaw[] = await this.repository.contact.find(query);