Skip to content

Commit e851696

Browse files
committed
Adição de Profile Route
1 parent 3e3a175 commit e851696

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/validate/validate.schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,17 @@ export const profilePictureSchema: JSONSchema7 = {
587587
},
588588
};
589589

590+
export const profileSchema: JSONSchema7 = {
591+
type: 'object',
592+
properties: {
593+
wuid: { type: 'string' },
594+
name: { type: 'string' },
595+
picture: { type: 'string' },
596+
status: { type: 'string' },
597+
isBusiness: { type: 'boolean' },
598+
},
599+
};
600+
590601
export const messageValidateSchema: JSONSchema7 = {
591602
$id: v4(),
592603
type: 'object',

src/whatsapp/controllers/chat.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export class ChatController {
4747
logger.verbose('requested fetchProfilePicture from ' + instanceName + ' instance');
4848
return await this.waMonitor.waInstances[instanceName].profilePicture(data.number);
4949
}
50+
51+
public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) {
52+
logger.verbose('requested fetchProfile from ' + instanceName + ' instance');
53+
return await this.waMonitor.waInstances[instanceName].profile(instanceName, data.number);
54+
}
5055

5156
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {
5257
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');

src/whatsapp/routers/chat.router.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
privacySettingsSchema,
99
profileNameSchema,
1010
profilePictureSchema,
11+
profileSchema,
1112
profileStatusSchema,
1213
readMessageSchema,
1314
whatsappNumberSchema,
@@ -129,6 +130,23 @@ export class ChatRouter extends RouterBroker {
129130

130131
return res.status(HttpStatus.OK).json(response);
131132
})
133+
.get(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
134+
logger.verbose('request received in fetchProfile');
135+
logger.verbose('request body: ');
136+
logger.verbose(req.body);
137+
138+
logger.verbose('request query: ');
139+
logger.verbose(req.query);
140+
141+
const response = await this.dataValidate<NumberDto>({
142+
request: req,
143+
schema: profileSchema,
144+
ClassRef: NumberDto,
145+
execute: (instance, data) => chatController.fetchProfile(instance, data),
146+
});
147+
148+
return res.status(HttpStatus.OK).json(response);
149+
})
132150
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
133151
logger.verbose('request received in findContacts');
134152
logger.verbose('request body: ');

src/whatsapp/services/whatsapp.service.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,36 @@ export class WAStartupService {
14361436
};
14371437
}
14381438
}
1439+
1440+
public async profile(instanceName: string) {
1441+
const jid = this.client?.user?.id;
1442+
1443+
this.logger.verbose('Getting profile with jid: ' + jid);
1444+
try {
1445+
this.logger.verbose('Getting profile info');
1446+
const info = await waMonitor.instanceInfo(instanceName);
1447+
const business = await this.fetchBusinessProfile(jid);
1448+
1449+
return {
1450+
wuid: jid,
1451+
name: info?.instance?.profileName,
1452+
picture: info?.instance?.profilePictureUrl,
1453+
status: info?.instance?.profileStatus,
1454+
os: this.client?.authState?.creds?.platform,
1455+
isBusiness: typeof business !== 'undefined',
1456+
};
1457+
} catch (error) {
1458+
this.logger.verbose('Profile not found');
1459+
return {
1460+
wuid: jid,
1461+
name: null,
1462+
picture: null,
1463+
status: null,
1464+
os: null,
1465+
isBusiness: false,
1466+
};
1467+
}
1468+
}
14391469

14401470
private async sendMessageWithTyping<T = proto.IMessage>(
14411471
number: string,

0 commit comments

Comments
 (0)