Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,14 @@ export class BaileysStartupService extends ChannelStartupService {
progress?: number;
syncType?: proto.HistorySync.HistorySyncType;
}) => {
//These logs are crucial; when something changes in Baileys/WhatsApp, we can more easily understand what changed!
this.logger.debug('Messages abaixo');
this.logger.debug(messages);
this.logger.debug('Chats abaixo');
this.logger.debug(chats);
this.logger.debug('Contatos abaixo');
this.logger.debug(contacts);

try {
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
console.log('received on-demand history sync, messages=', messages);
Expand Down Expand Up @@ -967,14 +975,29 @@ export class BaileysStartupService extends ChannelStartupService {
}

const contactsMap = new Map();
const contactsMapLidJid = new Map();

for (const contact of contacts) {
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: contact.id });
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,
Expand All @@ -986,13 +1009,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.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[] = [];
Expand Down Expand Up @@ -1475,8 +1524,14 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose(messageRaw);

sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);

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);

Expand Down