mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 19:32:21 -06:00
fix: normalize remoteJid in message updates and handle race condition in contact cache
This commit is contained in:
@@ -1565,8 +1565,15 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
for await (const { key, update } of args) {
|
for await (const { key, update } of args) {
|
||||||
const keyAny = key as any;
|
const keyAny = key as any;
|
||||||
const normalizedRemoteJid = keyAny.remoteJid?.replace(/:.*$/, '');
|
if (keyAny.remoteJid) {
|
||||||
const normalizedParticipant = keyAny.participant?.replace(/:.*$/, '');
|
keyAny.remoteJid = keyAny.remoteJid.replace(/:.*$/, '');
|
||||||
|
}
|
||||||
|
if (keyAny.participant) {
|
||||||
|
keyAny.participant = keyAny.participant.replace(/:.*$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedRemoteJid = keyAny.remoteJid;
|
||||||
|
const normalizedParticipant = keyAny.participant;
|
||||||
|
|
||||||
if (settings?.groupsIgnore && normalizedRemoteJid?.includes('@g.us')) {
|
if (settings?.groupsIgnore && normalizedRemoteJid?.includes('@g.us')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -1644,18 +1651,38 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
const searchId = originalMessageId || key.id;
|
const searchId = originalMessageId || key.id;
|
||||||
|
|
||||||
const messages = (await this.prismaRepository.$queryRaw`
|
let retries = 0;
|
||||||
SELECT * FROM "Message"
|
const maxRetries = 3;
|
||||||
WHERE "instanceId" = ${this.instanceId}
|
|
||||||
AND "key"->>'id' = ${searchId}
|
while (retries < maxRetries) {
|
||||||
LIMIT 1
|
const messages = (await this.prismaRepository.$queryRaw`
|
||||||
`) as any[];
|
SELECT * FROM "Message"
|
||||||
findMessage = messages[0] || null;
|
WHERE "instanceId" = ${this.instanceId}
|
||||||
|
AND "key"->>'id' = ${searchId}
|
||||||
|
LIMIT 1
|
||||||
|
`) as any[];
|
||||||
|
findMessage = messages[0] || null;
|
||||||
|
|
||||||
|
if (findMessage?.id) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
retries++;
|
||||||
|
if (retries < maxRetries) {
|
||||||
|
await delay(2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!findMessage?.id) {
|
if (!findMessage?.id) {
|
||||||
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
|
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (findMessage?.key?.remoteJid && findMessage.key.remoteJid !== key.remoteJid) {
|
||||||
|
this.logger.verbose(
|
||||||
|
`Updating key.remoteJid from ${key.remoteJid} to ${findMessage.key.remoteJid} based on stored message`,
|
||||||
|
);
|
||||||
|
key.remoteJid = findMessage.key.remoteJid;
|
||||||
|
}
|
||||||
message.messageId = findMessage.id;
|
message.messageId = findMessage.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -164,9 +164,24 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
|||||||
logger.verbose(
|
logger.verbose(
|
||||||
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
|
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
|
||||||
);
|
);
|
||||||
await prismaRepository.isOnWhatsapp.create({
|
try {
|
||||||
data: dataPayload,
|
await prismaRepository.isOnWhatsapp.create({
|
||||||
});
|
data: dataPayload,
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
// Check for unique constraint violation (Prisma error code P2002)
|
||||||
|
if (error.code === 'P2002' && error.meta?.target?.includes('remoteJid')) {
|
||||||
|
logger.verbose(
|
||||||
|
`[saveOnWhatsappCache] Race condition detected for ${remoteJid}, updating existing record instead.`,
|
||||||
|
);
|
||||||
|
await prismaRepository.isOnWhatsapp.update({
|
||||||
|
where: { remoteJid: remoteJid },
|
||||||
|
data: dataPayload,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Loga o erro mas não para a execução dos outros promises
|
// Loga o erro mas não para a execução dos outros promises
|
||||||
|
|||||||
Reference in New Issue
Block a user