From 3686a1bc831fb6255d9607f159461d51bd670ab9 Mon Sep 17 00:00:00 2001 From: Claudemir Todo Bom Date: Thu, 4 Jul 2024 21:16:06 -0300 Subject: [PATCH] enhancement of setting messages as read --- .../src/helpers/SetTicketMessagesAsRead.ts | 57 +++++++++++++------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/backend/src/helpers/SetTicketMessagesAsRead.ts b/backend/src/helpers/SetTicketMessagesAsRead.ts index 0532157f..251d0050 100644 --- a/backend/src/helpers/SetTicketMessagesAsRead.ts +++ b/backend/src/helpers/SetTicketMessagesAsRead.ts @@ -1,4 +1,4 @@ -import { proto, WASocket } from "@whiskeysockets/baileys"; +import { WASocket, proto } from "@whiskeysockets/baileys"; import { cacheLayer } from "../libs/cache"; import { getIO } from "../libs/socket"; import Message from "../models/Message"; @@ -10,10 +10,12 @@ const SetTicketMessagesAsRead = async (ticket: Ticket): Promise => { await ticket.update({ unreadMessages: 0 }); await cacheLayer.set(`contacts:${ticket.contactId}:unreads`, "0"); let companyId: number; + try { const wbot = await GetTicketWbot(ticket); - const getJsonMessage = await Message.findAll({ + const messages = await Message.findAll({ + attributes: ["id", "companyId", "dataJson"], where: { ticketId: ticket.id, fromMe: false, @@ -21,25 +23,44 @@ const SetTicketMessagesAsRead = async (ticket: Ticket): Promise => { }, order: [["createdAt", "DESC"]] }); - companyId = getJsonMessage[0]?.companyId; - getJsonMessage.map(async m => { - const message: proto.IWebMessageInfo = JSON.parse(m.dataJson); - if (message.key) { - await (wbot as WASocket).readMessages([message.key]); - } - }); + if (messages.length === 0) return; + + companyId = messages[0]?.companyId; + + const messageKeys = messages + .map(m => { + const message: proto.IWebMessageInfo = JSON.parse(m.dataJson); + return message.key; + }) + .filter(key => key !== undefined); - if (getJsonMessage.length > 0) { - const lastMessages: proto.IWebMessageInfo = JSON.parse( - getJsonMessage[0].dataJson - ); - if (lastMessages?.key?.remoteJid && lastMessages.key.fromMe === false) { - await (wbot as WASocket).chatModify( - { markRead: true, lastMessages: [lastMessages] }, - `${lastMessages.key.remoteJid}` + // Process message keys in batches of 250 + const batchSize = 250; + for (let i = 0; i < messageKeys.length; i += batchSize) { + const batch = messageKeys.slice(i, i + batchSize); + (wbot as WASocket).readMessages(batch).catch(err => { + logger.error( + { error: err as Error }, + `Could not mark messages as read. Err: ${err?.message}` ); - } + }); + } + + const lastMessage: proto.IWebMessageInfo = JSON.parse(messages[0].dataJson); + if (lastMessage?.key?.remoteJid && !lastMessage.key.fromMe) { + // Asynchronous chatModify call + (wbot as WASocket) + .chatModify( + { markRead: true, lastMessages: [lastMessage] }, + lastMessage.key.remoteJid + ) + .catch(err => { + logger.error( + { error: err as Error }, + `Could not modify chat. Err: ${err?.message}` + ); + }); } await Message.update(