Skip to content

Commit db60f04

Browse files
Merge pull request #2372 from JefersonRamos/bugfix/messaging-history-chats-return-remoteJid-invalid
fix(whatsapp): enhance contact and chat handling with improved JID ma…
2 parents b7aa2f2 + 21513f5 commit db60f04

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,14 +1040,29 @@ export class BaileysStartupService extends ChannelStartupService {
10401040
}
10411041

10421042
const contactsMap = new Map();
1043+
const contactsMapLidJid = new Map();
10431044

10441045
for (const contact of contacts) {
1046+
let jid = null;
1047+
1048+
if (contact?.id?.search('@lid') !== -1) {
1049+
if (contact.phoneNumber) {
1050+
jid = contact.phoneNumber;
1051+
}
1052+
}
1053+
1054+
if (!jid) {
1055+
jid = contact?.id;
1056+
}
1057+
10451058
if (contact.id && (contact.notify || contact.name)) {
1046-
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid: contact.id });
1059+
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid });
10471060
}
1061+
1062+
contactsMapLidJid.set(contact.id, { jid });
10481063
}
10491064

1050-
const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = [];
1065+
const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = [];
10511066
const chatsRepository = new Set(
10521067
(await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map(
10531068
(chat) => chat.remoteJid,
@@ -1059,13 +1074,39 @@ export class BaileysStartupService extends ChannelStartupService {
10591074
continue;
10601075
}
10611076

1062-
chatsRaw.push({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name });
1077+
let remoteJid = null;
1078+
let remoteLid = null;
1079+
1080+
if (chat.id.search('@lid') !== -1) {
1081+
const contact = contactsMapLidJid.get(chat.id);
1082+
1083+
remoteLid = chat.id;
1084+
1085+
if (contact && contact.jid) {
1086+
remoteJid = contact.jid;
1087+
}
1088+
}
1089+
1090+
if (!remoteLid && chat.accountLid && chat.accountLid.search('@lid') !== -1) {
1091+
remoteLid = chat.accountLid;
1092+
}
1093+
1094+
if (!remoteJid) {
1095+
remoteJid = chat.id;
1096+
}
1097+
1098+
chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name });
10631099
}
10641100

10651101
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
10661102

10671103
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
1068-
await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true });
1104+
const chatsToCreateMany = JSON.parse(JSON.stringify(chatsRaw)).map((chat) => {
1105+
delete chat.remoteLid;
1106+
return chat;
1107+
});
1108+
1109+
await this.prismaRepository.chat.createMany({ data: chatsToCreateMany, skipDuplicates: true });
10691110
}
10701111

10711112
const messagesRaw: any[] = [];
@@ -1549,8 +1590,14 @@ export class BaileysStartupService extends ChannelStartupService {
15491590
this.logger.verbose(messageRaw);
15501591

15511592
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
1552-
if ((messageRaw.key as any).remoteJid?.includes('@lid') && (messageRaw.key as any).remoteJidAlt) {
1553-
(messageRaw.key as any).remoteJid = (messageRaw.key as any).remoteJidAlt;
1593+
1594+
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
1595+
const lid = messageRaw.key.remoteJid;
1596+
1597+
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
1598+
messageRaw.key.remoteJidAlt = lid;
1599+
1600+
messageRaw.key.addressingMode = 'pn';
15541601
}
15551602
console.log(messageRaw);
15561603

0 commit comments

Comments
 (0)