diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 5a180df4..9fba9878 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1173,8 +1173,8 @@ export class BaileysStartupService extends ChannelStartupService { const oldMessage = await this.getMessage(editedMessage.key, true); if ((oldMessage as any)?.id) { const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs) - ? Math.floor(editedMessage.timestampMs.toNumber() / 1000) // Convert to int32 by dividing by 1000 to get seconds - : Math.floor((editedMessage.timestampMs as number) / 1000); // Convert to int32 by dividing by 1000 to get seconds + ? Math.floor(editedMessage.timestampMs.toNumber() / 1000) + : Math.floor((editedMessage.timestampMs as number) / 1000); await this.prismaRepository.message.update({ where: { id: (oldMessage as any).id }, @@ -1205,7 +1205,7 @@ export class BaileysStartupService extends ChannelStartupService { continue; } - await this.baileysCache.set(messageKey, true, 30 * 60); + await this.baileysCache.set(messageKey, true, 5 * 60); if ( (type !== 'notify' && type !== 'append') || @@ -1311,21 +1311,31 @@ export class BaileysStartupService extends ChannelStartupService { data: messageRaw, }); - if (received.key.fromMe === false) { - if (msg.status === status[3]) { - this.logger.log(`Update not read messages ${received.key.remoteJid}`); + const remoteJid = received.key.remoteJid; + const timestamp = msg.messageTimestamp; + const fromMe = received.key.fromMe.toString(); + const messageKey = `${remoteJid}_${timestamp}_${fromMe}`; - await this.updateChatUnreadMessages(received.key.remoteJid); - } else if (msg.status === status[4]) { - this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`); + const cachedTimestamp = await this.baileysCache.get(messageKey); - await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp); + if (!cachedTimestamp) { + if (!received.key.fromMe) { + if (msg.status === status[3]) { + this.logger.log(`Update not read messages ${remoteJid}`); + await this.updateChatUnreadMessages(remoteJid); + } else if (msg.status === status[4]) { + this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`); + await this.updateMessagesReadedByTimestamp(remoteJid, timestamp); + } + } else { + // is send message by me + this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`); + await this.updateMessagesReadedByTimestamp(remoteJid, timestamp); } - } else { - // is send message by me - this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`); - await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp); + await this.baileysCache.set(messageKey, true, 5 * 60); + } else { + this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`); } if (isMedia) { @@ -1481,7 +1491,7 @@ export class BaileysStartupService extends ChannelStartupService { const cached = await this.baileysCache.get(updateKey); if (cached) { - this.logger.info(`Message duplicated ignored: ${key.id}`); + this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`); continue; } @@ -1497,7 +1507,7 @@ export class BaileysStartupService extends ChannelStartupService { } } - if (key.remoteJid !== 'status@broadcast') { + if (key.remoteJid !== 'status@broadcast' && key.id !== undefined) { let pollUpdates: any; if (update.pollUpdates) { @@ -1525,19 +1535,20 @@ export class BaileysStartupService extends ChannelStartupService { continue; } + const message: any = { + messageId: findMessage.id, + keyId: key.id, + remoteJid: key?.remoteJid, + fromMe: key.fromMe, + participant: key?.remoteJid, + status: status[update.status] ?? 'DELETED', + pollUpdates, + instanceId: this.instanceId, + }; + if (update.message === null && update.status === undefined) { this.sendDataWebhook(Events.MESSAGES_DELETE, key); - const message: any = { - messageId: findMessage.id, - keyId: key.id, - remoteJid: key.remoteJid, - fromMe: key.fromMe, - participant: key?.remoteJid, - status: 'DELETED', - instanceId: this.instanceId, - }; - if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) await this.prismaRepository.messageUpdate.create({ data: message, @@ -1556,29 +1567,32 @@ export class BaileysStartupService extends ChannelStartupService { if (!key.fromMe && key.remoteJid) { readChatToUpdate[key.remoteJid] = true; - if (status[update.status] === status[4]) { - this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`); - this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp); + const remoteJid = key.remoteJid; + const timestamp = findMessage.messageTimestamp; + const fromMe = key.fromMe.toString(); + const messageKey = `${remoteJid}_${timestamp}_${fromMe}`; + + const cachedTimestamp = await this.baileysCache.get(messageKey); + + if (!cachedTimestamp) { + if (status[update.status] === status[4]) { + this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`); + await this.updateMessagesReadedByTimestamp(remoteJid, timestamp); + await this.baileysCache.set(messageKey, true, 5 * 60); + } + + await this.prismaRepository.message.update({ + where: { id: findMessage.id }, + data: { status: status[update.status] }, + }); + } else { + this.logger.info( + `Update readed messages duplicated ignored in message.update [avoid deadlock]: ${messageKey}`, + ); } } - - await this.prismaRepository.message.update({ - where: { id: findMessage.id }, - data: { status: status[update.status] }, - }); } - const message: any = { - messageId: findMessage.id, - keyId: key.id, - remoteJid: key.remoteJid, - fromMe: key.fromMe, - participant: key?.remoteJid, - status: status[update.status], - pollUpdates, - instanceId: this.instanceId, - }; - this.sendDataWebhook(Events.MESSAGES_UPDATE, message); if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) @@ -3745,7 +3759,7 @@ export class BaileysStartupService extends ChannelStartupService { break; } } - + if (!mediaMessage) { throw 'The message is not of the media type'; }