From e851696430806654a547b2590b7684b8ad1f8ba0 Mon Sep 17 00:00:00 2001 From: Alan Mosko Date: Fri, 21 Jul 2023 15:32:28 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Adi=C3=A7=C3=A3o=20de=20Profile=20Route?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validate/validate.schema.ts | 11 ++++++++ src/whatsapp/controllers/chat.controller.ts | 5 ++++ src/whatsapp/routers/chat.router.ts | 18 +++++++++++++ src/whatsapp/services/whatsapp.service.ts | 30 +++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 2b9ce19a..2533e4d7 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -587,6 +587,17 @@ export const profilePictureSchema: JSONSchema7 = { }, }; +export const profileSchema: JSONSchema7 = { + type: 'object', + properties: { + wuid: { type: 'string' }, + name: { type: 'string' }, + picture: { type: 'string' }, + status: { type: 'string' }, + isBusiness: { type: 'boolean' }, + }, +}; + export const messageValidateSchema: JSONSchema7 = { $id: v4(), type: 'object', diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index 0a176059..63c72092 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -47,6 +47,11 @@ export class ChatController { logger.verbose('requested fetchProfilePicture from ' + instanceName + ' instance'); return await this.waMonitor.waInstances[instanceName].profilePicture(data.number); } + + public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) { + logger.verbose('requested fetchProfile from ' + instanceName + ' instance'); + return await this.waMonitor.waInstances[instanceName].profile(instanceName, data.number); + } public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) { logger.verbose('requested fetchContacts from ' + instanceName + ' instance'); diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index 50ead521..ac7990c8 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -8,6 +8,7 @@ import { privacySettingsSchema, profileNameSchema, profilePictureSchema, + profileSchema, profileStatusSchema, readMessageSchema, whatsappNumberSchema, @@ -129,6 +130,23 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('fetchProfile'), ...guards, async (req, res) => { + logger.verbose('request received in fetchProfile'); + logger.verbose('request body: '); + logger.verbose(req.body); + + logger.verbose('request query: '); + logger.verbose(req.query); + + const response = await this.dataValidate({ + request: req, + schema: profileSchema, + ClassRef: NumberDto, + execute: (instance, data) => chatController.fetchProfile(instance, data), + }); + + return res.status(HttpStatus.OK).json(response); + }) .post(this.routerPath('findContacts'), ...guards, async (req, res) => { logger.verbose('request received in findContacts'); logger.verbose('request body: '); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 65cb40bb..b2ab7127 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1436,6 +1436,36 @@ export class WAStartupService { }; } } + + public async profile(instanceName: string) { + const jid = this.client?.user?.id; + + this.logger.verbose('Getting profile with jid: ' + jid); + try { + this.logger.verbose('Getting profile info'); + const info = await waMonitor.instanceInfo(instanceName); + const business = await this.fetchBusinessProfile(jid); + + return { + wuid: jid, + name: info?.instance?.profileName, + picture: info?.instance?.profilePictureUrl, + status: info?.instance?.profileStatus, + os: this.client?.authState?.creds?.platform, + isBusiness: typeof business !== 'undefined', + }; + } catch (error) { + this.logger.verbose('Profile not found'); + return { + wuid: jid, + name: null, + picture: null, + status: null, + os: null, + isBusiness: false, + }; + } + } private async sendMessageWithTyping( number: string, From 1ec3ed32eeaa8cd668e0b28d2e5aa81fc2d4ff6a Mon Sep 17 00:00:00 2001 From: Alan Mosko Date: Sun, 23 Jul 2023 10:18:00 -0300 Subject: [PATCH 2/3] Melhorias --- src/whatsapp/controllers/chat.controller.ts | 2 +- src/whatsapp/dto/chat.dto.ts | 13 +++ src/whatsapp/routers/chat.router.ts | 2 +- src/whatsapp/services/whatsapp.service.ts | 92 +++++++++++++++------ 4 files changed, 84 insertions(+), 25 deletions(-) diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index 63c72092..2b500ad1 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -50,7 +50,7 @@ export class ChatController { public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) { logger.verbose('requested fetchProfile from ' + instanceName + ' instance'); - return await this.waMonitor.waInstances[instanceName].profile(instanceName, data.number); + return await this.waMonitor.waInstances[instanceName].fetchProfile(instanceName, data.number); } public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) { diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index 07757194..5af66a5e 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -26,6 +26,19 @@ export class NumberDto { number: string; } +export class NumberBusiness { + wid?: string; + jid?: string; + exists?: boolean; + isBusiness: boolean; + name?: string; + message?: string; + description?: string; + email?: string; + website?: string[]; + address?: string; +} + export class ProfileNameDto { name: string; } diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index ac7990c8..68ecd97e 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -130,7 +130,7 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) - .get(this.routerPath('fetchProfile'), ...guards, async (req, res) => { + .post(this.routerPath('fetchProfile'), ...guards, async (req, res) => { logger.verbose('request received in fetchProfile'); logger.verbose('request body: '); logger.verbose(req.body); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index b2ab7127..e99cee84 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -85,6 +85,7 @@ import { ArchiveChatDto, DeleteMessage, OnWhatsAppDto, + NumberBusiness, PrivacySettingDto, ReadMessageDto, WhatsAppNumberDto, @@ -1437,23 +1438,66 @@ export class WAStartupService { } } - public async profile(instanceName: string) { - const jid = this.client?.user?.id; - + public async getStatus(number: string) { + const jid = this.createJid(number); + + this.logger.verbose('Getting profile status with jid:' + jid); + try { + this.logger.verbose('Getting status'); + return { + wuid: jid, + status: (await this.client.fetchStatus(jid))?.status, + }; + } catch (error) { + this.logger.verbose('Status not found'); + return { + wuid: jid, + status: null, + }; + } + } + + public async fetchProfile(instanceName: string, number?: string) { + const jid = (number) + ? this.createJid(number) + : this.client?.user?.id; this.logger.verbose('Getting profile with jid: ' + jid); try { this.logger.verbose('Getting profile info'); - const info = await waMonitor.instanceInfo(instanceName); const business = await this.fetchBusinessProfile(jid); - return { - wuid: jid, - name: info?.instance?.profileName, - picture: info?.instance?.profilePictureUrl, - status: info?.instance?.profileStatus, - os: this.client?.authState?.creds?.platform, - isBusiness: typeof business !== 'undefined', - }; + if (number) { + const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift(); + const picture = await this.profilePicture(jid); + const status = await this.getStatus(jid); + + return { + wuid: jid, + name: info?.name, + numberExists: info?.exists, + picture: picture?.profilePictureUrl, + status: status?.status, + isBusiness: business.isBusiness, + email: business?.email, + description: business?.description, + website: business?.website?.shift(), + }; + } else { + const info = await waMonitor.instanceInfo(instanceName); + + return { + wuid: jid, + name: info?.instance?.profileName, + numberExists: true, + picture: info?.instance?.profilePictureUrl, + status: info?.instance?.profileStatus, + isBusiness: business.isBusiness, + email: business?.email, + description: business?.description, + website: business?.website?.shift(), + }; + } + } catch (error) { this.logger.verbose('Profile not found'); return { @@ -2463,29 +2507,31 @@ export class WAStartupService { } } - public async fetchBusinessProfile(number: string) { + public async fetchBusinessProfile(number: string) : Promise { this.logger.verbose('Fetching business profile'); try { - let jid; - - if (!number) { - jid = this.instance.wuid; - } else { - jid = this.createJid(number); - } + const jid = (number) + ? this.createJid(number) + : this.instance.wuid; const profile = await this.client.getBusinessProfile(jid); this.logger.verbose('Trying to get business profile'); if (!profile) { + const info = await this.whatsappNumber({ numbers: [jid] }); + return { - exists: false, - message: 'Business profile not found', + isBusiness: false, + message: 'Not is business profile', + ...info?.shift() }; } this.logger.verbose('Business profile fetched'); - return profile; + return { + isBusiness: true, + ...profile + }; } catch (error) { throw new InternalServerErrorException( 'Error updating profile name', From 76d77ad76f54283fe47aa0376b89897055f73220 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sun, 23 Jul 2023 22:05:21 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Revert=20"Adi=C3=A7=C3=A3o=20de=20FetchProf?= =?UTF-8?q?ile"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validate/validate.schema.ts | 11 --- src/whatsapp/controllers/chat.controller.ts | 5 -- src/whatsapp/dto/chat.dto.ts | 13 --- src/whatsapp/routers/chat.router.ts | 18 ---- src/whatsapp/services/whatsapp.service.ts | 98 +++------------------ 5 files changed, 11 insertions(+), 134 deletions(-) diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 2533e4d7..2b9ce19a 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -587,17 +587,6 @@ export const profilePictureSchema: JSONSchema7 = { }, }; -export const profileSchema: JSONSchema7 = { - type: 'object', - properties: { - wuid: { type: 'string' }, - name: { type: 'string' }, - picture: { type: 'string' }, - status: { type: 'string' }, - isBusiness: { type: 'boolean' }, - }, -}; - export const messageValidateSchema: JSONSchema7 = { $id: v4(), type: 'object', diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index 2b500ad1..0a176059 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -47,11 +47,6 @@ export class ChatController { logger.verbose('requested fetchProfilePicture from ' + instanceName + ' instance'); return await this.waMonitor.waInstances[instanceName].profilePicture(data.number); } - - public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) { - logger.verbose('requested fetchProfile from ' + instanceName + ' instance'); - return await this.waMonitor.waInstances[instanceName].fetchProfile(instanceName, data.number); - } public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) { logger.verbose('requested fetchContacts from ' + instanceName + ' instance'); diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index 5af66a5e..07757194 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -26,19 +26,6 @@ export class NumberDto { number: string; } -export class NumberBusiness { - wid?: string; - jid?: string; - exists?: boolean; - isBusiness: boolean; - name?: string; - message?: string; - description?: string; - email?: string; - website?: string[]; - address?: string; -} - export class ProfileNameDto { name: string; } diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index 68ecd97e..50ead521 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -8,7 +8,6 @@ import { privacySettingsSchema, profileNameSchema, profilePictureSchema, - profileSchema, profileStatusSchema, readMessageSchema, whatsappNumberSchema, @@ -130,23 +129,6 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) - .post(this.routerPath('fetchProfile'), ...guards, async (req, res) => { - logger.verbose('request received in fetchProfile'); - logger.verbose('request body: '); - logger.verbose(req.body); - - logger.verbose('request query: '); - logger.verbose(req.query); - - const response = await this.dataValidate({ - request: req, - schema: profileSchema, - ClassRef: NumberDto, - execute: (instance, data) => chatController.fetchProfile(instance, data), - }); - - return res.status(HttpStatus.OK).json(response); - }) .post(this.routerPath('findContacts'), ...guards, async (req, res) => { logger.verbose('request received in findContacts'); logger.verbose('request body: '); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 1cd9035a..3851f067 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -85,7 +85,6 @@ import { ArchiveChatDto, DeleteMessage, OnWhatsAppDto, - NumberBusiness, PrivacySettingDto, ReadMessageDto, WhatsAppNumberDto, @@ -1444,79 +1443,6 @@ export class WAStartupService { }; } } - - public async getStatus(number: string) { - const jid = this.createJid(number); - - this.logger.verbose('Getting profile status with jid:' + jid); - try { - this.logger.verbose('Getting status'); - return { - wuid: jid, - status: (await this.client.fetchStatus(jid))?.status, - }; - } catch (error) { - this.logger.verbose('Status not found'); - return { - wuid: jid, - status: null, - }; - } - } - - public async fetchProfile(instanceName: string, number?: string) { - const jid = (number) - ? this.createJid(number) - : this.client?.user?.id; - this.logger.verbose('Getting profile with jid: ' + jid); - try { - this.logger.verbose('Getting profile info'); - const business = await this.fetchBusinessProfile(jid); - - if (number) { - const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift(); - const picture = await this.profilePicture(jid); - const status = await this.getStatus(jid); - - return { - wuid: jid, - name: info?.name, - numberExists: info?.exists, - picture: picture?.profilePictureUrl, - status: status?.status, - isBusiness: business.isBusiness, - email: business?.email, - description: business?.description, - website: business?.website?.shift(), - }; - } else { - const info = await waMonitor.instanceInfo(instanceName); - - return { - wuid: jid, - name: info?.instance?.profileName, - numberExists: true, - picture: info?.instance?.profilePictureUrl, - status: info?.instance?.profileStatus, - isBusiness: business.isBusiness, - email: business?.email, - description: business?.description, - website: business?.website?.shift(), - }; - } - - } catch (error) { - this.logger.verbose('Profile not found'); - return { - wuid: jid, - name: null, - picture: null, - status: null, - os: null, - isBusiness: false, - }; - } - } private async sendMessageWithTyping( number: string, @@ -2534,31 +2460,29 @@ export class WAStartupService { } } - public async fetchBusinessProfile(number: string) : Promise { + public async fetchBusinessProfile(number: string) { this.logger.verbose('Fetching business profile'); try { - const jid = (number) - ? this.createJid(number) - : this.instance.wuid; + let jid; + + if (!number) { + jid = this.instance.wuid; + } else { + jid = this.createJid(number); + } const profile = await this.client.getBusinessProfile(jid); this.logger.verbose('Trying to get business profile'); if (!profile) { - const info = await this.whatsappNumber({ numbers: [jid] }); - return { - isBusiness: false, - message: 'Not is business profile', - ...info?.shift() + exists: false, + message: 'Business profile not found', }; } this.logger.verbose('Business profile fetched'); - return { - isBusiness: true, - ...profile - }; + return profile; } catch (error) { throw new InternalServerErrorException( 'Error updating profile name',