fix(baileys): normalize remote JIDs for consistent database lookups

This commit is contained in:
Vitordotpy
2025-12-15 21:38:45 -03:00
parent 6f2bef678c
commit 2e3c8184ef

View File

@@ -1561,7 +1561,12 @@ export class BaileysStartupService extends ChannelStartupService {
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true} const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
for await (const { key, update } of args) { for await (const { key, update } of args) {
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) { // Normalize JIDs immediately to ensure consistent DB lookups
const keyAny = key as any;
if (keyAny.remoteJid) keyAny.remoteJid = keyAny.remoteJid.replace(/:.*$/, '');
if (keyAny.participant) keyAny.participant = keyAny.participant.replace(/:.*$/, '');
if (settings?.groupsIgnore && keyAny.remoteJid?.includes('@g.us')) {
continue; continue;
} }
@@ -1612,9 +1617,9 @@ export class BaileysStartupService extends ChannelStartupService {
const message: any = { const message: any = {
keyId: key.id, keyId: key.id,
remoteJid: key?.remoteJid, remoteJid: keyAny?.remoteJid?.replace(/:.*$/, ''),
fromMe: key.fromMe, fromMe: key.fromMe,
participant: key?.participant, participant: keyAny?.participant?.replace(/:.*$/, ''),
status: status[update.status] ?? 'SERVER_ACK', status: status[update.status] ?? 'SERVER_ACK',
pollUpdates, pollUpdates,
instanceId: this.instanceId, instanceId: this.instanceId,
@@ -4662,26 +4667,20 @@ export class BaileysStartupService extends ChannelStartupService {
return obj; return obj;
} }
private prepareMessage(message: proto.IWebMessageInfo): any { private prepareMessage(message: WAMessage): Message {
const contentType = getContentType(message.message); const keyAny = message.key as any;
const contentMsg = message?.message[contentType] as any; const messageRaw: any = {
key: {
const messageRaw = { ...message.key,
key: message.key, // Save key exactly as it comes from Baileys remoteJid: keyAny.remoteJid?.replace(/:.*$/, ''),
pushName: participant: keyAny.participant?.replace(/:.*$/, ''),
message.pushName || },
(message.key.fromMe pushName: message.pushName,
? 'Você' message: message.message,
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)), messageType: getContentType(message.message),
status: status[message.status], messageTimestamp: message.messageTimestamp,
message: this.deserializeMessageBuffers({ ...message.message }), source: getDevice(keyAny.id),
contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
messageType: contentType || 'unknown',
messageTimestamp: Long.isLong(message.messageTimestamp)
? message.messageTimestamp.toNumber()
: (message.messageTimestamp as number),
instanceId: this.instanceId, instanceId: this.instanceId,
source: getDevice(message.key.id),
}; };
if (!messageRaw.status && message.key.fromMe === false) { if (!messageRaw.status && message.key.fromMe === false) {