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 { 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, { const response = await this.client.sendMessage(jid, {
...(options as any), ...(options as any),
edit: data.key, edit: data.key,
@ -3933,15 +3943,17 @@ export class BaileysStartupService extends ChannelStartupService {
if ((message.key.valueOf() as any)?.deleted) { if ((message.key.valueOf() as any)?.deleted) {
new BadRequestException('You cannot edit deleted messages'); new BadRequestException('You cannot edit deleted messages');
} }
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
const updateMessage = this.prepareMessage({ ...response }); oldMessage.message.conversation = data.text;
} else {
oldMessage.message[oldMessage.messageType].caption = data.text;
}
message = await this.prismaRepository.message.update({ message = await this.prismaRepository.message.update({
where: { id: message.id }, where: { id: message.id },
data: { data: {
message: { message: oldMessage.message,
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
},
status: 'EDITED', status: 'EDITED',
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
}, },
}); });
const messageUpdate: any = { const messageUpdate: any = {