mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-19 09:53:36 -06:00
Check multiple numbers only once in the database
This commit is contained in:
parent
e59098cf61
commit
35cdce0d52
@ -11,6 +11,11 @@ export class ContactQuery {
|
|||||||
where: ContactRaw;
|
where: ContactRaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ContactQueryMany {
|
||||||
|
owner: ContactRaw['owner'];
|
||||||
|
ids: ContactRaw['id'][];
|
||||||
|
}
|
||||||
|
|
||||||
export class ContactRepository extends Repository {
|
export class ContactRepository extends Repository {
|
||||||
constructor(private readonly contactModel: IContactModel, private readonly configService: ConfigService) {
|
constructor(private readonly contactModel: IContactModel, private readonly configService: ConfigService) {
|
||||||
super(configService);
|
super(configService);
|
||||||
@ -169,4 +174,54 @@ export class ContactRepository extends Repository {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async findManyById(query: ContactQueryMany): Promise<ContactRaw[]> {
|
||||||
|
try {
|
||||||
|
this.logger.verbose('finding contacts');
|
||||||
|
if (this.dbSettings.ENABLED) {
|
||||||
|
this.logger.verbose('finding contacts in db');
|
||||||
|
return await this.contactModel.find({
|
||||||
|
owner: query.owner,
|
||||||
|
id: { $in: query.ids },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('finding contacts in store');
|
||||||
|
const contacts: ContactRaw[] = [];
|
||||||
|
if (query.ids.length > 0) {
|
||||||
|
this.logger.verbose('finding contacts in store by id');
|
||||||
|
query.ids.forEach((id) => {
|
||||||
|
contacts.push(
|
||||||
|
JSON.parse(
|
||||||
|
readFileSync(join(this.storePath, 'contacts', query.owner, id + '.json'), {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.logger.verbose('finding contacts in store by owner');
|
||||||
|
|
||||||
|
const openDir = opendirSync(join(this.storePath, 'contacts', query.owner), {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
});
|
||||||
|
for await (const dirent of openDir) {
|
||||||
|
if (dirent.isFile()) {
|
||||||
|
contacts.push(
|
||||||
|
JSON.parse(
|
||||||
|
readFileSync(join(this.storePath, 'contacts', query.owner, dirent.name), {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('contacts found in store: ' + contacts.length + ' contacts');
|
||||||
|
return contacts;
|
||||||
|
} catch (error) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3312,6 +3312,10 @@ export class WAStartupService {
|
|||||||
onWhatsapp.push(...groups);
|
onWhatsapp.push(...groups);
|
||||||
|
|
||||||
// USERS
|
// USERS
|
||||||
|
const contacts: ContactRaw[] = await this.repository.contact.findManyById({
|
||||||
|
owner: this.instance.name,
|
||||||
|
ids: jids.users.map(({ jid }) => (jid.startsWith('+') ? jid.substring(1) : jid)),
|
||||||
|
});
|
||||||
const verify = await this.client.onWhatsApp(
|
const verify = await this.client.onWhatsApp(
|
||||||
...jids.users.map(({ jid }) => (!jid.startsWith('+') ? `+${jid}` : jid)),
|
...jids.users.map(({ jid }) => (!jid.startsWith('+') ? `+${jid}` : jid)),
|
||||||
);
|
);
|
||||||
@ -3321,18 +3325,6 @@ export class WAStartupService {
|
|||||||
const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28;
|
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 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 numberVerified = verify.find((v) => {
|
||||||
const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length);
|
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);
|
const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length);
|
||||||
@ -3341,7 +3333,7 @@ export class WAStartupService {
|
|||||||
return {
|
return {
|
||||||
exists: !!numberVerified?.exists,
|
exists: !!numberVerified?.exists,
|
||||||
jid: numberVerified?.jid || user.jid,
|
jid: numberVerified?.jid || user.jid,
|
||||||
name: firstContactFound,
|
name: contacts.find((c) => c.id === jid)?.pushName,
|
||||||
number: user.number,
|
number: user.number,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user