Skip to content

Commit 348a4ff

Browse files
committed
fix: Corrige problemas com deadlocks
## Erros: - code: "40P01", message: "deadlock detected". - Argument status is missing.
1 parent fc00916 commit 348a4ff

1 file changed

Lines changed: 62 additions & 48 deletions

File tree

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

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,8 @@ export class BaileysStartupService extends ChannelStartupService {
11731173
const oldMessage = await this.getMessage(editedMessage.key, true);
11741174
if ((oldMessage as any)?.id) {
11751175
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
1176-
? Math.floor(editedMessage.timestampMs.toNumber() / 1000) // Convert to int32 by dividing by 1000 to get seconds
1177-
: Math.floor((editedMessage.timestampMs as number) / 1000); // Convert to int32 by dividing by 1000 to get seconds
1176+
? Math.floor(editedMessage.timestampMs.toNumber() / 1000)
1177+
: Math.floor((editedMessage.timestampMs as number) / 1000);
11781178

11791179
await this.prismaRepository.message.update({
11801180
where: { id: (oldMessage as any).id },
@@ -1205,7 +1205,7 @@ export class BaileysStartupService extends ChannelStartupService {
12051205
continue;
12061206
}
12071207

1208-
await this.baileysCache.set(messageKey, true, 30 * 60);
1208+
await this.baileysCache.set(messageKey, true, 5 * 60);
12091209

12101210
if (
12111211
(type !== 'notify' && type !== 'append') ||
@@ -1311,21 +1311,31 @@ export class BaileysStartupService extends ChannelStartupService {
13111311
data: messageRaw,
13121312
});
13131313

1314-
if (received.key.fromMe === false) {
1315-
if (msg.status === status[3]) {
1316-
this.logger.log(`Update not read messages ${received.key.remoteJid}`);
1317-
1318-
await this.updateChatUnreadMessages(received.key.remoteJid);
1319-
} else if (msg.status === status[4]) {
1320-
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
1321-
1322-
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1314+
const remoteJid = received.key.remoteJid;
1315+
const timestamp = msg.messageTimestamp;
1316+
const fromMe = received.key.fromMe.toString();
1317+
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1318+
1319+
const cachedTimestamp = await this.baileysCache.get(messageKey);
1320+
1321+
if (!cachedTimestamp) {
1322+
if (!received.key.fromMe) {
1323+
if (msg.status === status[3]) {
1324+
this.logger.log(`Update not read messages ${remoteJid}`);
1325+
await this.updateChatUnreadMessages(remoteJid);
1326+
} else if (msg.status === status[4]) {
1327+
this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
1328+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
1329+
}
1330+
} else {
1331+
// is send message by me
1332+
this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
1333+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
13231334
}
1324-
} else {
1325-
// is send message by me
1326-
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
13271335

1328-
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1336+
await this.baileysCache.set(messageKey, true, 5 * 60);
1337+
} else {
1338+
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
13291339
}
13301340

13311341
if (isMedia) {
@@ -1481,7 +1491,7 @@ export class BaileysStartupService extends ChannelStartupService {
14811491
const cached = await this.baileysCache.get(updateKey);
14821492

14831493
if (cached) {
1484-
this.logger.info(`Message duplicated ignored: ${key.id}`);
1494+
this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`);
14851495
continue;
14861496
}
14871497

@@ -1497,7 +1507,7 @@ export class BaileysStartupService extends ChannelStartupService {
14971507
}
14981508
}
14991509

1500-
if (key.remoteJid !== 'status@broadcast') {
1510+
if (key.remoteJid !== 'status@broadcast' && key.id !== undefined) {
15011511
let pollUpdates: any;
15021512

15031513
if (update.pollUpdates) {
@@ -1525,19 +1535,20 @@ export class BaileysStartupService extends ChannelStartupService {
15251535
continue;
15261536
}
15271537

1538+
const message: any = {
1539+
messageId: findMessage.id,
1540+
keyId: key.id,
1541+
remoteJid: key?.remoteJid,
1542+
fromMe: key.fromMe,
1543+
participant: key?.remoteJid,
1544+
status: status[update.status] ?? 'DELETED',
1545+
pollUpdates,
1546+
instanceId: this.instanceId,
1547+
};
1548+
15281549
if (update.message === null && update.status === undefined) {
15291550
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
15301551

1531-
const message: any = {
1532-
messageId: findMessage.id,
1533-
keyId: key.id,
1534-
remoteJid: key.remoteJid,
1535-
fromMe: key.fromMe,
1536-
participant: key?.remoteJid,
1537-
status: 'DELETED',
1538-
instanceId: this.instanceId,
1539-
};
1540-
15411552
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
15421553
await this.prismaRepository.messageUpdate.create({
15431554
data: message,
@@ -1556,29 +1567,32 @@ export class BaileysStartupService extends ChannelStartupService {
15561567
if (!key.fromMe && key.remoteJid) {
15571568
readChatToUpdate[key.remoteJid] = true;
15581569

1559-
if (status[update.status] === status[4]) {
1560-
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
1561-
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
1570+
const remoteJid = key.remoteJid;
1571+
const timestamp = findMessage.messageTimestamp;
1572+
const fromMe = key.fromMe.toString();
1573+
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1574+
1575+
const cachedTimestamp = await this.baileysCache.get(messageKey);
1576+
1577+
if (!cachedTimestamp) {
1578+
if (status[update.status] === status[4]) {
1579+
this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`);
1580+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
1581+
await this.baileysCache.set(messageKey, true, 5 * 60);
1582+
}
1583+
1584+
await this.prismaRepository.message.update({
1585+
where: { id: findMessage.id },
1586+
data: { status: status[update.status] },
1587+
});
1588+
} else {
1589+
this.logger.info(
1590+
`Update readed messages duplicated ignored in message.update [avoid deadlock]: ${messageKey}`,
1591+
);
15621592
}
15631593
}
1564-
1565-
await this.prismaRepository.message.update({
1566-
where: { id: findMessage.id },
1567-
data: { status: status[update.status] },
1568-
});
15691594
}
15701595

1571-
const message: any = {
1572-
messageId: findMessage.id,
1573-
keyId: key.id,
1574-
remoteJid: key.remoteJid,
1575-
fromMe: key.fromMe,
1576-
participant: key?.remoteJid,
1577-
status: status[update.status],
1578-
pollUpdates,
1579-
instanceId: this.instanceId,
1580-
};
1581-
15821596
this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
15831597

15841598
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
@@ -3745,7 +3759,7 @@ export class BaileysStartupService extends ChannelStartupService {
37453759
break;
37463760
}
37473761
}
3748-
3762+
37493763
if (!mediaMessage) {
37503764
throw 'The message is not of the media type';
37513765
}

0 commit comments

Comments
 (0)