mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2026-03-21 20:18:40 -06:00
Merge pull request #2372 from JefersonRamos/bugfix/messaging-history-chats-return-remoteJid-invalid
fix(whatsapp): enhance contact and chat handling with improved JID ma…
This commit is contained in:
@@ -1040,14 +1040,29 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
const contactsMap = new Map();
|
||||
const contactsMapLidJid = new Map();
|
||||
|
||||
for (const contact of contacts) {
|
||||
if (contact.id && (contact.notify || contact.name)) {
|
||||
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid: contact.id });
|
||||
let jid = null;
|
||||
|
||||
if (contact?.id?.search('@lid') !== -1) {
|
||||
if (contact.phoneNumber) {
|
||||
jid = contact.phoneNumber;
|
||||
}
|
||||
}
|
||||
|
||||
if (!jid) {
|
||||
jid = contact?.id;
|
||||
}
|
||||
|
||||
if (contact.id && (contact.notify || contact.name)) {
|
||||
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid });
|
||||
}
|
||||
|
||||
contactsMapLidJid.set(contact.id, { jid });
|
||||
}
|
||||
|
||||
const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = [];
|
||||
const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = [];
|
||||
const chatsRepository = new Set(
|
||||
(await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map(
|
||||
(chat) => chat.remoteJid,
|
||||
@@ -1059,13 +1074,39 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
continue;
|
||||
}
|
||||
|
||||
chatsRaw.push({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name });
|
||||
let remoteJid = null;
|
||||
let remoteLid = null;
|
||||
|
||||
if (chat.id.search('@lid') !== -1) {
|
||||
const contact = contactsMapLidJid.get(chat.id);
|
||||
|
||||
remoteLid = chat.id;
|
||||
|
||||
if (contact && contact.jid) {
|
||||
remoteJid = contact.jid;
|
||||
}
|
||||
}
|
||||
|
||||
if (!remoteLid && chat.accountLid && chat.accountLid.search('@lid') !== -1) {
|
||||
remoteLid = chat.accountLid;
|
||||
}
|
||||
|
||||
if (!remoteJid) {
|
||||
remoteJid = chat.id;
|
||||
}
|
||||
|
||||
chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name });
|
||||
}
|
||||
|
||||
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
|
||||
|
||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
|
||||
await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true });
|
||||
const chatsToCreateMany = JSON.parse(JSON.stringify(chatsRaw)).map((chat) => {
|
||||
delete chat.remoteLid;
|
||||
return chat;
|
||||
});
|
||||
|
||||
await this.prismaRepository.chat.createMany({ data: chatsToCreateMany, skipDuplicates: true });
|
||||
}
|
||||
|
||||
const messagesRaw: any[] = [];
|
||||
@@ -1549,8 +1590,14 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
this.logger.verbose(messageRaw);
|
||||
|
||||
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
|
||||
if ((messageRaw.key as any).remoteJid?.includes('@lid') && (messageRaw.key as any).remoteJidAlt) {
|
||||
(messageRaw.key as any).remoteJid = (messageRaw.key as any).remoteJidAlt;
|
||||
|
||||
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
|
||||
const lid = messageRaw.key.remoteJid;
|
||||
|
||||
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
|
||||
messageRaw.key.remoteJidAlt = lid;
|
||||
|
||||
messageRaw.key.addressingMode = 'pn';
|
||||
}
|
||||
console.log(messageRaw);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user