Skip to content

Commit 70d334b

Browse files
committed
fix(whatsapp): enhance contact and chat handling with improved JID mapping and debug logging
1 parent cd800f2 commit 70d334b

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,14 @@ export class BaileysStartupService extends ChannelStartupService {
939939
progress?: number;
940940
syncType?: proto.HistorySync.HistorySyncType;
941941
}) => {
942+
//These logs are crucial; when something changes in Baileys/WhatsApp, we can more easily understand what changed!
943+
this.logger.debug('Messages abaixo');
944+
this.logger.debug(messages);
945+
this.logger.debug('Chats abaixo');
946+
this.logger.debug(chats);
947+
this.logger.debug('Contatos abaixo');
948+
this.logger.debug(contacts);
949+
942950
try {
943951
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
944952
console.log('received on-demand history sync, messages=', messages);
@@ -967,14 +975,30 @@ export class BaileysStartupService extends ChannelStartupService {
967975
}
968976

969977
const contactsMap = new Map();
978+
const contactsMapLidJid = new Map();
970979

971980
for (const contact of contacts) {
981+
982+
let jid = null;
983+
984+
if (contact?.id?.search('@lid') !== -1) {
985+
if (contact.phoneNumber) {
986+
jid = contact.phoneNumber;
987+
}
988+
}
989+
990+
if (!jid) {
991+
jid = contact?.id;
992+
}
993+
972994
if (contact.id && (contact.notify || contact.name)) {
973-
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid: contact.id });
995+
contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid });
974996
}
997+
998+
contactsMapLidJid.set(contact.id, { jid });
975999
}
9761000

977-
const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = [];
1001+
const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = [];
9781002
const chatsRepository = new Set(
9791003
(await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map(
9801004
(chat) => chat.remoteJid,
@@ -986,7 +1010,24 @@ export class BaileysStartupService extends ChannelStartupService {
9861010
continue;
9871011
}
9881012

989-
chatsRaw.push({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name });
1013+
let remoteJid = null;
1014+
let remoteLid = null;
1015+
1016+
if (chat.id.search('@lid') !== -1) {
1017+
const contact = contactsMapLidJid.get(chat.id);
1018+
1019+
remoteLid = chat.id;
1020+
1021+
if (contact && contact.jid) {
1022+
remoteJid = contact.jid;
1023+
}
1024+
}
1025+
1026+
if (!remoteJid) {
1027+
remoteJid = chat.id;
1028+
}
1029+
1030+
chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name });
9901031
}
9911032

9921033
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);

0 commit comments

Comments
 (0)