Enhance message editing validation in BaileysStartupService

This commit is contained in:
Aditya Nandwana 2025-02-13 10:39:29 +05:30
parent 91e7a32209
commit c939ed2337

View File

@ -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 = {