Skip to content

Commit 8f062fa

Browse files
committed
fix: optimize send message from group with mentions
1 parent 2565b93 commit 8f062fa

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/whatsapp/services/whatsapp.service.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,17 +1405,6 @@ export class WAStartupService {
14051405

14061406
const sender = isJidGroup(jid) ? jid : isWA.jid;
14071407

1408-
if (isJidGroup(sender)) {
1409-
try {
1410-
this.logger.verbose('Getting group metadata');
1411-
const metadata = await this.client.groupMetadata(sender);
1412-
1413-
console.log('metadata', metadata);
1414-
} catch (error) {
1415-
throw new NotFoundException('Group not found');
1416-
}
1417-
}
1418-
14191408
try {
14201409
if (options?.delay) {
14211410
this.logger.verbose('Delaying message');
@@ -1443,29 +1432,39 @@ export class WAStartupService {
14431432
}
14441433

14451434
let mentions: string[];
1435+
if (isJidGroup(sender)) {
1436+
try {
1437+
this.logger.verbose('Getting group metadata');
1438+
const groupMetadata = await this.client.groupMetadata(sender);
14461439

1447-
if (options?.mentions) {
1448-
this.logger.verbose('Mentions defined');
1449-
1450-
if (!Array.isArray(options.mentions.mentioned) && !options.mentions.everyOne) {
1451-
throw new BadRequestException('Mentions must be an array');
1452-
}
1440+
if (options?.mentions) {
1441+
this.logger.verbose('Mentions defined');
14531442

1454-
if (options.mentions.everyOne) {
1455-
this.logger.verbose('Mentions everyone');
1443+
if (
1444+
!Array.isArray(options.mentions.mentioned) &&
1445+
!options.mentions.everyOne
1446+
) {
1447+
throw new BadRequestException('Mentions must be an array');
1448+
}
14561449

1457-
const groupMetadata = await this.client.groupMetadata(sender);
1458-
mentions = groupMetadata.participants.map((participant) => participant.id);
1459-
this.logger.verbose('Getting group metadata for mentions');
1460-
} else {
1461-
this.logger.verbose('Mentions manually defined');
1462-
mentions = options.mentions.mentioned.map((mention) => {
1463-
const jid = this.createJid(mention);
1464-
if (isJidGroup(jid)) {
1465-
throw new BadRequestException('Mentions must be a number');
1450+
if (options.mentions.everyOne) {
1451+
this.logger.verbose('Mentions everyone');
1452+
1453+
mentions = groupMetadata.participants.map((participant) => participant.id);
1454+
this.logger.verbose('Getting group metadata for mentions');
1455+
} else {
1456+
this.logger.verbose('Mentions manually defined');
1457+
mentions = options.mentions.mentioned.map((mention) => {
1458+
const jid = this.createJid(mention);
1459+
if (isJidGroup(jid)) {
1460+
throw new BadRequestException('Mentions must be a number');
1461+
}
1462+
return jid;
1463+
});
14661464
}
1467-
return jid;
1468-
});
1465+
}
1466+
} catch (error) {
1467+
throw new NotFoundException('Group not found');
14691468
}
14701469
}
14711470

0 commit comments

Comments
 (0)