Skip to content

Commit 6a30377

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
Enhance message editing validation in BaileysStartupService
1 parent b8740ea commit 6a30377

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,16 @@ export class BaileysStartupService extends ChannelStartupService {
33413341
}
33423342

33433343
try {
3344+
const oldMessage: any = await this.getMessage(data.key, true);
3345+
if (!oldMessage) throw new NotFoundException('Message not found');
3346+
if (oldMessage?.key?.remoteJid !== jid) {
3347+
throw new BadRequestException('RemoteJid does not match');
3348+
}
3349+
if (oldMessage?.messageTimestamp > Date.now() + 900000) {
3350+
// 15 minutes in milliseconds
3351+
throw new BadRequestException('Message is older than 15 minutes');
3352+
}
3353+
33443354
const response = await this.client.sendMessage(jid, {
33453355
...(options as any),
33463356
edit: data.key,
@@ -3365,14 +3375,17 @@ export class BaileysStartupService extends ChannelStartupService {
33653375
new BadRequestException('You cannot edit deleted messages');
33663376
}
33673377

3368-
const updateMessage = this.prepareMessage({ ...response });
3378+
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
3379+
oldMessage.message.conversation = data.text;
3380+
} else {
3381+
oldMessage.message[oldMessage.messageType].caption = data.text;
3382+
}
33693383
message = await this.prismaRepository.message.update({
33703384
where: { id: message.id },
33713385
data: {
3372-
message: {
3373-
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
3374-
},
3386+
message: oldMessage.message,
33753387
status: 'EDITED',
3388+
messageTimestamp: Math.floor(Date.now() / 1000),
33763389
},
33773390
});
33783391
const messageUpdate: any = {

0 commit comments

Comments
 (0)