Skip to content

Commit 35cdce0

Browse files
committed
Check multiple numbers only once in the database
1 parent e59098c commit 35cdce0

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/whatsapp/repository/contact.repository.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export class ContactQuery {
1111
where: ContactRaw;
1212
}
1313

14+
export class ContactQueryMany {
15+
owner: ContactRaw['owner'];
16+
ids: ContactRaw['id'][];
17+
}
18+
1419
export class ContactRepository extends Repository {
1520
constructor(private readonly contactModel: IContactModel, private readonly configService: ConfigService) {
1621
super(configService);
@@ -169,4 +174,54 @@ export class ContactRepository extends Repository {
169174
return [];
170175
}
171176
}
177+
178+
public async findManyById(query: ContactQueryMany): Promise<ContactRaw[]> {
179+
try {
180+
this.logger.verbose('finding contacts');
181+
if (this.dbSettings.ENABLED) {
182+
this.logger.verbose('finding contacts in db');
183+
return await this.contactModel.find({
184+
owner: query.owner,
185+
id: { $in: query.ids },
186+
});
187+
}
188+
189+
this.logger.verbose('finding contacts in store');
190+
const contacts: ContactRaw[] = [];
191+
if (query.ids.length > 0) {
192+
this.logger.verbose('finding contacts in store by id');
193+
query.ids.forEach((id) => {
194+
contacts.push(
195+
JSON.parse(
196+
readFileSync(join(this.storePath, 'contacts', query.owner, id + '.json'), {
197+
encoding: 'utf-8',
198+
}),
199+
),
200+
);
201+
});
202+
} else {
203+
this.logger.verbose('finding contacts in store by owner');
204+
205+
const openDir = opendirSync(join(this.storePath, 'contacts', query.owner), {
206+
encoding: 'utf-8',
207+
});
208+
for await (const dirent of openDir) {
209+
if (dirent.isFile()) {
210+
contacts.push(
211+
JSON.parse(
212+
readFileSync(join(this.storePath, 'contacts', query.owner, dirent.name), {
213+
encoding: 'utf-8',
214+
}),
215+
),
216+
);
217+
}
218+
}
219+
}
220+
221+
this.logger.verbose('contacts found in store: ' + contacts.length + ' contacts');
222+
return contacts;
223+
} catch (error) {
224+
return [];
225+
}
226+
}
172227
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,6 +3312,10 @@ export class WAStartupService {
33123312
onWhatsapp.push(...groups);
33133313

33143314
// USERS
3315+
const contacts: ContactRaw[] = await this.repository.contact.findManyById({
3316+
owner: this.instance.name,
3317+
ids: jids.users.map(({ jid }) => (jid.startsWith('+') ? jid.substring(1) : jid)),
3318+
});
33153319
const verify = await this.client.onWhatsApp(
33163320
...jids.users.map(({ jid }) => (!jid.startsWith('+') ? `+${jid}` : jid)),
33173321
);
@@ -3321,18 +3325,6 @@ export class WAStartupService {
33213325
const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28;
33223326
const jid = isBrWithDigit ? user.jid.slice(0, 4) + user.jid.slice(5) : user.jid;
33233327

3324-
const query: ContactQuery = {
3325-
where: {
3326-
owner: this.instance.name,
3327-
id: user.jid.startsWith('+') ? user.jid.substring(1) : user.jid,
3328-
},
3329-
};
3330-
const contacts: ContactRaw[] = await this.repository.contact.find(query);
3331-
let firstContactFound;
3332-
if (contacts.length > 0) {
3333-
firstContactFound = contacts[0].pushName;
3334-
}
3335-
33363328
const numberVerified = verify.find((v) => {
33373329
const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length);
33383330
const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length);
@@ -3341,7 +3333,7 @@ export class WAStartupService {
33413333
return {
33423334
exists: !!numberVerified?.exists,
33433335
jid: numberVerified?.jid || user.jid,
3344-
name: firstContactFound,
3336+
name: contacts.find((c) => c.id === jid)?.pushName,
33453337
number: user.number,
33463338
};
33473339
}),

0 commit comments

Comments
 (0)