From c20865dff1d45042fd70f8ac8605fd81de22b557 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Sun, 11 Sep 2022 11:06:38 +0530 Subject: [PATCH] fix: correctly set latest message in chat + lastMsgRecvTimestamp --- src/Types/Chat.ts | 7 ++++++- src/Utils/history.ts | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Types/Chat.ts b/src/Types/Chat.ts index c37da77..d21c70f 100644 --- a/src/Types/Chat.ts +++ b/src/Types/Chat.ts @@ -74,7 +74,12 @@ export type ChatModification = | { delete: true, lastMessages: LastMessageList } export type InitialReceivedChatsState = { - [jid: string]: { lastMsgRecvTimestamp: number } + [jid: string]: { + /** the last message received from the other party */ + lastMsgRecvTimestamp?: number + /** the absolute last message in the chat */ + lastMsgTimestamp: number + } } export type InitialAppStateSyncOptions = { diff --git a/src/Utils/history.ts b/src/Utils/history.ts index 4a92e80..f59c618 100644 --- a/src/Utils/history.ts +++ b/src/Utils/history.ts @@ -45,20 +45,30 @@ export const processHistoryMessage = ( } const msgs = chat.messages || [] + delete chat.messages + for(const item of msgs) { const message = item.message! const uqId = `${message.key.remoteJid}:${message.key.id}` if(!historyCache.has(uqId)) { messages.push(message) - const curItem = recvChats[message.key.remoteJid!] + let curItem = recvChats[message.key.remoteJid!] const timestamp = toNumber(message.messageTimestamp) - if(!curItem || timestamp > curItem.lastMsgRecvTimestamp) { - recvChats[chat.id] = { lastMsgRecvTimestamp: timestamp } + if(!curItem || timestamp > curItem.lastMsgTimestamp) { + curItem = { lastMsgTimestamp: timestamp } + recvChats[chat.id] = curItem // keep only the most recent message in the chat array chat.messages = [{ message }] } + if( + !message.key.fromMe + && (!curItem?.lastMsgRecvTimestamp || timestamp > curItem.lastMsgRecvTimestamp) + ) { + curItem.lastMsgRecvTimestamp = timestamp + } + historyCache.add(uqId) } }