Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: If contact stored with name, name added in return #390

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; name?: string }[];
} = {
groups: [],
broadcast: [],
Expand Down Expand Up @@ -3172,22 +3172,38 @@ 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.jid.startsWith('+') ? user.jid.substring(1) : user.jid,
},
};
const contacts: ContactRaw[] = await this.repository.contact.find(query);
let firstContactFound;
if (contacts.length > 0) {
firstContactFound = contacts[0].pushName;
}

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;
Expand Down