feat: more accurately handle app state sync

This commit is contained in:
Adhiraj Singh
2022-06-11 12:02:08 +05:30
parent 79518787b6
commit 5cb71ac862
7 changed files with 290 additions and 30 deletions

View File

@@ -1,8 +1,9 @@
import { promisify } from 'util'
import { inflate } from 'zlib'
import { proto } from '../../WAProto'
import { Chat, Contact } from '../Types'
import { Chat, Contact, InitialReceivedChatsState } from '../Types'
import { isJidUser } from '../WABinary'
import { toNumber } from './generics'
import { downloadContentFromMessage } from './messages-media'
const inflatePromise = promisify(inflate)
@@ -21,7 +22,11 @@ export const downloadHistory = async(msg: proto.IHistorySyncNotification) => {
return syncData
}
export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Set<string>) => {
export const processHistoryMessage = (
item: proto.IHistorySync,
historyCache: Set<string>,
recvChats: InitialReceivedChatsState
) => {
const messages: proto.IWebMessageInfo[] = []
const contacts: Contact[] = []
const chats: Chat[] = []
@@ -40,6 +45,13 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
const uqId = `${message.key.remoteJid}:${message.key.id}`
if(!historyCache.has(uqId)) {
messages.push(message)
const curItem = recvChats[message.key.remoteJid]
const timestamp = toNumber(message.messageTimestamp)
if(!message.key.fromMe && (!curItem || timestamp > curItem.lastMsgRecvTimestamp)) {
recvChats[message.key.remoteJid] = { lastMsgRecvTimestamp: timestamp }
}
historyCache.add(uqId)
}
}
@@ -81,7 +93,11 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
}
}
export const downloadAndProcessHistorySyncNotification = async(msg: proto.IHistorySyncNotification, historyCache: Set<string>) => {
export const downloadAndProcessHistorySyncNotification = async(
msg: proto.IHistorySyncNotification,
historyCache: Set<string>,
recvChats: InitialReceivedChatsState
) => {
const historyMsg = await downloadHistory(msg)
return processHistoryMessage(historyMsg, historyCache)
return processHistoryMessage(historyMsg, historyCache, recvChats)
}