From e59098cf611cf8379f1ba033b4a8695434c79527 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Wed, 7 Feb 2024 08:44:54 -0300 Subject: [PATCH 1/2] Changed whatsapp number response to 200 Nothing is being created so the code 201 is not correct --- src/whatsapp/routers/chat.router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index 77285d1b..e4161474 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -60,7 +60,7 @@ export class ChatRouter extends RouterBroker { execute: (instance, data) => chatController.whatsappNumber(instance, data), }); - return res.status(HttpStatus.CREATED).json(response); + return res.status(HttpStatus.OK).json(response); }) .put(this.routerPath('markMessageAsRead'), ...guards, async (req, res) => { logger.verbose('request received in markMessageAsRead'); From 35cdce0d52ec3caf2318bca0dc949d30229acb8c Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Wed, 7 Feb 2024 08:53:47 -0300 Subject: [PATCH 2/2] Check multiple numbers only once in the database --- src/whatsapp/repository/contact.repository.ts | 55 +++++++++++++++++++ src/whatsapp/services/whatsapp.service.ts | 18 ++---- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/whatsapp/repository/contact.repository.ts b/src/whatsapp/repository/contact.repository.ts index 074b12e9..d26ada35 100644 --- a/src/whatsapp/repository/contact.repository.ts +++ b/src/whatsapp/repository/contact.repository.ts @@ -11,6 +11,11 @@ export class ContactQuery { where: ContactRaw; } +export class ContactQueryMany { + owner: ContactRaw['owner']; + ids: ContactRaw['id'][]; +} + export class ContactRepository extends Repository { constructor(private readonly contactModel: IContactModel, private readonly configService: ConfigService) { super(configService); @@ -169,4 +174,54 @@ export class ContactRepository extends Repository { return []; } } + + public async findManyById(query: ContactQueryMany): Promise { + 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 []; + } + } } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index b9a89aae..22f7e7b2 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -3312,6 +3312,10 @@ export class WAStartupService { onWhatsapp.push(...groups); // 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( ...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 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); @@ -3341,7 +3333,7 @@ export class WAStartupService { return { exists: !!numberVerified?.exists, jid: numberVerified?.jid || user.jid, - name: firstContactFound, + name: contacts.find((c) => c.id === jid)?.pushName, number: user.number, }; }),