Skip to content

Commit c313f5b

Browse files
committed
Correção lId
1 parent d32d6a5 commit c313f5b

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

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

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys';
151151

152152
const groupMetadataCache = new CacheService(new CacheEngine(configService, 'groups').getEngine());
153153

154+
// Function to normalize JID and handle LID/JID conversion
155+
function normalizeJid(jid: string): string {
156+
if (!jid) return jid;
157+
158+
// Remove LID suffix and convert to standard JID format
159+
if (jid.includes(':lid')) {
160+
return jid.split(':')[0] + '@s.whatsapp.net';
161+
}
162+
163+
// Remove participant suffix from group messages
164+
if (jid.includes(':') && jid.includes('@g.us')) {
165+
return jid.split(':')[0] + '@g.us';
166+
}
167+
168+
// Remove any other participant suffixes
169+
if (jid.includes(':') && !jid.includes('@g.us')) {
170+
return jid.split(':')[0] + '@s.whatsapp.net';
171+
}
172+
173+
return jid;
174+
}
175+
154176
// Adicione a função getVideoDuration no início do arquivo
155177
async function getVideoDuration(input: Buffer | string | Readable): Promise<number> {
156178
const MediaInfoFactory = (await import('mediainfo.js')).default;
@@ -1091,7 +1113,8 @@ export class BaileysStartupService extends ChannelStartupService {
10911113
}
10921114
}
10931115

1094-
const messageKey = `${this.instance.id}_${received.key.id}`;
1116+
const normalizedJid = normalizeJid(received.key.remoteJid);
1117+
const messageKey = `${this.instance.id}_${normalizedJid}_${received.key.id}`;
10951118
const cached = await this.baileysCache.get(messageKey);
10961119

10971120
if (cached && !editedMessage) {
@@ -1118,8 +1141,9 @@ export class BaileysStartupService extends ChannelStartupService {
11181141
continue;
11191142
}
11201143

1144+
const normalizedRemoteJid = normalizeJid(received.key.remoteJid);
11211145
const existingChat = await this.prismaRepository.chat.findFirst({
1122-
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
1146+
where: { instanceId: this.instanceId, remoteJid: normalizedRemoteJid },
11231147
select: { id: true, name: true },
11241148
});
11251149

@@ -1198,7 +1222,8 @@ export class BaileysStartupService extends ChannelStartupService {
11981222
const { remoteJid } = received.key;
11991223
const timestamp = msg.messageTimestamp;
12001224
const fromMe = received.key.fromMe.toString();
1201-
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1225+
const normalizedRemoteJid = normalizeJid(remoteJid);
1226+
const messageKey = `${normalizedRemoteJid}_${timestamp}_${fromMe}`;
12021227

12031228
const cachedTimestamp = await this.baileysCache.get(messageKey);
12041229

@@ -1303,13 +1328,13 @@ export class BaileysStartupService extends ChannelStartupService {
13031328
});
13041329

13051330
const contact = await this.prismaRepository.contact.findFirst({
1306-
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
1331+
where: { remoteJid: normalizedRemoteJid, instanceId: this.instanceId },
13071332
});
13081333

13091334
const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
1310-
remoteJid: received.key.remoteJid,
1335+
remoteJid: normalizedRemoteJid,
13111336
pushName: received.key.fromMe ? '' : received.key.fromMe == null ? '' : received.pushName,
1312-
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
1337+
profilePicUrl: (await this.profilePicture(normalizedRemoteJid)).profilePictureUrl,
13131338
instanceId: this.instanceId,
13141339
};
13151340

@@ -1366,7 +1391,11 @@ export class BaileysStartupService extends ChannelStartupService {
13661391
continue;
13671392
}
13681393

1369-
const updateKey = `${this.instance.id}_${key.id}_${update.status}`;
1394+
// Normalize JID and ensure we have valid key components
1395+
const normalizedJid = normalizeJid(key.remoteJid);
1396+
const messageId = key.id || 'unknown';
1397+
const status = update.status || 'unknown';
1398+
const updateKey = `${this.instance.id}_${normalizedJid}_${messageId}_${status}`;
13701399

13711400
const cached = await this.baileysCache.get(updateKey);
13721401

@@ -1442,7 +1471,8 @@ export class BaileysStartupService extends ChannelStartupService {
14421471
const { remoteJid } = key;
14431472
const timestamp = findMessage.messageTimestamp;
14441473
const fromMe = key.fromMe.toString();
1445-
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1474+
const normalizedRemoteJid = normalizeJid(remoteJid);
1475+
const messageKey = `${normalizedRemoteJid}_${timestamp}_${fromMe}`;
14461476

14471477
const cachedTimestamp = await this.baileysCache.get(messageKey);
14481478

@@ -4130,8 +4160,15 @@ export class BaileysStartupService extends ChannelStartupService {
41304160
const contentType = getContentType(message.message);
41314161
const contentMsg = message?.message[contentType] as any;
41324162

4163+
// Normalize JID to handle LID/JID conversion
4164+
const normalizedKey = {
4165+
...message.key,
4166+
remoteJid: normalizeJid(message.key.remoteJid),
4167+
participant: message.key.participant ? normalizeJid(message.key.participant) : undefined,
4168+
};
4169+
41334170
const messageRaw = {
4134-
key: message.key,
4171+
key: normalizedKey,
41354172
pushName:
41364173
message.pushName ||
41374174
(message.key.fromMe

0 commit comments

Comments
 (0)