Merge pull request #1798 from apresentame/fix/webhook_event

Permitir correta utilização da evolution quando mensagens não são persistidas no DB
This commit is contained in:
Davidson Gomes 2025-08-04 18:52:58 -03:00 committed by GitHub
commit 03a44cf9b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1446,16 +1446,7 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
const findMessage = await this.prismaRepository.message.findFirst({
where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } },
});
if (!findMessage) {
continue;
}
const message: any = { const message: any = {
messageId: findMessage.id,
keyId: key.id, keyId: key.id,
remoteJid: key?.remoteJid, remoteJid: key?.remoteJid,
fromMe: key.fromMe, fromMe: key.fromMe,
@ -1465,6 +1456,16 @@ export class BaileysStartupService extends ChannelStartupService {
instanceId: this.instanceId, instanceId: this.instanceId,
}; };
let findMessage: any;
const configDatabaseData = this.configService.get<Database>('DATABASE').SAVE_DATA;
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
findMessage = await this.prismaRepository.message.findFirst({
where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } },
});
if (findMessage) message.messageId = findMessage.id;
}
if (update.message === null && update.status === undefined) { if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key); this.sendDataWebhook(Events.MESSAGES_DELETE, key);
@ -1480,7 +1481,9 @@ export class BaileysStartupService extends ChannelStartupService {
} }
continue; continue;
} else if (update.status !== undefined && status[update.status] !== findMessage.status) { }
if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) {
if (!key.fromMe && key.remoteJid) { if (!key.fromMe && key.remoteJid) {
readChatToUpdate[key.remoteJid] = true; readChatToUpdate[key.remoteJid] = true;
@ -3438,17 +3441,20 @@ export class BaileysStartupService extends ChannelStartupService {
where: { id: message.id }, where: { id: message.id },
data: { key: { ...existingKey, deleted: true }, status: 'DELETED' }, data: { key: { ...existingKey, deleted: true }, status: 'DELETED' },
}); });
const messageUpdate: any = { if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
messageId: message.id, const messageUpdate: any = {
keyId: messageId, messageId: message.id,
remoteJid: response.key.remoteJid, keyId: messageId,
fromMe: response.key.fromMe, remoteJid: response.key.remoteJid,
participant: response.key?.remoteJid, fromMe: response.key.fromMe,
status: 'DELETED', participant: response.key?.remoteJid,
instanceId: this.instanceId, status: 'DELETED',
}; instanceId: this.instanceId,
await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); };
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
}
} else { } else {
if (!message) return response;
await this.prismaRepository.message.deleteMany({ where: { id: message.id } }); await this.prismaRepository.message.deleteMany({ where: { id: message.id } });
} }
this.sendDataWebhook(Events.MESSAGES_DELETE, { this.sendDataWebhook(Events.MESSAGES_DELETE, {
@ -3780,6 +3786,10 @@ export class BaileysStartupService extends ChannelStartupService {
private async formatUpdateMessage(data: UpdateMessageDto) { private async formatUpdateMessage(data: UpdateMessageDto) {
try { try {
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
return data;
}
const msg: any = await this.getMessage(data.key, true); const msg: any = await this.getMessage(data.key, true);
if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') { if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') {
@ -3813,13 +3823,15 @@ export class BaileysStartupService extends ChannelStartupService {
try { try {
const oldMessage: any = await this.getMessage(data.key, true); const oldMessage: any = await this.getMessage(data.key, true);
if (!oldMessage) throw new NotFoundException('Message not found'); if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
if (oldMessage?.key?.remoteJid !== jid) { if (!oldMessage) throw new NotFoundException('Message not found');
throw new BadRequestException('RemoteJid does not match'); if (oldMessage?.key?.remoteJid !== jid) {
} throw new BadRequestException('RemoteJid does not match');
if (oldMessage?.messageTimestamp > Date.now() + 900000) { }
// 15 minutes in milliseconds if (oldMessage?.messageTimestamp > Date.now() + 900000) {
throw new BadRequestException('Message is older than 15 minutes'); // 15 minutes in milliseconds
throw new BadRequestException('Message is older than 15 minutes');
}
} }
const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key }); const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key });
@ -3837,7 +3849,7 @@ export class BaileysStartupService extends ChannelStartupService {
); );
const messageId = messageSent.message?.protocolMessage?.key?.id; const messageId = messageSent.message?.protocolMessage?.key?.id;
if (messageId) { if (messageId && this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
let message = await this.prismaRepository.message.findFirst({ let message = await this.prismaRepository.message.findFirst({
where: { key: { path: ['id'], equals: messageId } }, where: { key: { path: ['id'], equals: messageId } },
}); });
@ -3849,6 +3861,7 @@ 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') { if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
oldMessage.message.conversation = data.text; oldMessage.message.conversation = data.text;
} else { } else {
@ -3862,16 +3875,19 @@ export class BaileysStartupService extends ChannelStartupService {
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
}, },
}); });
const messageUpdate: any = {
messageId: message.id, if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
keyId: messageId, const messageUpdate: any = {
remoteJid: messageSent.key.remoteJid, messageId: message.id,
fromMe: messageSent.key.fromMe, keyId: messageId,
participant: messageSent.key?.remoteJid, remoteJid: messageSent.key.remoteJid,
status: 'EDITED', fromMe: messageSent.key.fromMe,
instanceId: this.instanceId, participant: messageSent.key?.remoteJid,
}; status: 'EDITED',
await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); instanceId: this.instanceId,
};
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
}
} }
} }
} }