mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-08-29 02:36:11 -06:00
fix: corrigido disparo de eventos quando nao usa a opcao da ENV de salvar mensagens no banco
This commit is contained in:
parent
cf95c027eb
commit
6101c8d651
@ -1443,16 +1443,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 = {
|
||||
messageId: findMessage.id,
|
||||
keyId: key.id,
|
||||
remoteJid: key?.remoteJid,
|
||||
fromMe: key.fromMe,
|
||||
@ -1462,6 +1453,16 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
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) {
|
||||
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
|
||||
|
||||
@ -1477,7 +1478,9 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
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) {
|
||||
readChatToUpdate[key.remoteJid] = true;
|
||||
|
||||
@ -3431,6 +3434,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
where: { id: message.id },
|
||||
data: { key: { ...existingKey, deleted: true }, status: 'DELETED' },
|
||||
});
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
|
||||
const messageUpdate: any = {
|
||||
messageId: message.id,
|
||||
keyId: messageId,
|
||||
@ -3441,6 +3445,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
instanceId: this.instanceId,
|
||||
};
|
||||
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
|
||||
}
|
||||
} else {
|
||||
await this.prismaRepository.message.deleteMany({ where: { id: message.id } });
|
||||
}
|
||||
@ -3771,6 +3776,10 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
private async formatUpdateMessage(data: UpdateMessageDto) {
|
||||
try {
|
||||
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
|
||||
return data;
|
||||
}
|
||||
|
||||
const msg: any = await this.getMessage(data.key, true);
|
||||
|
||||
if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') {
|
||||
@ -3804,6 +3813,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
try {
|
||||
const oldMessage: any = await this.getMessage(data.key, true);
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
|
||||
if (!oldMessage) throw new NotFoundException('Message not found');
|
||||
if (oldMessage?.key?.remoteJid !== jid) {
|
||||
throw new BadRequestException('RemoteJid does not match');
|
||||
@ -3812,6 +3822,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
// 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 });
|
||||
if (messageSent) {
|
||||
@ -3828,7 +3839,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
);
|
||||
|
||||
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({
|
||||
where: { key: { path: ['id'], equals: messageId } },
|
||||
});
|
||||
@ -3840,6 +3851,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
if ((message.key.valueOf() as any)?.deleted) {
|
||||
new BadRequestException('You cannot edit deleted messages');
|
||||
}
|
||||
|
||||
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
|
||||
oldMessage.message.conversation = data.text;
|
||||
} else {
|
||||
@ -3853,6 +3865,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
|
||||
},
|
||||
});
|
||||
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
|
||||
const messageUpdate: any = {
|
||||
messageId: message.id,
|
||||
keyId: messageId,
|
||||
@ -3866,6 +3880,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return messageSent;
|
||||
} catch (error) {
|
||||
|
Loading…
Reference in New Issue
Block a user