From c939ed23372bdeccef8bbf1522c740efc991059e Mon Sep 17 00:00:00 2001 From: Aditya Nandwana Date: Thu, 13 Feb 2025 10:39:29 +0530 Subject: [PATCH] Enhance message editing validation in BaileysStartupService --- .../whatsapp/whatsapp.baileys.service.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 519b5c88..e19a77e8 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -3910,6 +3910,16 @@ export class BaileysStartupService extends ChannelStartupService { } try { + const oldMessage: any = await this.getMessage(data.key, true); + if (!oldMessage) throw new NotFoundException('Message not found'); + if (oldMessage?.key?.remoteJid !== jid) { + throw new BadRequestException('RemoteJid does not match'); + } + if (oldMessage?.messageTimestamp > Date.now() + 900000) { + // 15 minutes in milliseconds + throw new BadRequestException('Message is older than 15 minutes'); + } + const response = await this.client.sendMessage(jid, { ...(options as any), edit: data.key, @@ -3933,15 +3943,17 @@ export class BaileysStartupService extends ChannelStartupService { if ((message.key.valueOf() as any)?.deleted) { new BadRequestException('You cannot edit deleted messages'); } - - const updateMessage = this.prepareMessage({ ...response }); + if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') { + oldMessage.message.conversation = data.text; + } else { + oldMessage.message[oldMessage.messageType].caption = data.text; + } message = await this.prismaRepository.message.update({ where: { id: message.id }, data: { - message: { - ...updateMessage?.message?.[updateMessage.messageType]?.editedMessage, - }, + message: oldMessage.message, status: 'EDITED', + messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds }, }); const messageUpdate: any = {